-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
102 lines (95 loc) · 3.24 KB
/
Copy pathBoard.java
File metadata and controls
102 lines (95 loc) · 3.24 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
/**
* Board.java
*
* @author Deep Patel
*
* REMARKS: This class stores and initializes the board on which the
* game is played and which piece is located at which spot
*/
public class Board {
//Instance variables
private Spot[][] boxes;
//Constructor
public Board() {
this.boxes = new Spot[8][8];
this.resetBoard();
}
//Returns the position of a box and what piece is in the spot
public Spot getBox(int x, int y) {
if (x < 0 || x > boxes.length-1 || y < 0 || y > boxes.length-1) {
System.out.println("Error");
}
return boxes[x][y];
}
/**
* This method is used to get the letter of the piece at a specific location
* Parameters:
* int row and col give the position of the piece
* Returns a char of the symbol of the piece
*/
public char getSymbol(int row, int col){
char symbol = ' '; //Empty if no piece on square
Piece piece = boxes[row][col].getPiece();
if(piece instanceof Pawn){
symbol = 'p';
if(piece.getWhosePiece()){
symbol = 'P';
}
}else if(piece instanceof Rook){
symbol = 'r';
if(piece.getWhosePiece()){
symbol = 'R';
}
}else if(piece instanceof Bishop){
symbol = 'b';
if(piece.getWhosePiece()){
symbol = 'B';
}
}else if(piece instanceof Knight){
symbol = 'n';
if(piece.getWhosePiece()){
symbol = 'N';
}
}else if(piece instanceof King){
symbol = 'k';
if(piece.getWhosePiece()){
symbol = 'K';
}
}else if(piece instanceof Queen){
symbol = 'q';
if(piece.getWhosePiece()){
symbol = 'Q';
}
}
return symbol;
}
//Reset the board back to the original position
public void resetBoard() {
boxes[0][0] = new Spot(0, 0, new Rook(false));
boxes[0][1] = new Spot(0, 1, new Knight(false));
boxes[0][2] = new Spot(0, 2, new Bishop(false));
boxes[0][3] = new Spot(0, 3, new King(false));
boxes[0][4] = new Spot(0, 4, new Queen(false));
boxes[0][5] = new Spot(0, 5, new Bishop(false));
boxes[0][6] = new Spot(0, 6, new Knight(false));
boxes[0][7] = new Spot(0, 7, new Rook(false));
for(int i = 0; i < boxes.length; i++){
boxes[1][i] = new Spot(1, i, new Pawn(false));
boxes[6][i] = new Spot(6, i, new Pawn(true));
}
boxes[7][0] = new Spot(7, 0, new Rook(true));
boxes[7][1] = new Spot(7, 1, new Knight(true));
boxes[7][2] = new Spot(7, 2, new Bishop(true));
boxes[7][3] = new Spot(7, 3, new King(true));
boxes[7][4] = new Spot(7, 4, new Queen(true));
boxes[7][5] = new Spot(7, 5, new Bishop(true));
boxes[7][6] = new Spot(7, 6, new Knight(true));
boxes[7][7] = new Spot(7, 7, new Rook(true));
// initialize remaining boxes without any piece
for (int i = 2; i < 6; i++) {
for (int j = 0; j < boxes.length; j++) {
boxes[i][j] = new Spot(i, j, null);
}
}
}
}