diff --git a/status-checker.js b/status-checker.js index 7df4962..a249b7d 100644 --- a/status-checker.js +++ b/status-checker.js @@ -1,4 +1,4 @@ -import { checkIfNoMovesLeft } from './board-printer.js'; +import { checkIfNoMovesLeft } from "./board-printer.js"; /* Example board: @@ -9,6 +9,15 @@ import { checkIfNoMovesLeft } from './board-printer.js'; ]; */ +let board = [ + ["X", "_", "_"], + ["_", "X", "_"], + ["O", "O", "X"], +]; + +let player = "X"; +let row = 0; + /* Given 3 parameters: - a tic-tac-toe board (array of arrays) @@ -18,6 +27,13 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { + if ( + board[rowNumber][0] === player && + board[rowNumber][1] === player && + board[rowNumber][2] + ) { + return true; + } else return false; } /* @@ -29,6 +45,13 @@ function checkRow(board, player, rowNumber) { Otherwise, return false */ function checkColumn(board, player, columnNumber) { + if ( + board[columnNumber][0] === player && + board[columnNumber][1] === player && + board[columnNumber][2] + ) { + return true; + } else return false; } /* @@ -39,43 +62,57 @@ function checkColumn(board, player, columnNumber) { Otherwise, return false */ function checkDiagonal(board, player) { - // It may be easier to use an if statement than a loop here + // It may be easier to use an if statement than a loop here + if ( + board[0][0] === player && + board[1][1] === player && + board[2][2] === player + ) + return true; + else if ( + board[0][2] === player && + board[1][1] === player && + board[2][0] === player + ) + return true; + else return false; } +console.log(checkDiagonal(board, player)); /* There is no need to change any code below this line. */ function checkIfPlayerWon(board, player) { - for(let i = 0; i <= 2; i++) { - if(checkRow(board, player, i) || checkColumn(board, player, i)) { - return true; - } + for (let i = 0; i <= 2; i++) { + if (checkRow(board, player, i) || checkColumn(board, player, i)) { + return true; } + } - if(checkDiagonal(board, player)) { - return true; - } + if (checkDiagonal(board, player)) { + return true; + } - return false; + return false; } export function isGameOver(board) { - if(checkIfPlayerWon(board, 'X')) { - console.log('X has won the game!\n'); - return true; - } + if (checkIfPlayerWon(board, "X")) { + console.log("X has won the game!\n"); + return true; + } - if(checkIfPlayerWon(board, 'O')) { - console.log('O has won the game!\n'); - return true; - } + if (checkIfPlayerWon(board, "O")) { + console.log("O has won the game!\n"); + return true; + } - if(checkIfNoMovesLeft(board)) { - console.log('Game Over - It\s a tie!\n'); - return true; - } + if (checkIfNoMovesLeft(board)) { + console.log("Game Over - Its a tie!\n"); + return true; + } - return false; + return false; }