-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
201 lines (182 loc) · 6.02 KB
/
Board.java
File metadata and controls
201 lines (182 loc) · 6.02 KB
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
import jdk.internal.jimage.ImageStrings;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
public class Board {
private Piece[][] array;
public Board(String fileName, Player white, Player black) {
int pieceType;
int col;
int row;
String nextWord;
Player owner;
this.array = new Piece[8][8];
try {
File file = new File(fileName);
if (file.exists() == false) {
System.err.println("Error: Cannot find file " + fileName);
System.exit(1);
}
Scanner in = new Scanner(file);
while (in.hasNext()) {
if ((nextWord = in.nextLine()).length() > 2) {
pieceType = nextWord.charAt(0);
col = nextWord.charAt(1) - '0';
row = nextWord.charAt(2) - '0';
owner = black;
if (pieceType >= 'a' && pieceType <= 'z') {
owner = white;
}
switch (pieceType) {
case 'K': case 'k': this.addPiece(new King(col, row, owner)); break;
case 'B': case 'b': this.addPiece(new Bishop(col, row, owner)); break;
case 'N': case 'n': this.addPiece(new Knight(col, row, owner)); break;
case 'Q': case 'q': this.addPiece(new Queen(col, row, owner)); break;
case 'R': case 'r': this.addPiece(new Rook(col, row, owner)); break;
case 'P': case 'p': this.addPiece(new Pawn(col, row, owner)); break;
}
}
}
in.close();
} catch (FileNotFoundException e) {
System.err.println("Error file not found : " + e);
System.exit(1);
}
}
public List<Coordinates> getAllCoordinates() {
List<Coordinates> coordi = new ArrayList();
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++) {
coordi.add(new Coordinates(i,j));
}
}
return coordi;
}
public List<Piece> getPieces(Player player) {
List<Piece> list = new ArrayList<>();
for (Piece piece : getPieces()){
if (piece.sameColor(player.getKing())){
list.add(piece);
}
}
return list;
}
public List<Piece> getPieces() {
List<Piece> list = new ArrayList<>();
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (!isEmptyCell(x,y)) {
list.add(this.array[x][y]);
}
}
}
return list;
}
public void addPiece(Piece piece) {
int x = piece.getX();
int y = piece.getY();
this.array[x][y] = piece;
}
public Piece getPiece(Coordinates coordinates) {
int x = coordinates.getX();
int y = coordinates.getY();
return getPiece(x, y);
}
public Piece getPiece(int x, int y) {
return this.array[x][y];
}
public void emptyCell(Coordinates coordinates) {
int x = coordinates.getX();
int y = coordinates.getY();
if (!isEmptyCell(coordinates)) {
array[x][y] = null;
}
}
public boolean isEmptyCell(Coordinates coordinates) {
int x = coordinates.getX();
int y = coordinates.getY();
return isEmptyCell(x, y);
}
public boolean isEmptyCell(int x, int y) {
if (array[x][y] == null) {
return true;
}
return false;
}
public boolean sameColumnNothingBetween(Coordinates origin, Coordinates destination) {
int supY;
int infX;
int infY;
if(origin.getY() < destination.getY()){
infX = origin.getX();
infY = origin.getY();
supY = destination.getY();
}else {
infX = destination.getX();
infY = destination.getY();
supY = origin.getY();
}
for(int y = infY+1; y < supY ; y++){
if (!isEmptyCell(infX,y)){
return false;
}
}
return true;
}
public boolean sameRowNothingBetween(Coordinates origin, Coordinates destination) {
int supX;
int infX;
int infY;
if(origin.getX() < destination.getX()){
infX = origin.getX();
infY = origin.getY();
supX = destination.getX();
}else {
supX = origin.getX();
infX = destination.getX();
infY = destination.getY();
}
for(int x = infX+1; x < supX ; x++){
if (!isEmptyCell(x,infY)){
return false;
}
}
return true;
}
public boolean sameDiagonalNothingBetween(Coordinates origin, Coordinates destination) {
//on regarde d'abord le quel des deux a le plus petit y, pour par la suite limiter les actions a 2.
//on regarde ensuite celui qui a le plus petit x pour voir si on fait une diagonal qui descend a droite ou a
//gauche
int supX;
int infX;
int infY;
int supY;
if(origin.getY() < destination.getY()){
infX = origin.getX();
infY = origin.getY();
supX = destination.getX();
supY = destination.getY();
}else {
supY = origin.getY();
supX = origin.getX();
infX = destination.getX();
infY = destination.getY();
}
if (infX < supX){
for (int ite = 1; ite < supY-infY; ite++){
if (!isEmptyCell(ite+infX, infY+ite)){
return false;
}
}
}else {
for (int ite = 1; ite < supY-infY; ite++){
if (!isEmptyCell(infX-ite, infY+ite)){
return false;
}
}
}
return true;
}
}