From 5655b7f24cd088f68d8c33906832cbf505b43ebd Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 20:13:00 +0000 Subject: [PATCH 1/8] f --- board-printer.js | 6 ++---- status-checker.js | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/board-printer.js b/board-printer.js index e11a262..965963c 100644 --- a/board-printer.js +++ b/board-printer.js @@ -15,13 +15,11 @@ ================= Test your function by calling it with an example tic-tac-toe board. */ -export function printBoard(board) { -} +export function printBoard(board) {} /* Given a tic-tac-toe board (an array of arrays), - return true if there are no moves left to make (there are no more '_' values) - return false if there are still moves that can be made */ -export function checkIfNoMovesLeft(board) { -} +export function checkIfNoMovesLeft(board) {} diff --git a/status-checker.js b/status-checker.js index 7df4962..d204016 100644 --- a/status-checker.js +++ b/status-checker.js @@ -18,6 +18,7 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { + } /* From 77406a6ca0cc13dcd8f786ff71cb4e759b703ff7 Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 20:32:17 +0000 Subject: [PATCH 2/8] printBoard function done --- board-printer.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/board-printer.js b/board-printer.js index 965963c..6be809f 100644 --- a/board-printer.js +++ b/board-printer.js @@ -15,7 +15,12 @@ ================= Test your function by calling it with an example tic-tac-toe board. */ -export function printBoard(board) {} +export function printBoard(board) { + for (let row of board) { + console.log(row.join(" | ").replaceAll("_", " ")); + console.log("================="); + } +} /* Given a tic-tac-toe board (an array of arrays), From 32baec6708977a3dabb378d407c424c57df7f21f Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 20:46:41 +0000 Subject: [PATCH 3/8] checkIfNoMovesLeft(board) function done --- board-printer.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/board-printer.js b/board-printer.js index 6be809f..df4e61c 100644 --- a/board-printer.js +++ b/board-printer.js @@ -27,4 +27,11 @@ export function printBoard(board) { - return true if there are no moves left to make (there are no more '_' values) - return false if there are still moves that can be made */ -export function checkIfNoMovesLeft(board) {} +export function checkIfNoMovesLeft(board) { + for (let row of board) { + for (let element of row) { + if (element === "_") return false; + } + } + return true; +} From f5d0f9a24c2c27103c4db8ec03ef97ae29079a99 Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:08:42 +0000 Subject: [PATCH 4/8] validateMove function done --- move-maker.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/move-maker.js b/move-maker.js index b462350..7236c38 100644 --- a/move-maker.js +++ b/move-maker.js @@ -16,8 +16,17 @@ ]; */ function validateMove(move, board) { - // Implement this at the end if you have time, otherwise you can help your teammates! - return true; + const [x, y] = move.split(",").map((element) => Number(element)); + + const validNumbers = [1, 2, 3]; + if (!validNumbers.includes(x) || !validNumbers.includes(y)) return false; + + if (board[x - 1][y - 1] !== "_") { + console.log("Try again..."); + return false; + } + + return true; } /* @@ -32,5 +41,5 @@ function validateMove(move, board) { - Return true */ export function makeMove(board, move, player) { - return false; + return false; } From c925b5319be52b7862c73dd6f7c40cd2c00cf0d8 Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:19:59 +0000 Subject: [PATCH 5/8] makeMove() completed --- move-maker.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/move-maker.js b/move-maker.js index 7236c38..172b698 100644 --- a/move-maker.js +++ b/move-maker.js @@ -41,5 +41,9 @@ function validateMove(move, board) { - Return true */ export function makeMove(board, move, player) { - return false; + if (!validateMove(move, board)) return false; + + const [x, y] = move.split(",").map((element) => Number(element)); + board[x - 1][y - 1] = player; + return true; } From 162c2e8be003403e5ab2c90d1f68557ebb3af628 Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:26:57 +0000 Subject: [PATCH 6/8] checkRow() COMPLETED --- status-checker.js | 52 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/status-checker.js b/status-checker.js index d204016..5099f0c 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: @@ -18,7 +18,7 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { - + return board[rowNumber].every((element) => element === player); } /* @@ -29,8 +29,7 @@ function checkRow(board, player, rowNumber) { Return true if the player has made a move in all 3 squares in the column Otherwise, return false */ -function checkColumn(board, player, columnNumber) { -} +function checkColumn(board, player, columnNumber) {} /* Given 2 parameters: @@ -40,43 +39,42 @@ 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 } - /* 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; } From 8d04df78b907539756c2ed0055a69d02ce75e5ba Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:32:58 +0000 Subject: [PATCH 7/8] checkColumn() COMPLETED --- status-checker.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/status-checker.js b/status-checker.js index 5099f0c..01ece7d 100644 --- a/status-checker.js +++ b/status-checker.js @@ -29,7 +29,12 @@ function checkRow(board, player, rowNumber) { Return true if the player has made a move in all 3 squares in the column Otherwise, return false */ -function checkColumn(board, player, columnNumber) {} +function checkColumn(board, player, columnNumber) { + for (let i = 0; i < 3; i++) { + if (board[i][columnNumber] !== player) return false; + } + return true; +} /* Given 2 parameters: From e4826124605916ec92b138adbf35643f37da0f71 Mon Sep 17 00:00:00 2001 From: LorenaCapraru <108892538+LorenaCapraru@users.noreply.github.com> Date: Thu, 12 Jan 2023 21:38:56 +0000 Subject: [PATCH 8/8] checkDiagonal() completed --- status-checker.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/status-checker.js b/status-checker.js index 01ece7d..32a8359 100644 --- a/status-checker.js +++ b/status-checker.js @@ -44,6 +44,20 @@ function checkColumn(board, player, columnNumber) { Otherwise, return false */ function checkDiagonal(board, player) { + if ( + board[0][0] === player && + board[1][1] === player && + board[2][2] === player + ) + return true; + if ( + board[0][2] === player && + board[1][1] === player && + board[2][0] === player + ) + return true; + + return false; // It may be easier to use an if statement than a loop here }