From 28b96ca4bf8a7a32d751c806822d19e7abb903e2 Mon Sep 17 00:00:00 2001 From: Bahare Date: Thu, 12 Jan 2023 19:41:38 +0000 Subject: [PATCH 1/3] status-checker done --- package-lock.json | 130 +--------------------------------------------- status-checker.js | 64 ++++++++++++++--------- 2 files changed, 41 insertions(+), 153 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3693696..a857af8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,136 +1,8 @@ { "name": "tic-tac-toe", "version": "1.0.0", - "lockfileVersion": 2, + "lockfileVersion": 1, "requires": true, - "packages": { - "": { - "name": "tic-tac-toe", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "prompt": "^1.3.0" - } - }, - "node_modules/@colors/colors": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.5.0.tgz", - "integrity": "sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==", - "engines": { - "node": ">=0.1.90" - } - }, - "node_modules/async": { - "version": "3.2.3", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.3.tgz", - "integrity": "sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g==" - }, - "node_modules/colors": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz", - "integrity": "sha512-pFGrxThWcWQ2MsAz6RtgeWe4NK2kUE1WfsrvvlctdII745EW9I0yflqhe7++M5LEc7bV2c/9/5zc8sFcpL0Drw==", - "engines": { - "node": ">=0.1.90" - } - }, - "node_modules/cycle": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz", - "integrity": "sha512-TVF6svNzeQCOpjCqsy0/CSy8VgObG3wXusJ73xW2GbG5rGx7lC8zxDSURicsXI2UsGdi2L0QNRCi745/wUDvsA==", - "engines": { - "node": ">=0.4.0" - } - }, - "node_modules/eyes": { - "version": "0.1.8", - "resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz", - "integrity": "sha512-GipyPsXO1anza0AOZdy69Im7hGFCNB7Y/NGjDlZGJ3GJJLtwNSb2vrzYrTYJRrRloVx7pl+bhUaTB8yiccPvFQ==", - "engines": { - "node": "> 0.1.90" - } - }, - "node_modules/isstream": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", - "integrity": "sha512-Yljz7ffyPbrLpLngrMtZ7NduUgVvi6wG9RJ9IUcyCd59YQ911PBJphODUcbOVbqYfxe1wuYf/LJ8PauMRwsM/g==" - }, - "node_modules/lodash": { - "version": "4.17.21", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", - "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" - }, - "node_modules/mute-stream": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", - "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==" - }, - "node_modules/prompt": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/prompt/-/prompt-1.3.0.tgz", - "integrity": "sha512-ZkaRWtaLBZl7KKAKndKYUL8WqNT+cQHKRZnT4RYYms48jQkFw3rrBL+/N5K/KtdEveHkxs982MX2BkDKub2ZMg==", - "dependencies": { - "@colors/colors": "1.5.0", - "async": "3.2.3", - "read": "1.0.x", - "revalidator": "0.1.x", - "winston": "2.x" - }, - "engines": { - "node": ">= 6.0.0" - } - }, - "node_modules/read": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz", - "integrity": "sha512-rSOKNYUmaxy0om1BNjMN4ezNT6VKK+2xF4GBhc81mkH7L60i6dp8qPYrkndNLT3QPphoII3maL9PVC9XmhHwVQ==", - "dependencies": { - "mute-stream": "~0.0.4" - }, - "engines": { - "node": ">=0.8" - } - }, - "node_modules/revalidator": { - "version": "0.1.8", - "resolved": "https://registry.npmjs.org/revalidator/-/revalidator-0.1.8.tgz", - "integrity": "sha512-xcBILK2pA9oh4SiinPEZfhP8HfrB/ha+a2fTMyl7Om2WjlDVrOQy99N2MXXlUHqGJz4qEu2duXxHJjDWuK/0xg==", - "engines": { - "node": ">= 0.4.0" - } - }, - "node_modules/stack-trace": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", - "integrity": "sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==", - "engines": { - "node": "*" - } - }, - "node_modules/winston": { - "version": "2.4.7", - "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.7.tgz", - "integrity": "sha512-vLB4BqzCKDnnZH9PHGoS2ycawueX4HLqENXQitvFHczhgW2vFpSOn31LZtVr1KU8YTw7DS4tM+cqyovxo8taVg==", - "dependencies": { - "async": "^2.6.4", - "colors": "1.0.x", - "cycle": "1.0.x", - "eyes": "0.1.x", - "isstream": "0.1.x", - "stack-trace": "0.0.x" - }, - "engines": { - "node": ">= 0.10.0" - } - }, - "node_modules/winston/node_modules/async": { - "version": "2.6.4", - "resolved": "https://registry.npmjs.org/async/-/async-2.6.4.tgz", - "integrity": "sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==", - "dependencies": { - "lodash": "^4.17.14" - } - } - }, "dependencies": { "@colors/colors": { "version": "1.5.0", diff --git a/status-checker.js b/status-checker.js index 7df4962..ddd36fb 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,6 +18,7 @@ import { checkIfNoMovesLeft } from './board-printer.js'; Otherwise, return false */ function checkRow(board, player, rowNumber) { + return board[rowNumber].every((element) => element === player); } /* @@ -29,6 +30,12 @@ function checkRow(board, player, rowNumber) { Otherwise, return false */ function checkColumn(board, player, columnNumber) { + for (let row of board) { + if (row[columnNumber] !== player) { + return false; + } + } + return true; } /* @@ -39,43 +46,52 @@ 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) || + (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. */ 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 88b511d97baf8670374a55ee7fdc6065de93043b Mon Sep 17 00:00:00 2001 From: leilafarsani <108967493+leilafarsani@users.noreply.github.com> Date: Thu, 12 Jan 2023 22:06:10 +0000 Subject: [PATCH 2/3] board-printer --- board-printer.js | 16 ++++++++++++++++ move-maker.js | 7 ++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/board-printer.js b/board-printer.js index e11a262..0bb1b8e 100644 --- a/board-printer.js +++ b/board-printer.js @@ -16,7 +16,15 @@ Test your function by calling it with an example tic-tac-toe board. */ export function printBoard(board) { + for (let row of board) { + console.log( + ` ${row[0] !== "_" ? row[0] : " "} | ${ + row[1] !== "_" ? row[1] : " " + } | ${row[2] !== "_" ? row[2] : " "}\n=================` + ); + } } +printBoard(); /* Given a tic-tac-toe board (an array of arrays), @@ -24,4 +32,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..8ca399a 100644 --- a/move-maker.js +++ b/move-maker.js @@ -16,8 +16,7 @@ ]; */ function validateMove(move, board) { - // Implement this at the end if you have time, otherwise you can help your teammates! - return true; + // Implement this at the end if you have time, otherwise you can help your teammates! } /* @@ -31,6 +30,4 @@ function validateMove(move, board) { - Update the board with the player's value ('X' or 'O') in the correct position - Return true */ -export function makeMove(board, move, player) { - return false; -} +export function makeMove(board, move, player) {} From eb759c26c5154be5db0f10577127ead74456a730 Mon Sep 17 00:00:00 2001 From: leilafarsani <108967493+leilafarsani@users.noreply.github.com> Date: Thu, 12 Jan 2023 22:09:30 +0000 Subject: [PATCH 3/3] move-maker --- move-maker.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/move-maker.js b/move-maker.js index 8ca399a..18f525f 100644 --- a/move-maker.js +++ b/move-maker.js @@ -16,7 +16,15 @@ ]; */ function validateMove(move, board) { - // Implement this at the end if you have time, otherwise you can help your teammates! + let regex = /[1-3]/g; + if ( + move.match(regex).length === 2 && + move[1] === "," && + board[move[0] - 1][move[2] - 1] === "_" + ) + return true; + console.log("Try again..."); + return false; } /* @@ -30,4 +38,10 @@ function validateMove(move, board) { - Update the board with the player's value ('X' or 'O') in the correct position - Return true */ -export function makeMove(board, move, player) {} +export function makeMove(board, move, player) { + if (validateMove(move, board)) { + board[move[0] - 1][move[2] - 1] = player; + return true; + } + return false; +}