-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBoard.java
More file actions
181 lines (157 loc) · 4.36 KB
/
Board.java
File metadata and controls
181 lines (157 loc) · 4.36 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
import java.util.Arrays;
/**
* Write a description of class Board here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Board
{
private final int MAX_ROWS;
private final int MAX_COLS;
private Pieces[][] gameboard;
public Board(int max_rows, int max_cols)
{
this.MAX_ROWS = max_rows;
this.MAX_COLS = max_cols;
gameboard = new Pieces[MAX_ROWS][MAX_COLS];
}
public int getMaxRows()
{
return MAX_ROWS;
}
public int getMaxCols()
{
return MAX_COLS;
}
public void printBoard()
{
printBoard("");
}
public void printBoard(String msg)
{
HelperFunctions.clearScreenBlueJ();
String colHeader = "T 1 | 2 | 3 ";
System.out.println(colHeader);
System.out.println("-".repeat(colHeader.length()));
for (int row=0; row < MAX_ROWS; row++)
{
String printStr = "";
for (int col=0; col < MAX_COLS; col++)
{
char playerName;
if (gameboard[row][col] == null)
{
playerName = ' ';
} else
{
playerName = gameboard[row][col].getName();
}
printStr = printStr + "|" + " " + playerName + " ";
}
System.out.println((row + 1) + " " + printStr.substring(1));
if (row < (MAX_ROWS - 1))
{
System.out.println("-".repeat(printStr.length() + 1));
}
}
System.out.println("\n" + msg);
}
private boolean isSpaceFree(int row, int col)
{
if ((row >= MAX_ROWS) || (col >= MAX_COLS))
{
return false;
}
if (gameboard[row][col] == null)
{
return true;
} else
{
return false;
}
}
public boolean play(char player, int row, int col)
{
if (isSpaceFree(row, col))
{
gameboard[row][col] = new Pieces(player, row, col);
Main.plays.add(gameboard[row][col]);
return true;
}
{
return false;
}
}
public boolean autoPlay(char player)
{
boolean played = false;
a:
for (int row = 0; row < MAX_ROWS; row++)
{
for (int col = 0; col < MAX_COLS; col++)
{
if (play(player, row, col))
{
played = true;
break a;
}
}
}
return played;
}
public boolean haveAWinner()
{
boolean winner = false;
// winner in rows
for (int row = 0; row < MAX_ROWS; row++)
{
if (!Arrays.stream(gameboard[row]).anyMatch(obj -> obj == null))
{
winner = Arrays.stream(gameboard[row]).map(x -> x.getName())
.distinct().count() <= 1;
}
}
// winner in cols
if (!winner)
{
a:
for (int col = 0; col < MAX_COLS; col++)
{
Pieces[] column = new Pieces[MAX_ROWS];
for (int row = 0; row < MAX_ROWS; row++)
{
column[row] = gameboard[row][col];
}
if (!Arrays.stream(column).anyMatch(obj -> obj == null))
{
winner = Arrays.stream(column).map(x -> x.getName())
.distinct().count() <= 1;
if (winner)
{
break a;
}
}
}
}
// winner in diagonals
if (!winner)
{
Pieces[] diagonals = new Pieces[MAX_ROWS];
for (int diag = 0; diag < MAX_ROWS; diag++)
{
diagonals[diag] = gameboard[diag][diag];
}
if (!Arrays.stream(diagonals).anyMatch(obj -> obj == null))
{
winner = Arrays.stream(diagonals).map(x -> x.getName())
.distinct().count() <= 1;
}
}
return winner;
}
public boolean spaceAvailable()
{
return Arrays.stream(gameboard).flatMap(x -> Arrays.stream(x)) .anyMatch(obj -> obj == null);
}
}