Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

London9-Lorena-Sahar-Pamela Tic-Tac-Toe #10

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
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.join(" | ").replaceAll("_", " "));
console.log("=================");
}
}

/*
Expand All @@ -24,4 +28,10 @@ export function printBoard(board) {
- return false if there are still moves that can be made
*/
export function checkIfNoMovesLeft(board) {
for (let row of board) {
for (let element of row) {
if (element === "_") return false;
}
}
return true;
}
19 changes: 16 additions & 3 deletions move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand All @@ -32,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;
}
66 changes: 42 additions & 24 deletions status-checker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkIfNoMovesLeft } from './board-printer.js';
import { checkIfNoMovesLeft } from "./board-printer.js";

/*
Example board:
Expand All @@ -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);
}

/*
Expand All @@ -29,6 +30,10 @@ function checkRow(board, player, rowNumber) {
Otherwise, return false
*/
function checkColumn(board, player, columnNumber) {
for (let i = 0; i < 3; i++) {
if (board[i][columnNumber] !== player) return false;
}
return true;
}

/*
Expand All @@ -39,43 +44,56 @@ 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[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
}

/*
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;
}