-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.java
141 lines (113 loc) · 5.37 KB
/
King.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ChessGame;
/**
*
* @author Alizey
*/
public class King extends Piece{
public King(boolean colour, boolean alive){
this.colour = colour;
this.alive = alive;
}
@Override
void move(int inirow, int inicolumn, int firow, int ficolumn){
boolean check = true;
Piece.che = false;
if((Chessboard.chance%2 == 0 && colour == true) || (Chessboard.chance%2 == 1 && colour == false)){
if(inirow < 8 && inicolumn < 8 && inirow >= 0 && inicolumn >= 0 &&(firow < 8 && ficolumn < 8 && firow >= 0 && ficolumn >= 0) ){
if(Chessboard.boxes[firow][ficolumn] == null){
if(inirow == firow && inicolumn == ficolumn)
check = false;
else{
if(Math.abs(firow - inirow) == 1 && Math.abs(ficolumn - inicolumn) == 1){
// for diagonal movement
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else if(Math.abs(firow - inirow) == 1 && (ficolumn - inicolumn) == 0){
// for vertical movement
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else if(Math.abs(ficolumn - inicolumn) == 1 && (firow - inirow) == 0){
// for horizontal movement
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else
check = false;
}
}
else{
//if(inirow != firow && inicolumn != ficolumn){
if(inirow == firow && inicolumn == ficolumn)
check = false;
else{
if(Math.abs(firow - inirow) == 1 && Math.abs(ficolumn - inicolumn) == 1){
// for diagonal movement
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[firow][ficolumn].colour){
kill((Piece)Chessboard.boxes[firow][ficolumn], firow, ficolumn);
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else
check = false;
}
else if(Math.abs(firow - inirow) == 1 && (ficolumn - inicolumn) == 0){
// for vertical movement
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[firow][ficolumn].colour){
kill((Piece)Chessboard.boxes[firow][ficolumn], firow, ficolumn);
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else
check = false;
}
else if(Math.abs(ficolumn - inicolumn) == 1 && (firow - inirow) == 0){
// for horizontal movement
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[firow][ficolumn].colour){
kill((Piece)Chessboard.boxes[firow][ficolumn], firow, ficolumn);
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
}
else
check = false;
}
else
check = false;
}
}
}
else
check = false;
}
else
check = false;
if(check == false)
System.out.println("Wrong inputs for move!");
else
Chessboard.chance++;
}
@Override
void kill(Piece p, int row, int column){
p.alive = false;
Chessboard.graveyard[Chessboard.gravecounter] = p;
Chessboard.boxes[row][column] = null;
Chessboard.gravecounter++;
}
@Override
boolean check(int inirow, int inicolumn){
return false;
}
@Override
public String toString(){
if(colour == true)
return "wK";
else
return "bK";
}
}