-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
161 lines (147 loc) · 4.54 KB
/
main.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
var huPlayer, aiPlayer, game;
var origGrid = Array.from(Array(9).keys());
function playerX() {
huPlayer = "X";
aiPlayer = "O";
document.turn = huPlayer;
game = true;
removeStartMenu();
};
function playerO() {
huPlayer = "O";
aiPlayer = "X";
document.turn = huPlayer;
game = true;
removeStartMenu();
};
function getCell(num) {
return document.getElementById("cell-"+num).innerText;
}
function gamePlay(cell) {
if (document.turn == huPlayer && game) {
if (cell.innerText == "") {
cell.innerText = huPlayer;
} else {
return
}
var i = cell.id;
i = i.split("-");
i = i[1];
origGrid[i] = huPlayer;
if (checkWinner(origGrid, huPlayer)) {
gameAlert("Congratulations", "Player " + huPlayer + " won!");
game = false;
} else if (getAvailSpots(origGrid).length == 0) {
gameAlert("Draw", "Nobody wins!");
game = false;
}
document.turn = aiPlayer;
}
if (document.turn == aiPlayer && game) {
moveAI(origGrid);
if (checkWinner(origGrid, aiPlayer)) {
gameAlert("Congratulations", "Player " + aiPlayer + " won!");
game = false;
} else if (getAvailSpots(origGrid).length == 0) {
gameAlert("Draw", "Nobody wins!");
game = false;
}
document.turn = huPlayer;
}
}
function moveAI(origGrid) {
value = minimax(origGrid, aiPlayer).index
document.getElementById("cell-"+value).innerText = aiPlayer;
origGrid[value] = aiPlayer;
return
}
function minimax(newGrid, player) {
var availableSpots = getAvailSpots(newGrid);
if (checkWinner(newGrid, huPlayer)) {
return {score: -10};
} else if (checkWinner(newGrid, aiPlayer)) {
return {score: 10};
} else if (availableSpots.length === 0) {
return {score: 0};
}
var moves = [];
for (var i = 0; i < availableSpots.length; i++) {
var move = {};
move.index = newGrid[availableSpots[i]];
newGrid[availableSpots[i]] = player;
if (player == aiPlayer) {
var result = minimax(newGrid, huPlayer);
move.score = result.score;
} else {
var result = minimax(newGrid, aiPlayer);
move.score = result.score;
}
newGrid[availableSpots[i]] = move.index;
moves.push(move);
}
var bestMove;
if(player === aiPlayer) {
var bestScore = -10000;
for(var i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
var bestScore = 10000;
for(var i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
function getAvailSpots() {
return origGrid.filter(s => typeof s == 'number');
}
function checkWinner(grid, player) {
result = null;
// check horizontal
if ((grid[0] == player && grid[1] == player && grid[2] == player) ||
(grid[3] == player && grid[4] == player && grid[5] == player) ||
(grid[6] == player && grid[7] == player && grid[8] == player)) {
result = true;
}
// check vertical
if ((grid[0] == player && grid[3] == player && grid[6] == player) ||
(grid[1] == player && grid[4] == player && grid[7] == player) ||
(grid[2] == player && grid[5] == player && grid[8] == player)) {
result = true;
}
// check diagonal
if ((grid[0] == player && grid[4] == player && grid[8] == player) ||
(grid[6] == player && grid[4] == player && grid[2] == player)) {
result = true;
}
return result;
}
function gameAlert(header, message) {
var messageoverlay = document.getElementById("messageoverlay");
var messagebox = document.getElementById("messagebox");
messageoverlay.style.display = "block";
messagebox.style.left = (window.innerWidth/2) - (550 * .5)+"px";
messagebox.style.top = "80px";
messagebox.style.display = "block";
document.getElementById("messageboxhead").innerHTML = header;
document.getElementById("messageboxbody").innerHTML = message;
document.getElementById("messageboxfoot").innerHTML = '<button onclick="reset()">Start Over</button>';
}
function reset() {
location.reload();
document.getElementById("messagebox").style.display = "none";
document.getElementById("messageoverlay").style.display = "none";
game = true;
}
function removeStartMenu() {
document.getElementById("start-menu").style.display = 'none';
document.getElementById("gameBoard").style.display = 'table';
document.getElementById("title").style.padding = "30px 30px 0px 30px";
}