-
Notifications
You must be signed in to change notification settings - Fork 30
/
tictactoe.js
356 lines (307 loc) · 10.8 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* Play Tic Toe, against a friend or the computer.
If u choose to play against the computer, the 'Computer Player' will use the getBestMove function to choose one of the best possible moves.
*/
// -- Constants --
const SYMBOLS = {
x:'X',
o:'O'
}
const RESULT = {
incomplete: 0,
playerXWon: SYMBOLS.x,
playerOWon: SYMBOLS.o,
tie: 3
}
const VIEW = {
question1: 1,
question2: 2,
game: 3,
result: 4
}
function Board (options){
// Creates the board Object for the game
// -- Data Stracture --
state = {
view: VIEW.question1,
players: [
{
symbol: null,
isComputer: false,
score: 0
},
{
symbol: null,
isComputer: false,
score: 0
}
]
}
function initGame(){
state.game= {
_gameBoard: [
["", "", ""],
["", "", ""],
["", "", ""]
],
turn: Math.round(Math.random()), //we set this var randomly for the first move.
}
}
function moveCount(board){
//receives a board and returns the number of moves that have been played.
let moveCount = 0
for (let i = 0; i<board.length; i++){
for (let j = 0 ; j<board[i].length ; j++){
if (board[i][j]!=""){
moveCount++
}
}
}
return moveCount
}
function getResult(board,symbol){
// receives a board, and the symbol of the player and returns an object with the result and an array of the winning line
let result = RESULT.incomplete
if (moveCount(board)<5){
return {result}
}
function succession (line){
return (line === symbol.repeat(3))
}
let line
let winningLine=[]
//first we check row, then column, then diagonal
for (var i = 0 ; i<3 ; i++){
line = board[i].join('')
if(succession(line)){
result = symbol;
winningLine = [[i,0], [i,1], [i,2]]
return {result, winningLine};
}
}
for (var j=0 ; j<3; j++){
let column = [board[0][j],board[1][j],board[2][j]]
line = column.join('')
if(succession(line)){
result = symbol
winningLine = [[0,j], [1,j], [2,j]]
return {result, winningLine};
}
}
let diag1 = [board[0][0],board[1][1],board[2][2]]
line = diag1.join('')
if(succession(line)){
result = symbol
winningLine = [[0,0], [1,1], [2,2]]
return {result, winningLine};
}
let diag2 = [board[0][2],board[1][1],board[2][0]]
line = diag2.join('')
if(succession(line)){
result = symbol
winningLine = [[0,2], [1,1], [2,0]]
return {result, winningLine};
}
//Check for tie
if (moveCount(board)==9){
result=RESULT.tie
return {result, winningLine}
}
return {result}
}
function getBestMove (board, symbol){
// Receives a board, and the symbol of the player who has the next move. Returns the cordinates of the move and a score for that move (1-for winning, 0 for tie, and -1 for losing)
function copyBoard(board) {
let copy = []
for (let row = 0 ; row<3 ; row++){
copy.push([])
for (let column = 0 ; column<3 ; column++){
copy[row][column] = board[row][column]
}
}
return copy
}
function getAvailableMoves (board) {
// Receives a board, and returns an array of available moves.
let availableMoves = []
for (let row = 0 ; row<3 ; row++){
for (let column = 0 ; column<3 ; column++){
if (board[row][column]===""){
availableMoves.push({row, column})
}
}
}
return availableMoves
}
function shuffleArray (array){
// shuffles the array in place
for (var i = array.length - 1; i > 0; i--) {
var rand = Math.floor(Math.random() * (i + 1));
[array[i], array[rand]]=[array[rand], array[i]]
}
}
let availableMoves = getAvailableMoves(board)
let availableMovesAndScores = []
for (var i=0 ; i<availableMoves.length ; i++){
// Iterates over each available move. If it finds a winning move it returns it immediately. Otherwise it pushes a move and a score to the availableMovesAndScores array.
let move = availableMoves[i]
let newBoard = copyBoard(board)
newBoard = applyMove(newBoard,move, symbol)
result = getResult(newBoard,symbol).result
let score
if (result == RESULT.tie) {score = 0}
else if (result == symbol) {
score = 1
}
else {
let otherSymbol = (symbol==SYMBOLS.x)? SYMBOLS.o : SYMBOLS.x
nextMove = getBestMove(newBoard, otherSymbol)
score = - (nextMove.score)
}
if(score === 1)
return {move, score}
availableMovesAndScores.push({move, score})
}
shuffleArray(availableMovesAndScores)
availableMovesAndScores.sort((moveA, moveB )=>{
return moveB.score - moveA.score
})
return availableMovesAndScores[0]
}
function render(){
// Renders the screen according to the state.
function getPlayerName(playerSymbol){
if(playerSymbol === state.players[0].symbol)
return state.players[0].isComputer ? 'Computer' : "Player1"
return state.players[1].isComputer ? 'Computer' : "Player2"
}
function buttonHTML(btnGroup, data, text){
return `<button type="button" class="btn btn-lg btn-default btnGroup${btnGroup}" data=${data}>${text}</button>`
}
function htmlSpaces (times){
return ' '.repeat(times)
}
function htmlQ1(){
return `<div id="view1"><h3>Which do you prefer?\n</h3>
${buttonHTML(1, "1player", "Man Against computer")}
${buttonHTML(1, "2players", "Man Against Man")}
</div>`
}
function htmlQ2(){
const html2=`<div id="view2"><h3>${!state.players[1].isComputer? "Player 1, <br />" : ""}Which symbols would you like to use?</h3>
${buttonHTML(2, "X", "X")}
${buttonHTML(2, "O", "O")}`
return html2
}
function htmlGame (){
const moveNumber = moveCount(state.game._gameBoard) + 1
const playerName = state.game.turn === 0 ? 'Player1' : state.players[1].isComputer ? 'Computer' : 'Player2'
// let playerName = 'Computer'
// if(!state.players[state.game.turn].isComputer)
// playerName = state.game.turn === 0 ? 'Player1' : 'Player2'
let htmlBefore = `<h3>move: ${moveNumber} ${htmlSpaces(5)} turn: ${playerName}</h3>`
let board = state.game._gameBoard.reduce(function(acc,curr,rowIndex){
return acc + `<div id= "row${rowIndex}" class="row">${curr.map((str,colIndex)=>`<div class="cell col${colIndex}" data-row=${rowIndex} data-column=${colIndex}>${str}</div>`).join('')}</div>`
}, ``)
let htmlAfter = `<h4>Score: ${htmlSpaces(1)} Player 1 - ${state.players[0].score} ${htmlSpaces(2)} ${state.players[1].isComputer? "Computer" : "Player 2" } - ${state.players[1].score}</h4>`
return `<div id='gameView'> ${htmlBefore} <div id="board">${board}</div> ${htmlAfter} </div>`
}
function htmlGameEnd (){
function arraysAreEqual (arr1, arr2){
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
}
let {result, winningLine} = getResult(state.game._gameBoard, state.players[state.game.turn].symbol )
let resultText = "tie"
if(result !== RESULT.tie)
resultText = getPlayerName(result) + " Won"
let htmlBefore = `<h3>${resultText} ${htmlSpaces(2)} Click to restart </h3> `
let board = state.game._gameBoard.reduce(function(acc,curr,rowIndex){
return acc + `<div id="row${rowIndex}" class="row">${curr.map(
(str,colIndex)=>
`<div class="cell col${colIndex} ${winningLine.some(arr=>(arraysAreEqual(arr,[rowIndex,colIndex]))) ? "winningLine" : ""}"
data-row=${rowIndex} data-column=${colIndex}>${str}</div>`).join('')}</div>`
}, ``)
let htmlAfter = `<h4>Score: ${htmlSpaces(1)} Player 1 - ${state.players[0].score} ${htmlSpaces(2)} ${state.players[1].isComputer? "Computer" : "Player 2" } - ${state.players[1].score}</h4>`
return `<div id='resultView'> ${htmlBefore} <div id="board">${board}</id> ${htmlAfter} </div>`
}
let html = ''
if (state.view == VIEW.question1) {html = htmlQ1()}
else if (state.view == VIEW.question2) {html = htmlQ2()}
else if (state.view == VIEW.result) {html=htmlGameEnd()}
else {html=htmlGame()}
// console.log(html)
options.el.innerHTML = html
}
function question1Handler (ev){
state.players[1].isComputer = !($(ev.currentTarget).attr('data')==="2players")
state.view = VIEW.question2
render()
}
function question2Handler (ev){
let player1Symbol = $(ev.currentTarget).attr('data')
state.players[0].symbol=player1Symbol;
state.players[1].symbol=(player1Symbol===SYMBOLS.x)? SYMBOLS.o: SYMBOLS.x;
state.view = VIEW.game
initGame()
if(state.players[state.game.turn].isComputer)
doComputerMove()
render()
}
function doComputerMove (){
let symbol = state.players[1].symbol
let move = getBestMove(state.game._gameBoard, symbol).move
executeTurn(state.game._gameBoard,move, symbol)
}
function playerMoveHandler (ev){
let symbol = state.players[state.game.turn].symbol
let row = parseInt($(ev.currentTarget).attr('data-row'))
let column = parseInt($(ev.currentTarget).attr('data-column'))
executeTurn(state.game._gameBoard, {row, column}, symbol)
}
function applyMove(board,move, symbol) {
board[move.row][move.column]= symbol
return board
}
function executeTurn(board, move, symbol) {
if (board[move.row][move.column]!==""){
return board
}
applyMove(board,move,symbol)
let result = getResult(board, symbol).result
if (result === RESULT.incomplete){
state.game.turn = (state.game.turn+1)%2
render()
} else {
//Increment score and show result
if(result !== RESULT.tie) {
let winningPlayer = state.players.find((player)=>{return player.symbol == result})
winningPlayer.score++
}
state.view = VIEW.result
render()
}
if (result==RESULT.incomplete && state.players[state.game.turn].isComputer){
doComputerMove()
}
}
function beginGame(){
initGame()
state.view = VIEW.game
render()
if(state.game.turn === 1 && state.players[1].isComputer)
doComputerMove();
}
$(options.el).on('click', '.btnGroup1', question1Handler)
$(options.el).on('click', '.btnGroup2', question2Handler)
$(options.el).on('click', '#gameView .cell', playerMoveHandler)
$(options.el).on('click', '#resultView', beginGame)
render ()
}
const board = new Board ({
el : document.getElementById('root')
})