diff --git a/board-printer.js b/board-printer.js index e11a262..916a18d 100644 --- a/board-printer.js +++ b/board-printer.js @@ -15,7 +15,38 @@ ================= Test your function by calling it with an example tic-tac-toe board. */ +//It needs to be checked +let board = [ + ["X", "0", "X"], + ["X", "0", "X"], + ["0", "X", "0"], +]; +console.log(board); + +// we change 'X' into '_' in the first row and second column place (index [0][1] in array) +board[0][1] = "_"; +// we change 'X' into '_' in the first row and third column place (index [0][2] in array) +board[0][2] = "_"; +// we change 'X' into '_' in the second row and first column place (index [1][0] in array) +board[1][0] = "_"; +// we change '0' into 'X' in the second row and second column place (index [1][1] in array) +board[1][1] = "X"; +// we change 'X' into '_' in the second row and third column place (index [1][2] in array) +board[1][2] = "_"; +// we change 'X' into '0' in the third row and second column place (index [2][1] in array) +board[2][1] = "0"; +// we change '0' into 'X' in the third row and third column place (index [2][2] in array) +board[2][2] = "X"; + +console.log(board); + export function printBoard(board) { + for(let i = 0; i < board.length; i++){ + console.log(board[i].join(" | ")); + if(i !== board.length - 1){ + console.log("================="); + } + } } /* @@ -24,4 +55,12 @@ export function printBoard(board) { - return false if there are still moves that can be made */ export function checkIfNoMovesLeft(board) { + for(let i = 0; i < board.length; i++){ + for(let j = 0; j < board[i].length; j++){ + if(board[i][j] === '_'){ + return false; + } + } + } + return true; } diff --git a/move-maker.js b/move-maker.js index b462350..e8d8b56 100644 --- a/move-maker.js +++ b/move-maker.js @@ -16,10 +16,30 @@ ]; */ function validateMove(move, board) { - // Implement this at the end if you have time, otherwise you can help your teammates! + // Implement this at the end if you have time, otherwise you can help your teammates! + if (move[0] !== "1" || move[0] !== "2" || move[0] !== "3") { + console.log("Try again..."); + return false; + } else if (move[2] !== "1" || move[2] !== "2" || move[2] !== "3") { + console.log("Try again..."); + return false; + } else if (move[1] !== ",") { + console.log("Try again..."); + return false; + } + return true; +} +// clean version validateMove +/* +function validateMove(move, board){ + if(move.length !=3 || move[0]<'1' || move[0]>'3' || move[2]<'1' || move[2]>'3' || move[1] !==',' || board [move[0]-1][move[2]-1] !=='_'){ + console.log('Try again...'); + return false; + } return true; } + /* Given 3 parameters: - a board (an array of arrays) @@ -32,5 +52,5 @@ function validateMove(move, board) { - Return true */ export function makeMove(board, move, player) { - return false; + return false; } diff --git a/status-checker.js b/status-checker.js index 7df4962..d3a9fbf 100644 --- a/status-checker.js +++ b/status-checker.js @@ -18,8 +18,15 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { + + for (let rowNumber of board) { + if ((board[rowNumber] === 0 && board[rowNumber] === player) + && (board[rowNumber] === 1 && board[rowNumber] === player) + && (board[rowNumber] === 2 && board[rowNumber] === player)) { + return true; + } return false; +} } - /* Given 3 parameters: - a tic-tac-toe board (array of arrays) @@ -29,8 +36,14 @@ function checkRow(board, player, rowNumber) { Otherwise, return false */ function checkColumn(board, player, columnNumber) { + for (let columnNumber of board) { + if ((board[columnNumber] === 0 && board[columnNumber] === player) + && (board[columnNumber] === 1 && board[columnNumber] === player) + && (board[columnNumber] === 2 && board[columnNumber] === player)) { + return true; + } return false; +} } - /* Given 2 parameters: - a tic-tac-toe board (array of arrays) @@ -40,9 +53,20 @@ function checkColumn(board, player, columnNumber) { */ function checkDiagonal(board, player) { // 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 || + board[0][2] === player && + board[1][1] === player && + board[2][0] === player + ) { + return true; + } else { + return false; + } } - /* There is no need to change any code below this line. */