-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.js
210 lines (153 loc) · 5.36 KB
/
tictactoe.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
// Tic-Tac-Toe Game
// Frederick Abi Chahine
// ###########################################
// Declaring / Initializing variables
// ###########################################
let win_state_matrix = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[6, 4, 2]
]
let game_board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
const sections = document.querySelectorAll(".section")
let restart
const human_player = "red_coin"
const ai_player = "yellow_coin"
// ###########################################
// Main function
// ###########################################
const main = () => {
restart = document.getElementById("restart")
restart.onclick = clickRestart
for(let i = 0; i<sections.length; i++){
sections[i].addEventListener("click", checkSection, false)
}
}
// ###########################################
// Other functions
// ###########################################
const checkSection = (section) => {
if (typeof(game_board[section.target.id]) == "number"){
clickSection(section.target.id, human_player)
if (!(checkIfTie()) && !(checkIfWin(game_board, human_player))){
clickSection(calculateBestMove(), ai_player)
}
}
}
// -------------------------------------------
const clickSection = (section, player) => {
game_board[section] = player
const section_id = document.getElementById(section)
if (player == "red_coin"){
section_id.className += " red-coin"
}
else if (player == "yellow_coin"){
section_id.className += " yellow-coin"
}
let win = checkIfWin(game_board, player)
if (win){
endGame(win)
}
}
// -------------------------------------------
const checkIfEmpty = () => { //this function filters the array by creating a new one and returns it without the positions equal to true
return game_board.filter(x => typeof x == "number")
}
// -------------------------------------------
const checkIfTie = () => {
if (checkIfEmpty().length == 0){
for(let i = 0; i<sections.length; i++){
sections[i].removeEventListener("click", checkSection, false)
}
document.getElementById("display-message").innerText = "It is a Tie!"
return true
}
return false
}
// -------------------------------------------
const checkIfWin = (game_matrix, player) => {
let moves = game_matrix.reduce((s, t, w) => (t == player) ? s.concat(w) : s, [])
let win = null
for (let [i, j] of win_state_matrix.entries()) {
if (j.every(x => moves.indexOf(x) > -1)) {
win = { index: i, player: player }
break
}
}
return win
}
// -------------------------------------------
const endGame = (win) => {
for(let index of win_state_matrix[win.index]){
document.getElementById(index).style.backgroundColor = win.player == human_player ? "#00AEEA":"#00AEEA"
}
for(let i = 0; i<sections.length; i++){
sections[i].removeEventListener("click", checkSection, false)
}
document.getElementById("display-message").innerText = win.player == human_player ? "You Win!":"You lost! :("
}
// -------------------------------------------
const calculateBestMove = () => { //minimax will return the best position and this function will get the index of it
return minimax(game_board, ai_player).index
}
// -------------------------------------------
const minimax = (game_matrix, player) => {
let store_availability = checkIfEmpty()
if (checkIfWin(game_matrix, human_player)){
return {score:-10}
}
else if (checkIfWin(game_matrix, ai_player)){
return {score:+10}
}
else if (store_availability.length == 0){
return {score:0}
}
let progress = []
for(let i = 0; i<store_availability.length; i++){
let single_progression = {}
single_progression.index = game_matrix[store_availability[i]]
game_matrix[store_availability[i]] = player //using it like a temp variable
if (player == ai_player){
let outcome = minimax(game_matrix, human_player)
single_progression.score = outcome.score // to keep track of score to see if you are winning or losing
}
else if (player == human_player){
let outcome = minimax(game_matrix, ai_player)
single_progression.score = outcome.score
}
game_matrix[store_availability[i]] = single_progression.index
progress.push(single_progression) //adding to the progress array
}
let best_index
if (player == ai_player){
let ultimate_score = -Infinity
for(let i = 0; i<progress.length; i++){
if (progress[i].score > ultimate_score){
ultimate_score = progress[i].score
best_index = i
}
}
}
else if (player == human_player){
let ultimate_score = Infinity
for(let i = 0; i<progress.length; i++){
if (progress[i].score < ultimate_score){
ultimate_score = progress[i].score
best_index = i
}
}
}
return progress[best_index]
}
// -------------------------------------------
const clickRestart = () => {
document.location.reload(true)
}
// -------------------------------------------
main()
//end