-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
95 lines (83 loc) · 2.63 KB
/
Board.java
File metadata and controls
95 lines (83 loc) · 2.63 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
public class Board {
private char[][] board;
public int getBoardSize() {
return boardSize;
}
private int boardSize;
public Board(int boardSize){
this.boardSize = boardSize;
this.board = new char[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
this.board[i][j] = ' ';
}
}
}
public boolean isValidMove(int x, int y){
return board[x][y] != ' ';
}
public void placeMove(int x, int y, char move){
board[x][y] = move;
}
public void printBoard(){
String displayBoard = "";
for (int i = 0; i < boardSize; i++) { //for each row
for(int j = 0; j < boardSize; j++) { //printing board;s move first
if(j == boardSize-1)
displayBoard += board[i][j];
else
displayBoard += board[i][j] + "|";
}
if (i != boardSize-1) { // if it's not at the end of each row
displayBoard += "\n"; //next row
for (int j=0;j< boardSize;j++) {
if (j == boardSize-1)
displayBoard+= "-"; //printing border for the board
else
displayBoard +="-+";
}
displayBoard += "\n"; //onto the next row
}
}
System.out.println(displayBoard);
}
public boolean isDiagonal (char move){
for (int row = 0; row < board.length; row++) {
if (board[row][row] != move)
return false;
}
return true;
}
public boolean isAntiDiagonal (char move){
for (int row = 0; row < board.length; row++) {
if (board[row][boardSize - row - 1] != move)
return false;
}
return true;
}
public boolean isColumn (char move, int j){
for (int row = 0; row < board.length; row++) {
if (board[row][j] != move) {
return false;
}
}
return true;
}
public boolean isRow (char move, int i){
for(int col = 0; col<board.length; col++){
if (board[i][col] != move) {
return false;
}
}
return true;
}
public boolean isFinished(){
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != 'X' && board[i][j] != 'O') // that means it's empty
return false;
}
}
return true;
}
}