Skip to content
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
23 changes: 23 additions & 0 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
['_', 'X', '_'],
['O', 'O', 'X']
];

We should output something like this (feel free to be creative):
X | |
=================
Expand All @@ -15,13 +16,35 @@
=================
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("=================");
}
}
}


/*
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) {
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;
}
22 changes: 22 additions & 0 deletions move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
77 changes: 51 additions & 26 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,8 +18,13 @@ import { checkIfNoMovesLeft } from './board-printer.js';
Otherwise, return false
*/
function checkRow(board, player, rowNumber) {
for (let i = 0; i < board[rowNumber].length; i++) {
if (board[rowNumber][i] !== player) {
return false;
}
}
return true;
}

/*
Given 3 parameters:
- a tic-tac-toe board (array of arrays)
Expand All @@ -29,8 +34,13 @@ function checkRow(board, player, rowNumber) {
Otherwise, return false
*/
function checkColumn(board, player, columnNumber) {
for (let i = 0; i < board.length; i++) {
if (board[i][columnNumber] !== player) {
return false;
}
}
return true;
}

/*
Given 2 parameters:
- a tic-tac-toe board (array of arrays)
Expand All @@ -39,43 +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[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;
}