-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomBot.js
More file actions
334 lines (327 loc) · 9.14 KB
/
randomBot.js
File metadata and controls
334 lines (327 loc) · 9.14 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
let pieces;
let isPlayersTurn;
let isSecondPlayersTurn;
let assignLetter;
let winner;
let gameFinished;
//Setup function
function setup() {
//Creates canvas
createCanvas(702, 701);
//Set framerate
frameRate(10);
//Initialises variables
isPlayersTurn = true;
isCPUsTurn = false;
assignLetter = "f";
gameFinished = false;
//Initialise piece array
pieces = [[null, null, null, null, null, null],[null, null, null, null, null, null],[null, null, null, null, null, null],[null, null, null, null, null, null],[null, null, null, null, null, null],[null, null, null, null, null, null],[null, null, null, null, null, null]];
}
//Draw function
function draw() {
//Create grid
stroke(0);
//Vertical lines
line(1, 100, 1, 701);
line(101, 100, 101, 701);
line(201, 100, 201, 701);
line(301, 100, 301, 701);
line(401, 100, 401, 701);
line(501, 100, 501, 701);
line(601, 100, 601, 701);
line(701, 100, 701, 701);
//Horizontal lines
line(0, 100, 702, 100);
line(0, 200, 702, 200);
line(0, 300, 702, 300);
line(0, 400, 702, 400);
line(0, 500, 702, 500);
line(0, 600, 702, 600);
line(0, 700, 702, 700);
//Draw info
stroke(31, 46, 255);
fill(31, 46, 255);
textSize(20);
text("Blue = Player 1", 500, 25);
stroke(255, 54, 54);
fill(255, 54, 54);
textSize(20);
text("Red = CPU", 500, 60);
//Draw pieces
if (!gameFinished){
drawPieces();
checkWinner();
}
//Logic for CPUs turn
//
//
let isCPUPlaced = false;
if (isCPUsTurn && !gameFinished){
//Get random column to place piece in
let column = random(7);
column = floor(column);
print(column);
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isCPUPlaced && pieces[column][i] === null){
pieces[column][i] = assignLetter;
isCPUPlaced = true;
}
}
}
//If player has placed piece, make it so it switches which player's turn it is
if (isCPUPlaced){
//Draw updated board
drawPieces();
//Switch players turn
if (isPlayersTurn){
isPlayersTurn = false;
isCPUsTurn = true;
assignLetter = "s";
}
else if (isCPUsTurn){
isPlayersTurn = true;
isCPUsTurn = false;
assignLetter = "f";
}
}
}
//Function to draw pieces on grid
function drawPieces(){
//Loop through x values
for (i = 0; i < pieces.length; i++){
let YMult;
//Loop through y values
for (j = 0; j < pieces[i].length; j++){
YMult = 6 - j;
//Draw piece in relevant colour
if (pieces[i][j] === "f"){
stroke(31, 46, 255);
fill(31, 46, 255);
square((i*100)+1, (YMult*100), 99);
}
else if (pieces[i][j] === "s"){
stroke(255, 54, 54);
fill(255, 54, 54);
square((i*100)+1, (YMult*100), 99);
}
}
}
}
//Function to run when the mouse is clicked
function mouseClicked(){
//Let player place token
//Check if its either player's turn
let isPlaced = false;
if (isPlayersTurn && !gameFinished){
//Check if in boundaries of top selection section
if (mouseY > 0 && mouseY < 100 && mouseX > 0 && mouseX < 702){
//Check if in first selection section
if (mouseX < 101){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[0][i] === null){
pieces[0][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in second selection section
else if (mouseX < 201){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[1][i] === null){
pieces[1][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in third selection section
else if (mouseX < 301){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[2][i] === null){
pieces[2][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in fourth selection section
else if (mouseX < 401){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[3][i] === null){
pieces[3][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in fifth selection section
else if (mouseX < 501){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[4][i] === null){
pieces[4][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in sixth selection section
else if (mouseX < 601){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[5][i] === null){
pieces[5][i] = assignLetter;
isPlaced = true;
}
}
}
//Check if in third selection section
else if (mouseX < 702){
//Loop through list, see if any free spaces
for (i = 0; i < 6; i++){
//If piece not placed and place empty, place piece
if (!isPlaced && pieces[6][i] === null){
pieces[6][i] = assignLetter;
isPlaced = true;
}
}
}
}
}
//If player has placed piece, make it so it switches which player's turn it is
if (isPlaced){
//Draw updated board
drawPieces();
//Switch players turn
if (isPlayersTurn){
isPlayersTurn = false;
isCPUsTurn = true;
assignLetter = "s";
}
else if (isCPUsTurn){
isPlayersTurn = true;
isCPUsTurn = false;
assignLetter = "f";
}
}
}
//Function to check for a winner
function checkWinner(){
//Four is the boolean as to whether a four-in-a-row is found
let four = false;
//Loop through x positions
for (i = 0; i < pieces.length; i++){
//Loop through y positions
for (j = 0; j < pieces[i].length; j++){
//Check if 1st player has a four
if (pieces[i][j] === "f" && checkAllDirections(i, j, "f")){
four = true;
winner = "P1";
}
//Check if 2nd player has a four
else if (pieces[i][j] === "s" && checkAllDirections(i, j, "s")){
four = true;
winner = "P2";
}
}
}
//If someone has won
if (four){
textSize(32);
stroke(0);
fill(0);
text("Winner: " + winner + "!", 10, 50);
gameFinished = true;
}
}
//Function to check if a four-in-a-row has occurred in any direction from the current piece
//Since I scan top-left to bottom-right, vertically, only four directions need to be checked
function checkAllDirections(x, y, checkChar){
let four = false;
//Check all directions
if (checkDirection("r", x, y, checkChar) || checkDirection("br", x, y, checkChar) || checkDirection("d", x, y, checkChar) || checkDirection("bl", x, y, checkChar)){
four = true;
}
return four;
}
//Function to check if a four-in-a-row has occurred in a given direction from the current piece
function checkDirection(direction, startX, startY, checkChar){
//If connected == 3 then there is a four-in-a-row
let connected = 0;
let tried = 0;
//Checking right
if (direction === "r"){
let x = startX;
//Keep trying until 3 in line have been checked
while (tried <= 2 && x < 6){
//Try next position to the right
x++;
//If piece is there, increment number found
if (pieces[x][startY] === checkChar){
connected++;
}
tried++;
}
}
//Checking diagonally down right
else if (direction === "br"){
let x = startX;
let y = startY;
//Keep trying until 3 in line have been checked
while (tried <= 2 && x < 6 && y < 5){
//Try next x position & y (i.e. diagonally down right)
x++;
y++;
//If piece is there, increment number found
if (pieces[x][y] === checkChar){
connected++;
}
tried++;
}
}
//Checking down
else if (direction === "d"){
let y = startY;
//Keep trying until 3 in line have been checked
while (tried <= 2 && y < 5){
//Try next position down
y++;
//If piece is there, increment number found
if (pieces[startX][y] === checkChar){
connected++;
}
tried++;
}
}
//Checking diagonally down left
else if (direction === "bl"){
let x = startX;
let y = startY;
//Keep trying until 3 in line have been checked
while (tried <= 2 && x > 0 && y < 5){
//Try next x position & y (i.e. diagonally down left)
x--;
y++;
//If piece is there, increment number found
if (pieces[x][y] === checkChar){
connected++;
}
tried++;
}
}
//If four in a row, return true
if (connected === 3){
return true;
}
else{
return false;
}
}