From 1455f7d4f922430296ec3e2055db96f26e85b341 Mon Sep 17 00:00:00 2001 From: elahemortazavi Date: Sat, 10 Dec 2022 14:16:42 +0000 Subject: [PATCH 1/5] tic-toc fhy --- board-printer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/board-printer.js b/board-printer.js index e11a262..d83735b 100644 --- a/board-printer.js +++ b/board-printer.js @@ -6,6 +6,7 @@ ['_', 'X', '_'], ['O', 'O', 'X'] ]; + We should output something like this (feel free to be creative): X | | ================= From 47c107359a9a89279e547054248287d7b0c5b392 Mon Sep 17 00:00:00 2001 From: howard-ss <102481688+howard-ss@users.noreply.github.com> Date: Sat, 10 Dec 2022 15:05:14 +0000 Subject: [PATCH 2/5] Update status-checker.js Update status-checker.js --- status-checker.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/status-checker.js b/status-checker.js index 7df4962..c482aa6 100644 --- a/status-checker.js +++ b/status-checker.js @@ -18,8 +18,14 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { + for (let player of rowNumber){ + if (rowNumber.includes('X')){ + return true; + }else{ + return false; + } + } } - /* Given 3 parameters: - a tic-tac-toe board (array of arrays) @@ -29,8 +35,14 @@ function checkRow(board, player, rowNumber) { Otherwise, return false */ function checkColumn(board, player, columnNumber) { + for (let player of columnNumber){ + if (columnNumber.includes('X')){ + return true; + }else{ + return false; + } + } } - /* Given 2 parameters: - a tic-tac-toe board (array of arrays) @@ -39,7 +51,14 @@ 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 + + if (board.forEach(player).includes('X')){ + return true; + }else{ + return false; + } } From 4b27d6d6891141ab7a52807e0fba3e03898c540b Mon Sep 17 00:00:00 2001 From: elahemortazavi Date: Wed, 11 Jan 2023 09:48:04 +0000 Subject: [PATCH 3/5] part 1-1 --- board-printer.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/board-printer.js b/board-printer.js index d83735b..0939d58 100644 --- a/board-printer.js +++ b/board-printer.js @@ -16,8 +16,17 @@ ================= Test your function by calling it with an example tic-tac-toe board. */ +let board = [ + ['X', '_', '_'], + ['_', 'X', '_'], + ['O', 'O', 'X'] + ]; + export function printBoard(board) { + console.log(`[${boardArr[2]}]\n[${boardArr[0]}]\n[${boardArr[1]}]`); + } +printBoard(board); /* Given a tic-tac-toe board (an array of arrays), From d43933c55b3937453045248fcbcaed3b212655bd Mon Sep 17 00:00:00 2001 From: Howard Date: Fri, 13 Jan 2023 14:18:56 +0000 Subject: [PATCH 4/5] passed prompt test --- board-printer.js | 14 ++++++++ move-maker.js | 22 ++++++++++++ status-checker.js | 86 +++++++++++++++++++++++++---------------------- 3 files changed, 82 insertions(+), 40 deletions(-) diff --git a/board-printer.js b/board-printer.js index d83735b..8d79d62 100644 --- a/board-printer.js +++ b/board-printer.js @@ -17,6 +17,12 @@ Test your function by calling it with an example tic-tac-toe 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("================="); + } + } } /* @@ -25,4 +31,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..7d90fca 100644 --- a/move-maker.js +++ b/move-maker.js @@ -17,6 +17,21 @@ */ function validateMove(move, board) { // Implement this at the end if you have time, otherwise you can help your teammates! + let moveArray = move.split(",").map((x) => parseInt(x)); + if (moveArray.length !== 2) { + console.log("Try again..."); + return false; + } + let row = moveArray[0] - 1; + let col = moveArray[1] - 1; + if (row < 0 || row > 2 || col < 0 || col > 2) { + console.log("Try again..."); + return false; + } + if (board[row][col] !== "_") { + console.log("Try again..."); + return false; + } return true; } @@ -32,5 +47,12 @@ function validateMove(move, board) { - Return true */ export function makeMove(board, move, player) { + if (validateMove(move, board)) { + let moveArray = move.split(",").map((x) => parseInt(x)); + let row = moveArray[0] - 1; + let col = moveArray[1] - 1; + board[row][col] = player; + return true; + } return false; } diff --git a/status-checker.js b/status-checker.js index c482aa6..f2cb059 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,13 +18,12 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { - for (let player of rowNumber){ - if (rowNumber.includes('X')){ - return true; - }else{ + for (let i = 0; i < board[rowNumber].length; i++) { + if (board[rowNumber][i] !== player) { return false; + } } - } + return true; } /* Given 3 parameters: @@ -35,13 +34,12 @@ function checkRow(board, player, rowNumber) { Otherwise, return false */ function checkColumn(board, player, columnNumber) { - for (let player of columnNumber){ - if (columnNumber.includes('X')){ - return true; - }else{ - return false; + for (let i = 0; i < board.length; i++) { + if (board[i][columnNumber] !== player) { + return false; } } + return true; } /* Given 2 parameters: @@ -51,50 +49,58 @@ 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.forEach(player).includes('X')){ - return true; - }else{ - return false; - } + 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; } - /* 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 bf50144f76a07a5b68a1440a7abadd49a781da97 Mon Sep 17 00:00:00 2001 From: elahemortazavi Date: Fri, 13 Jan 2023 16:39:15 +0000 Subject: [PATCH 5/5] update --- board-printer.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/board-printer.js b/board-printer.js index 0939d58..2e9d48a 100644 --- a/board-printer.js +++ b/board-printer.js @@ -16,17 +16,13 @@ ================= Test your function by calling it with an example tic-tac-toe board. */ -let board = [ - ['X', '_', '_'], - ['_', 'X', '_'], - ['O', 'O', 'X'] - ]; + export function printBoard(board) { - console.log(`[${boardArr[2]}]\n[${boardArr[0]}]\n[${boardArr[1]}]`); + } -printBoard(board); + /* Given a tic-tac-toe board (an array of arrays),