-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cs
More file actions
292 lines (259 loc) · 8.87 KB
/
Board.cs
File metadata and controls
292 lines (259 loc) · 8.87 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System.Collections.Generic;
namespace ConnectFourGame
{
// Logic-only class: not a MonoBehaviour, do not attach to GameObject
public class Board
{
public int[,] board = new int[6, 7];
private Stack<Move> moves = new Stack<Move>();
private Stack<Move> undoedMoves = new Stack<Move>();
public void Reset()
{
board = new int[6, 7];
moves.Clear();
undoedMoves.Clear();
}
public bool IsWin(int row, int col)
{
bool iswin = false;
int color = board[row, col];
// vertical
if (row + 3 < 6 &&
board[row + 1, col] == color &&
board[row + 2, col] == color &&
board[row + 3, col] == color)
iswin = true;
// diagonal y=x
if (row + 3 < 6 && col + 3 < 7 &&
board[row + 1, col + 1] == color &&
board[row + 2, col + 2] == color &&
board[row + 3, col + 3] == color)
iswin = true;
if (row + 2 < 6 && col + 2 < 7 && row - 1 >= 0 && col - 1 >= 0 &&
board[row + 2, col + 2] == color &&
board[row + 1, col + 1] == color &&
board[row - 1, col - 1] == color)
return true;
if (row + 1 < 6 && col + 1 < 7 && row - 2 >= 0 && col - 2 >= 0 &&
board[row - 2, col - 2] == color &&
board[row + 1, col + 1] == color &&
board[row - 1, col - 1] == color)
return true;
if (row - 3 >= 0 && col - 3 >= 0 &&
board[row - 1, col - 1] == color &&
board[row - 2, col - 2] == color &&
board[row - 3, col - 3] == color &&
board[row, col] == color)
iswin = true;
// diagonal y=-x
if (row + 3 < 6 && col - 3 >= 0 &&
board[row, col] == color &&
board[row + 1, col - 1] == color &&
board[row + 2, col - 2] == color &&
board[row + 3, col - 3] == color)
return true;
if (row - 1 >= 0 && col + 1 < 7 && row + 2 < 6 && col - 2 >= 0 &&
board[row + 2, col - 2] == color &&
board[row + 1, col - 1] == color &&
board[row - 1, col + 1] == color)
return true;
if (row - 2 >= 0 && col + 2 < 7 && row + 1 < 6 && col - 1 >= 0 &&
board[row - 2, col + 2] == color &&
board[row - 1, col + 1] == color &&
board[row + 1, col - 1] == color)
return true;
if (row - 3 >= 0 && col + 3 < 7 &&
board[row - 1, col + 1] == color &&
board[row - 2, col + 2] == color &&
board[row - 3, col + 3] == color &&
board[row, col] == color)
iswin = true;
// horizontal
if (col + 3 < 7 &&
board[row, col + 1] == color &&
board[row, col + 2] == color &&
board[row, col + 3] == color &&
board[row, col] == color)
iswin = true;
if (col - 1 >= 0 && col + 2 < 7 &&
board[row, col - 1] == color &&
board[row, col + 1] == color &&
board[row, col + 2] == color)
return true;
if (col - 2 >=0 && col + 1 < 7 &&
board[row, col - 1] == color &&
board[row, col - 2] == color &&
board[row, col + 1] == color &&
board[row, col] == color)
iswin = true;
if (col - 3 >= 0 &&
board[row, col - 1] == color &&
board[row, col - 2] == color &&
board[row, col - 3] == color &&
board[row, col] == color)
iswin = true;
return iswin;
}
public bool CanDrop(int col)
{
return board[0, col] == 0;
}
// Returns the row where the piece was dropped, or -1 if not possible
public int Drop(int id, int col)
{
if (CanDrop(col))
{
for (int i = 5; i >= 0; i--)
{
if (board[i, col] == 0)
{
board[i, col] = id;
moves.Push(new Move(i, col, id));
ClearUndoedMoves();
return i;
}
}
}
return -1;
}
public bool IsDraw()
{
for (int i = 0; i < 7; i++)
if (CanDrop(i))
return false;
return true;
}
public int UnDo()
{
if (moves.Count > 0)
{
Move last = moves.Pop();
board[last.Row, last.Column] = 0;
undoedMoves.Push(last);
return 0;
}
else
{
// TODO: Show "no previous moves" message in Unity UI if desired
return -1;
}
}
public int ReDo()
{
if (undoedMoves.Count > 0)
{
Move last = undoedMoves.Pop();
board[last.Row, last.Column] = last.Id;
moves.Push(last);
return 0;
}
else
{
// TODO: Show "no undoed moves" message in Unity UI if desired
return -1;
}
}
public void ClearUndoedMoves()
{
undoedMoves.Clear();
}
public List<int> LegalMoves()
{
var legalmoves = new List<int>();
for (int i = 0; i < 7; i++)
if (CanDrop(i))
legalmoves.Add(i);
return legalmoves;
}
public int GetCell(int row, int col)
{
return board[row, col];
}
public Board Clone()
{
Board newBoard = new Board();
// Assuming your board has a 2D array called grid[,] and other relevant state
for (int row = 0; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
newBoard.board[row, col] = this.board[row, col];
}
}
// Copy any other relevant fields (e.g., turn count, last move, etc.)
// Example:
// newBoard.turnCount = this.turnCount;
// newBoard.lastMove = this.lastMove;
return newBoard;
}
public bool PlayerIsWin(int id)
{
// Horizontal check
for (int row = 0; row < 6; row++)
{
for (int col = 0; col < 4; col++)
{
if (board[row, col] == id &&
board[row, col + 1] == id &&
board[row, col + 2] == id &&
board[row, col + 3] == id)
{
return true;
}
}
}
// Vertical check
for (int col = 0; col < 7; col++)
{
for (int row = 0; row < 3; row++)
{
if (board[row, col] == id &&
board[row + 1, col] == id &&
board[row + 2, col] == id &&
board[row + 3, col] == id)
{
return true;
}
}
}
// Diagonal down-right check
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
if (board[row, col] == id &&
board[row + 1, col + 1] == id &&
board[row + 2, col + 2] == id &&
board[row + 3, col + 3] == id)
{
return true;
}
}
}
// Diagonal up-right check
for (int row = 3; row < 6; row++)
{
for (int col = 0; col < 4; col++)
{
if (board[row, col] == id &&
board[row - 1, col + 1] == id &&
board[row - 2, col + 2] == id &&
board[row - 3, col + 3] == id)
{
return true;
}
}
}
return false;
}
public bool IsFull()
{
for (int i = 0; i < 6; i++)
{
if (CanDrop(i))
return false;
}
return true;
}
}
}