-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
203 lines (164 loc) · 6.24 KB
/
Copy pathBoard.java
File metadata and controls
203 lines (164 loc) · 6.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
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
/*
Description:
The Board class represents a game board consisting of a grid of Cell objects.
It provides methods to manage the board state, retrieve board information,
and display the board/boards in a structured format.
Fields:
- rows: The number of rows in the board.
- columns: The number of columns in the board.
- gameType: The type of game being played on this board.
- board: A 2D array representing the board grid, containing Cell objects.
Constructors:
- Board(): Creates an empty board with undefined dimensions and game type.
- Board(int rows, int columns): Creates a board with specified dimensions and initializes all cells.
- Board(int rows, int columns, String type): Creates a board with specified dimensions and game type.
Static Methods:
- boardsToString(Board[][] boards): returns a string representation of a 2D array of boards
- displayBoards(Board[][] boards): prints the current state of the 2D array of boards
Other Important Methods:
- getCell(int row, int column): Returns the Cell object at the specified coordinates.
- displayBoard(): Prints the current state of the board with row and column labels.
- isBoardCellEmpty(int row, int column): Checks if the specified cell on the board is empty.
- toString(): returns the string representation of a board.
*/
public class Board {
private final int rows;
private final int columns;
private String gameType;
private Cell[][] board;
// CONSTRUCTORS
// to create empty board
public Board() {
this.rows = -1;
this.columns = -1;
this.gameType = null;
this.board = null;
}
// to create a board of rows x columns size
public Board(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.gameType = null;
this.board = new Cell[rows][columns];
// fill the board with empty cells
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = new Cell(i,j);
}
}
}
public Board(int rows, int columns, String type) {
this.rows = rows;
this.columns = columns;
this.gameType = type;
this.board = new Cell[rows][columns];
// fill the board with empty cells
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = new Cell(i,j);
}
}
}
// GETTER methods
public int getRows() {
return this.rows;
}
public int getColumns() {
return this.columns;
}
public String getGameType() {
return this.gameType;
}
public Cell[][] getBoard() {
return this.board;
}
public Cell getCell(int row, int column) {
return this.board[row][column];
}
// SETTER methods - can't change the rows/columns of a board after you create it
public void setGameType(String type) {
this.gameType = type;
}
// VALIDATION method - if a cell on the board is filled or not
public boolean isBoardCellEmpty(int row, int column) {
return this.board[row][column].isEmpty();
}
// DISPLAY methods:
// helper function used to get the board width separators
private String getBoardWidthSeperator(boolean withNumbers) {
String seperator = "";
for (int i = 0; i < this.columns; i++) {
if (i == 0 && withNumbers) seperator += " ";
seperator += "+---";
}
return seperator + "+";
}
// toString method to get the string representation of the board
public String toString() {
String board = "";
for (int i = 0; i < this.rows; i++) {
if (i == 0) {
board += " ";
for (int c = 0; c < this.columns; c++) {
if (c < 10) board += (c + " ");
else board += (c + " ");
}
board += "\n";
}
board += this.getBoardWidthSeperator(true);
board += "\n";
for (int j = 0; j < this.columns; j++) {
if (j == 0) {
if (i < 10) board += (i + " ");
else board += (i + " ");
}
char cellValue = this.board[i][j].getValue() != null ? this.board[i][j].getValue().getSymbol() : ' ';
board += ("| " + cellValue + " ");
if (j == this.columns - 1) board += "|\n";
}
}
board += this.getBoardWidthSeperator(true);
board += "\n";
return board;
}
public void displayBoard() {
System.out.print(this.toString());
}
// STATIC DISPLAY METHODS
private static String getBoardSeperator(int numCols, int numBoards) {
String seperator = "";
for (int b = 0; b < numBoards; b++) {
for (int i = 0; i < numCols; i++) seperator += "+---";
seperator += "+ ";
}
return seperator;
}
private static String boardRowToString(Board b, int row) {
String str = "| ";
for (int col = 0; col < b.getColumns(); col++) {
char val = b.getCell(row, col).getValue() == null ? ' ' : b.getCell(row, col).getValue().getSymbol(); // if null then put empty space there
str += (val + " | ");
}
return str;
}
public static String boardsToString(Board[][] boards) {
int gridRows = boards.length, gridCols = boards[0].length;
int boardRows = boards[0][0].getRows(), boardCols = boards[0][0].getColumns();
String seperator = getBoardSeperator(boardCols, gridCols) + "\n";
String str = seperator;
for (int gr = 0; gr < gridRows; gr++) {
for (int br = 0; br < boardRows; br++) {
for (Board board : boards[gr]) {
str += boardRowToString(board, br);
}
str += "\n";
str += seperator;
}
if (gr < gridRows - 1) str += seperator;
}
return str;
}
public static void displayBoards(Board[][] boards) {
System.out.println(boardsToString(boards));
}
}