-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
228 lines (207 loc) · 8.43 KB
/
Board.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.*;
//---------------imports-------------------
public class Board{//add the tap listener later in android studio
//variables
private BoardCell[][] board;
private King wk;
private King bk;
public Piece remove;
public boolean isFirstClick;
int[] from;
private Player player;
public Board(boolean isWhite,Player p){//fix the function
this.player=p;
board=new BoardCell[8][8];
for(int i=0;i<board.length;i++){
for(int j=0;j<board.length;j++){
board[i][j]=new BoardCell(i,j);
//color handling
if((i+j)%2==0){
board[i][j].isWhite=true;
}
else{
board[i][j].isWhite=false;
}
if(i==0){//add backrank pieces
if(j==0 || j==7){
board[i][j].setPiece(new Rook(i,j,!isWhite));
}
if(j==1 || j==6){
//add other colored horse
board[i][j].setPiece(new Knight(i, j,!isWhite));
}
if(j==2 || j==5){
//add other colored bishop
board[i][j].setPiece(new Bishop(i, j, !isWhite));
}
if(isWhite){
if(j==3){
//add other colored queen
board[i][j].setPiece(new Queen(i, j, !isWhite));
}
if(j==4){
if(!isWhite){
wk=new King(i, j, !isWhite);
board[i][j].setPiece(wk);
}
else{
bk=new King(i, j, !isWhite);
board[i][j].setPiece(bk);
}
}
}else{
if(j==3){
if(!isWhite){
wk=new King(i, j, !isWhite);
board[i][j].setPiece(wk);
}
else{
bk=new King(i, j, !isWhite);
board[i][j].setPiece(bk);
}
}
if(j==4){
//add other colored queen
board[i][j].setPiece(new Queen(i, j, !isWhite));
}
}
}
if(i==1){
//add not our color pawn
board[i][j].setPiece(new Pawn(i, j, !isWhite));
}
if(i==6){
//add our colored pawn
board[i][j].setPiece(new Pawn(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(i==7){
if(j==0 || j==7){
//ad our colored rook
board[i][j].setPiece(new Rook(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(j==1 || j==6){
//add our colored horse
board[i][j].setPiece(new Knight(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(j==2 || j==5){
//add our colored bishop
board[i][j].setPiece(new Bishop(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(j==3){
//add our colored queen
board[i][j].setPiece(new Queen(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(isWhite){
if(j==3){
//add our colored queen
board[i][j].setPiece(new Queen(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
if(j==4){
if(!isWhite){
bk=new King(i, j, isWhite);
board[i][j].setPiece(bk);
}
else{
wk=new King(i, j, isWhite);
board[i][j].setPiece(wk);
}
player.pieces.add(board[i][j].getPiece());
}
}else{
if(j==3){
if(!isWhite){
bk=new King(i, j, isWhite);
board[i][j].setPiece(bk);
}
else{
wk=new King(i, j, isWhite);
board[i][j].setPiece(wk);
}
player.pieces.add(board[i][j].getPiece());
}
if(j==4){
board[i][j].setPiece(new Queen(i, j, isWhite));
player.pieces.add(board[i][j].getPiece());
}
}
}
}
}
}
public void psuedoClickListener(int rawX,int rawY){
int row=rawY%BoardCell.size;
int col=rawX%BoardCell.size;
Piece curr=this.getCell(row, col).getPiece();
if(isFirstClick){
if(curr!=null && curr.isWhite==player.isWhite){
from=curr.getRawPos();
isFirstClick=!isFirstClick;
curr.drawValidMoves();
}
}else{
row=rawY%BoardCell.size;
col=rawX%BoardCell.size;
for(int[] mov: curr.getMoves()){
if(mov[0]==row && mov[1]==col){
player.move(from, new int[]{row,col});
isFirstClick=!isFirstClick;
this.move(from , mov);
player.ch.move=from[0]+","+from[1]+" "+mov[0]+","+mov[1];
}
}
this.draw();
}
}
public void move(int[] from,int[] to) {
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].getPiece().move(to[0], to[1]);
this.board[from[0]][from[1]].setPiece(null);
}
public void playerMove(int[] from,int[] to){
Piece p=this.getCell(to[0], to[1]).getPiece();
if(p!=null){
player.remove(p);
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].setPiece(null);
}else{
this.board[to[0]][to[1]].setPiece(this.board[from[0]][from[1]].getPiece());
this.board[from[0]][from[1]].setPiece(null);
}
}
public void draw(){}
public void printBoard(){
for(int i=0;i<this.board.length;i++){
for(int j=0;j<this.board[i].length;j++){
board[i][j].print();
}
System.out.println();
}
}
public BoardCell getCell(int row,int col){
return this.board[row][col];
}
public BoardCell getCell(Piece p){
for(int i=0;i<this.board.length;i++){
for(int j=0;j<this.board.length;j++){
if(this.board[i][j].getPiece()==p){
return this.board[i][j];
}
}
}
return null;
}
public boolean isCheck(boolean isWhite){//check if it is check, here we input the color of the player
//check kings implementation of ischeck to see the dir
if(isWhite){
return wk.isCheck(this);
}else{
return bk.isCheck(this);
}
}
}