-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
304 lines (277 loc) · 6.32 KB
/
script2.js
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
var winningPlays = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[6,4,2],
[0,1,2],
]
var board = [
[-1,-1,-1],
[-1,-1,-1],
[-1,-1,-1],
]
var totalMoves = 0;
var winner = false;
var playerScore = 0;
var aiScore = 0;
var blankInTwo = -1;
function reset(){
document.getElementById("playertotal").innerHTML = playerScore.toString();
document.getElementById("aitotal").innerHTML = playerScore.toString();
}
function playerGo(box){
var row = findRow(box);
var pos = findPos(box);
if(!winner){
if(board[row][pos] == -1){
board[row][pos] = "X";
document.getElementById(box).innerText = "X";
totalMoves = totalMoves + 1;
checkWin("X");
if(totalMoves < 9 && !winner){ //stops the AI from trying to make a move after the board is full
aiGo(board);
}
}
}
}
function aiGo(board){
var madeMove = false;
while(!madeMove){
if(board[1][1] == -1){ //if center is free play center
board[1][1] = "O";
document.getElementById(4).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else if(board[1][1] == "X" && totalMoves == 1){ //if user went center on frist move we take corner
var pickCorner = Math.floor(Math.random() * 4);
if(pickCorner == 0){
board[0][0] = "O";
document.getElementById(0).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else if(pickCorner == 1){
board[0][2] = "O";
document.getElementById(2).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else if(pickCorner == 2){
board[2][0] = "O";
document.getElementById(6).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else if(pickCorner == 3){
board[2][2] = "O";
document.getElementById(8).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
}
else if(checkTwoInRow("O")){ //if the ai has two in a row act to this plan
madeMove = true;
console.log("ai has 2 in row");
console.log("The free space is position: " + blankInTwo);
var row = findRow(blankInTwo);
var pos = findPos(blankInTwo);
board[row][pos] = "O";
document.getElementById(blankInTwo).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else if(checkTwoInRow("X")){ //if the user has two in a row act to this plan
madeMove = true;
console.log("User has 2 in row");
console.log("The free space is position: " + blankInTwo);
var row = findRow(blankInTwo);
var pos = findPos(blankInTwo);
board[row][pos] = "O";
document.getElementById(blankInTwo).innerText = "O";
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
else { //if all else fails then ai will make a random move
var tryPos = Math.floor(Math.random() * 9);
var row = findRow(tryPos);
var pos = findPos(tryPos);
if(board[row][pos] == -1){
board[row][pos] = "O";
document.getElementById(tryPos).innerText = "O"
totalMoves = totalMoves + 1;
madeMove = true;
checkWin("O");
}
}
}
}
function checkTwoInRow(XO){ //return true if the user has 2 "X" in a row (can be seperated by white space)
var counter = 0;
var hasTwo = false;
var hasO = false;
var blankSpace = -1
var XorO = "";
var opposite = "";
if(XO == "X"){
XorO = "X";
opposite = "O";
}
else if(XO == "O"){
XorO = "O";
opposite = "X";
}
for(let p = 0; p <= 8; p++){ //loops through each test
if(counter == 2){ //there is 2 in a row
if(hasO == false){
hasTwo = true;
blankInTwo = blankSpace;
counter = 0;
blankSpace = -1;
}
}
else {
counter = 0;
hasO = false;
blankSpace = -1;
}
for(let g = 0; g < 3; g++){ //loops through each element in specific test
var checkPos = winningPlays[p][g];
var row = findRow(checkPos);
var pos = findPos(checkPos);
if(board[row][pos] == opposite){
hasO = true;
}
else if(board[row][pos] == XorO){
counter = counter + 1;
}
else if(board[row][pos] == -1){ //the blank space
blankSpace = winningPlays[row][pos];
}
}
}
if(hasTwo){
return true;
}
else {
return false;
}
}
function checkWin(XO){
var possibleWinner = 0;
var checkRow = [];
var winningRow = [];
for(let i = 0; i <= 8; i++){ //loops through all tests
if(possibleWinner == 3){
winner = true;
winningRow = checkRow;
}
else {
possibleWinner = 0;
checkRow = [];
}
for(let j = 0; j < 3; j++){ //loops through each element in specific test
var checkPos = winningPlays[i][j];
var row = findRow(checkPos);
var pos = findPos(checkPos);
if(board[row][pos] == XO){
possibleWinner = possibleWinner + 1;
checkRow.push(checkPos);
}
}
}
if(winner){
for(let n = 0; n < 3; n++){
if(XO == "X"){
document.getElementById(winningRow[n]).style.background = "lightgreen";
}
else if(XO == "O"){
document.getElementById(winningRow[n]).style.background = "#ff6161";
}
}
if(XO == "X"){
playerScore = playerScore + 1;
updateScore("X");
}
else if(XO == "O"){
aiScore = aiScore + 1;
updateScore("O");
}
}
else if(totalMoves == 9){
for(let p = 0; p < 9; p++){
document.getElementById(p).style.background = "#ff6161";
}
}
}
function updateScore(XO){
if(XO == "X"){
document.getElementById("playertotal").innerHTML = playerScore.toString();
}
else if(XO == "O"){
document.getElementById("aitotal").innerHTML = aiScore.toString();
}
}
function newGame(){
board = [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]]
totalMoves = 0;
winner = false;
for(let p = 0; p < 9; p++){
document.getElementById(p).innerText = "";
document.getElementById(p).style.background = "white";
}
}
//////////////////////////////////////
//////Helper functions////////////////
//////////////////////////////////////
function findRow(id){ //given an id return the row 0,1,2
if(id < 3){
return 0;
}
else if(id >= 3 && id < 6){
return 1;
}
else if(id >= 6 && id < 9){
return 2;
}
}
function findPos(id) { //given an id returns the position in that row 0,1,2
if(id == 0){
return 0;
}
else if(id == 1){
return 1;
}
else if(id == 2){
return 2;
}
else if(id == 3){
return 0;
}
else if(id == 4){
return 1;
}
else if(id == 5){
return 2;
}
else if(id == 6){
return 0;
}
else if(id == 7){
return 1;
}
else if(id == 8){
return 2;
}
}