forked from JoeMeas/ChessGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessBoard.java
50 lines (39 loc) · 1.57 KB
/
ChessBoard.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
//Uses mapping of 8x8 chess board
//Checks if piece is on board, remove if lands on piece
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class ChessBoard {
public final int LENGTH = 8;
public final int WIDTH = 8;
public ChessSquare[][] squares;
public ChessBoard(){
squares = new ChessSquare[LENGTH][WIDTH];
for(int i = 0; i <= LENGTH - 1; i++){
for(int j = 0; j <= WIDTH - 1; j++){
squares[i][j] = new ChessSquare(i, j, null);
}
}
}
public void printBoard(){
final StringBuilder sb = new StringBuilder();
// Generate squares and y axis
Stream.of(squares).flatMap(i ->
Stream.concat( Stream.of( String.valueOf( 8 - i[0].getPosX()) + " | " ),
Stream.concat( Stream.of(i).map(j -> j.getGraphic() + " "), Stream.of("\n")))
).forEach(s -> sb.append(s));
// Generate x axis
sb.append(" ---------------\n ");
IntStream.range(0, 8).mapToObj(i -> String.format("%c ", 'A' + i)).forEach(s -> sb.append(s));
System.out.println(sb);
}
public ChessPiece getPiece(final String positionStart) {
return this.getSquare(positionStart).getPiece();
}
public void setPiece(final ChessPiece chessPiece) {
this.squares[chessPiece.posX][chessPiece.posY].setPiece(chessPiece);
}
public ChessSquare getSquare (final String positionStart) {
final PositionTuple position = new PositionTuple(positionStart);
return squares[position.x][position.y];
}
}