Created
May 8, 2021 07:40
-
-
Save theSundayProgrammer/c3b25ab06a8d51052eec8043a494bc68 to your computer and use it in GitHub Desktop.
The model part of the MVC code for a web page that plays tic tac toe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lines = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8], | |
[0, 3, 6], | |
[1, 4, 7], | |
[2, 5, 8], | |
[0, 4, 8], | |
[2, 4, 6], | |
]; | |
const slotStatus = { empty:0, cross:1, circle:2}; | |
function display(val){ | |
switch (val){ | |
case slotStatus.empty: return ""; | |
case slotStatus.circle: return "O"; | |
case slotStatus.cross: return "X"; | |
default: return "X"; | |
} | |
} | |
function is_player_going_to_win ( player, board){ | |
lines.findIndex((X)=> | |
{ | |
let count = 0; | |
X.forEach((x)=>{ | |
if (board[x]===player) ++count; | |
else if(board[x] !== slotStatus.empty) --count | |
}); | |
return count===2; | |
}); | |
} | |
function has_player_won ( player, board){ | |
lines.findIndex((X)=> | |
X.findIndex((x)=>board[x]!==player) === -1); | |
} | |
function fill_corner( slot, board){ | |
let corners=[0,2,6,8]; | |
//console.log("fill_corner ",slot ) | |
//TODO use corners.find | |
let corner = corners.findIndex((x)=> board[x]===slotStatus.empty); | |
if (corner>=0) | |
board[corner] = slot; | |
return corner>=0; | |
} | |
function fill_any( player, board) | |
{ | |
//ToDo: use board.findIndex | |
let i = board.findIndex((x)=>board[i]===slotStatus.empty); | |
if(i>=0) | |
board[i] = player; | |
} | |
function fill_line ( k, player, board){ | |
let i = lines[k].find((x)=>board[x] === slotStatus.empty); | |
if(i !== undefined){ | |
board[i] = player; | |
} | |
} | |
function forced_play( player, opponent, board) | |
{ | |
let k = is_player_going_to_win(player,board); | |
if (k >=0){ | |
fill_line(k,player,board); | |
return true; | |
} | |
k = is_player_going_to_win(opponent,board); | |
if (k >=0){ | |
fill_line(k,player,board); | |
return true; | |
} | |
return false; | |
} | |
function play_turn( board){ | |
let turn=0; | |
board.forEach((x)=> {if (x !==slotStatus.empty) | |
{ | |
//console.log("x=", x); | |
++turn; | |
} | |
}); | |
//console.log("turn=", turn) | |
switch (turn){ | |
case 0: | |
board[4]=slotStatus.circle; | |
break; | |
case 1: | |
if (board[4] === slotStatus.empty) { | |
board[4] = slotStatus.cross; | |
} else { | |
board[0] = slotStatus.cross; | |
} | |
break; | |
case 2: | |
{ | |
let middle = [1, 3, 5, 7]; | |
let diagnol = [8, 8, 0, 0]; | |
for (let i = 0; i < 4; ++i) { | |
if (board[middle[i]] !== slotStatus.empty) { | |
board[diagnol[i]] = slotStatus.circle; | |
return; | |
} | |
} | |
fill_corner(slotStatus.circle, board); | |
break; | |
} | |
case 3: | |
case 5: | |
case 7: | |
{ | |
if (forced_play(slotStatus.cross, slotStatus.circle, board)) | |
break; | |
else if (fill_corner(slotStatus.cross, board)) | |
break; | |
else fill_any(slotStatus.cross, board); | |
break; | |
} | |
case 4: | |
case 6: | |
case 8: | |
{ | |
if (forced_play(slotStatus.circle, slotStatus.cross, board)) | |
break; | |
else if (fill_corner(slotStatus.circle, board)) | |
break; | |
else fill_any(slotStatus.circle, board); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment