-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.js
181 lines (167 loc) · 5.39 KB
/
Bot.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
var board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
// 0 is empty 1 is a cross and 2 is a nought
var playersSide = 1;
var aisSide = 0;
var turn = 0;
// 0 is crosses and 1 is noughts
function printBoard() {
var square = 0;
var line1 = [];
var line2 = [];
var line3 = [];
for (square = 0; square < 9; square++) {
if (board[square] === 0) {
if (square < 3) {
line1.push('.');
} else if (square < 6) {
line2.push('.');
} else if (square < 9) {
line3.push('.');
}
} else if (board[square] === 1) {
if (square < 3) {
line1.push('x');
} else if (square < 6) {
line2.push('x');
} else if (square < 9) {
line3.push('x');
}
} else if (board[square] === 2) {
if (square < 3) {
line1.push('o');
} else if (square < 6) {
line2.push('o');
} else if (square < 9) {
line3.push('o');
}
}
}
console.log(line1[0] + '|' + line1[1] + '|' + line1[2]);
console.log('-+-+-');
console.log(line2[0] + '|' + line2[1] + '|' + line2[2]);
console.log('-+-+-');
console.log(line3[0] + '|' + line3[1] + '|' + line3[2]);
console.log("\n")
}
function resetBoard() {
board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
}
function placeCross(square) {
board[square] = 1;
turn = 1;
}
function placeNought(square) {
board[square] = 2;
turn = 0;
}
function emptySquare(square, lastTurn) {
board[square] = 0;
turn = lastTurn;
}
function getMoves() {
var moves = [];
for (var i = 0; i < 9; i++) {
if (board[i] === 0) {
moves.push(i);
}
}
return moves;
}
function isPlayersTurn() {
if (turn == playersTurn) return true;
return false;
}
function getBestMove() {
var moves = getMoves();
var bestScore = -999999
var bestMove;
for (var i = 0; i < moves.length; i++) {// Do each move
if(playersSide === 0) {
placeNought(moves[i]);
} else {
placeCross(moves[i]);
}
// Returns negative score because the original function is based off crosses, which is positive
var score = (aisSide === 0) ? -negamax(1) :negamax(-1);
console.log(score);
emptySquare(moves[i], aisSide);
if (score > bestScore) { // If there is a better move, do that instead
bestScore = score;
bestMove = moves[i];
}
}
return bestMove;
}
function negamax(colour) {
var moves = getMoves();
// If someone was won, get the score
if (moves.length === 0 ||
(board[0] == 1 && board[1] == 1 && board[2] == 1) ||
(board[3] == 1 && board[4] == 1 && board[5] == 1) ||
(board[6] == 1 && board[7] == 1 && board[8] == 1) ||
(board[0] == 1 && board[3] == 1 && board[6] == 1) ||
(board[1] == 1 && board[4] == 1 && board[7] == 1) ||
(board[2] == 1 && board[5] == 1 && board[6] == 1) ||
(board[0] == 1 && board[4] == 1 && board[8] == 1) ||
(board[2] == 1 && board[4] == 1 && board[6] == 1) ||
(board[0] == 2 && board[1] == 2 && board[2] == 2) ||
(board[3] == 2 && board[4] == 2 && board[5] == 2) ||
(board[6] == 2 && board[7] == 2 && board[8] == 2) ||
(board[0] == 2 && board[3] == 2 && board[6] == 2) ||
(board[1] == 2 && board[4] == 2 && board[7] == 2) ||
(board[2] == 2 && board[5] == 2 && board[6] == 2) ||
(board[0] == 2 && board[4] == 2 && board[8] == 2) ||
(board[2] == 2 && board[4] == 2 && board[6] == 2)) {
return getScore() * colour; // Multiply by the side so that it gives a score relative to the player
}
var bestScore = -999999;
// Does each possible move and checks what the other player would do in that situation
for (var i = 0; i < moves.length; i++) {
if ((aisSide === 0 && colour == -1) || (aisSide === 1 && colour === 1)) {
if (playersSide === 0) {
placeCross(moves[i]);
} else {
placeNought(moves[i])
}
} else {
if (playersSide === 0) {
placeNought(moves[i]);
} else {
placeCross(moves[i]);
}
}
var score = -negamax(-colour);
if ((aisSide === 0 && colour == -1) || (aisSide === 1 && colour === 1)) {
emptySquare(moves[i], playersSide);
} else {
emptySquare(moves[i], aisSide);
} // If this score was better than the last, update it
if (score > bestScore) bestScore = score;
}
return bestScore;
}
function getScore() { // if they win, give them 10 points, if they loose, give them -10, else give 0
if ((board[0] == 1 && board[1] == 1 && board[2] == 1) ||
(board[3] == 1 && board[4] == 1 && board[5] == 1) ||
(board[6] == 1 && board[7] == 1 && board[8] == 1) ||
(board[0] == 1 && board[3] == 1 && board[6] == 1) ||
(board[1] == 1 && board[4] == 1 && board[7] == 1) ||
(board[2] == 1 && board[5] == 1 && board[6] == 1) ||
(board[0] == 1 && board[4] == 1 && board[8] == 1) ||
(board[2] == 1 && board[4] == 1 && board[6] == 1)) {
return -10;
} else if ((board[0] == 2 && board[1] == 2 && board[2] == 2) ||
(board[3] == 2 && board[4] == 2 && board[5] == 2) ||
(board[6] == 2 && board[7] == 2 && board[8] == 2) ||
(board[0] == 2 && board[3] == 2 && board[6] == 2) ||
(board[1] == 2 && board[4] == 2 && board[7] == 2) ||
(board[2] == 2 && board[5] == 2 && board[6] == 2) ||
(board[0] == 2 && board[4] == 2 && board[8] == 2) ||
(board[2] == 2 && board[4] == 2 && board[6] == 2)) {
return 10;
} else if (board[0] != 0 && board[1] != 0 && board[2] != 0 && board[3] != 0 && board[4] != 0 && board[5] != 0 && board[6] != 0 && board[7] != 0 && board[8] != 0) {
return 0;
}
}
placeCross(getBestMove());
printBoard();