function ticTacToe() {
function randomAI(thisBoard, currPlay) {
let aiMove = [];
let checkValid = false;
let aiXcoord = Math.floor(Math.random() * 3);
aiMove.push(aiXcoord);
let aiYcoord = Math.floor(Math.random() * 3);
aiMove.push(aiYcoord);
while (checkValid === false){
if (thisBoard[aiMove[0]][aiMove[1]] == `X` || thisBoard[aiMove[0]][aiMove[1]] == `O`) {
aiMove = randomAI(newBoard, currentPlayer);
} else {
checkValid=true
}
}
return aiMove;
}
let playerOne;
let playerTwo;
let currentPlayer;
let player = playerOne;
function vsHuman () {
playerOne=prompt('Player One, enter your name: ');
playerTwo = prompt('Player Two, enter your name: ');
player= playerOne
console.log(``);
console.log(` 0 1 2 `);
console.log(` _______`);
console.log(`0| , , |`);
console.log(`1| , , |`);
console.log(`2| , , |`);
console.log(` _______`);
}
const makeBoard = () => {
return [[` `, ` `, ` `], [` `, ` `, ` `], [` `, ` `, ` `]];
}
let newBoard = makeBoard();
alert(`Welcome to Tic-Tac-Toe by Tjolanda Sullivan, Sung Y Kim, and Miguel Manalo`)
let gameType = confirm(`Do you want to play against the CPU?`)
if (gameType === true) {
playerOne = prompt('Player One, enter your name: ');
playerTwo = "CPU is O:";
player = playerOne
console.log(``);
console.log(` 0 1 2 `);
console.log(` _______`);
console.log(`0| , , |`);
console.log(`1| , , |`);
console.log(`2| , , |`);
console.log(` _______`);
} else {
vsHuman();
}
const coolBoard = (board, coord) => {
let tempArray = board;
tempArray[coord[0]][coord[1]] = currentPlayer;
console.log(` 0 1 2 `);
console.log(` _______`);
console.log(`0| ${tempArray[0]} |`);
console.log(`1| ${tempArray[1]} |`);
console.log(`2| ${tempArray[2]} |`);
console.log(` _______`);
console.log(``);
return tempArray;
}
function getMove() {
let playerMove = [];
let playerXcoord = null;
let playerYcoord = null;
alert(`\n${player}, it's your turn now.\nYou are ${currentPlayer}\n`);
function xMove() {
playerXcoord = prompt(`${player}, enter your Y-coordinate of 0, 1, or 2`);
while (playerXcoord != 0 || playerXcoord != 1 || playerXcoord != 2 || playerXcoord == null){
switch (playerXcoord) {
case '0':
case '1':
case '2':
case 0:
case 1:
case 2:
return playerXcoord;
default:
console.log(playerXcoord, `playerYcoord`)
console.log(`Invalid move. Please enter 0, 1, or 2`);
playerXcoord = null;
playerXcoord = prompt(
`${player} enter your Y-coordinate of 0, 1, or 2`);
}
}
}
function yMove() {
playerYcoord = prompt(`${player}, enter your X-coordinate of 0, 1, or 2`);
while (playerYcoord != 0 || playerYcoord != 1 || playerYcoord != 2 || playerYcoord == null){
switch (playerYcoord) {
case '0':
case '1':
case '2':
case 0:
case 1:
case 2:
return playerYcoord;
default:
console.log(playerYcoord, `playerXcoord`)
console.log(`Invalid move. Please enter 0, 1, or 2`);
playerYcoord = null;
playerYcoord = prompt(
`${player} enter your X-coordinate of 0, 1, or 2`);
}
}
}
xMove();
playerMove.push(Number(playerXcoord));
console.log(playerMove, `is your Y-coordinate`);
yMove();
playerMove.push(Number(playerYcoord));
console.log(`Your move is `, playerMove, `\n`);
return playerMove;
}
function isValidMove(currBoard, currMove) {
let checkValid=false
while (checkValid===false){
if (currBoard[currMove[0]][currMove[1]] == `X` || currBoard[currMove[0]][currMove[1]] == `O`) {
console.log(`Invalid move, please enter a new move`);
currMove=getMove();
} else {
checkValid=true
}
}
return currMove;
}
function checkResult (result, playerName) {
let userPlay;
if (result === true) {
console.log(`\nHooray! ${playerName} won the game!`);
console.log(``);
console.log(`Thank you for playing! \nThis version of Tic-Tac-Toe was brought to you by: \nTjolanda Sullivan, Sung Y Kim, Miguel Manalo \n\n░░░░░░░░░░░░▄▄░░░░░░░░░
░░░░░░░░░░░█░░█░░░░░░░░
░░░░░░░░░░░█░░█░░░░░░░░
░░░░░░░░░░█░░░█░░░░░░░░
░░░░░░░░░█░░░░█░░░░░░░░
███████▄▄█░░░░░██████▄░░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█████░░░░░░░░░█░░
██████▀░░░░▀▀██████▀░░░░\n\n
idea: robertheaton.com/2018/10/09/programming-pr… \nASCII art: emojiforu.com/text-art/ascii-thumbs-up-tex…\n`)
userPlay = confirm(`Do you want to play again?`);
return userPlay;
}
else if (result === `Draw`) {
console.log(`It's a tie. Thanks for playing! \nThis version of Tic-Tac-Toe was brought to you by: \nTjolanda Sullivan, Sung Y Kim, Miguel Manalo \n\nidea: robertheaton.com/2018/10/09/programming-pr… t/\n`);
userPlay = confirm(`Do you want to play again?`);
return userPlay;
}
}
const check_winner = (board) => {
let resultList = {
var1: 'OOO',
var2: 'XXX'
}
for(let i = 0; i < board.length; i++) {
let tempString = board[i][0] + board[i][1] + board[i][2];
if(tempString === resultList.var1 && board[i][0] === currentPlayer) {
return true;
} else if(tempString === resultList.var2 && board[i][0] === currentPlayer) {
return true;
} else {}
}
for(let j = 0; j < board.length; j++) {
let tempString = board[0][j] + board[1][j] + board[2][j];
if(tempString === resultList.var1 && board[0][j] === currentPlayer) {
return true;
} else if(tempString === resultList.var2 && board[0][j] === currentPlayer) {
return true;
} else {}
}
let diaResult1 = board[0][0] + board[1][1] + board[2][2];
let diaResult2 = board[2][0] + board[1][1] + board[0][2];
if(diaResult1 === resultList.var1 && board[1][1] === currentPlayer) {
return true;
} else if(diaResult1 === resultList.var2 && board[1][1] === currentPlayer){
return true;
} else {}
if(diaResult2 === resultList.var1 && board[1][1] === currentPlayer) {
return true;
} else if(diaResult2 === resultList.var2 && board[1][1] === currentPlayer){
return true;
} else {}
let valueCounter = 0;
for(let k = 0; k < board.length; k++) {
for(let l = 0; l < board.length; l++) {
if(board[k][l] !== ` `) {
valueCounter++;
}
}
}
if(valueCounter === 9) {
return 'Draw';
}
return false;
}
let checkMove;
let nowMove;
const makeMove = (prevBoard, playMove)=> {
let playerInput = coolBoard(prevBoard, playMove);
return playerInput;
}
for (let i = 1; i < 10; i += 1) {
let result;
if (i % 2 === 0) {
currentPlayer = 'O';
if (gameType === true) {
checkMove = randomAI(newBoard, currentPlayer);
} else {
checkMove = getMove();
}
nowMove = isValidMove(newBoard, checkMove);
newBoard = makeMove(newBoard, nowMove);
result = check_winner(newBoard);
let decision = checkResult(result, player);
player = playerOne;
if (decision == true) {
ticTacToe();
break;
} else if (decision == false) {
console.log(`Thanks again! Goodbye!`)
break;
}
} else {
currentPlayer = 'X';
checkMove = getMove();
nowMove = isValidMove(newBoard, checkMove);
newBoard = makeMove(newBoard, nowMove);
result = check_winner(newBoard);
let decision = checkResult(result, player);
player = playerTwo;
if (decision == true) {
ticTacToe();
break;
} else if (decision == false) {
console.log(`Thanks again! Goodbye!`)
break;
}
}
}
}
ticTacToe();