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
39 changes: 39 additions & 0 deletions board-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,38 @@
=================
Test your function by calling it with an example tic-tac-toe board.
*/
//It needs to be checked
let board = [
["X", "0", "X"],
["X", "0", "X"],
["0", "X", "0"],
];
console.log(board);

// we change 'X' into '_' in the first row and second column place (index [0][1] in array)
board[0][1] = "_";
// we change 'X' into '_' in the first row and third column place (index [0][2] in array)
board[0][2] = "_";
// we change 'X' into '_' in the second row and first column place (index [1][0] in array)
board[1][0] = "_";
// we change '0' into 'X' in the second row and second column place (index [1][1] in array)
board[1][1] = "X";
// we change 'X' into '_' in the second row and third column place (index [1][2] in array)
board[1][2] = "_";
// we change 'X' into '0' in the third row and second column place (index [2][1] in array)
board[2][1] = "0";
// we change '0' into 'X' in the third row and third column place (index [2][2] in array)
board[2][2] = "X";

console.log(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("=================");
}
}
}

/*
Expand All @@ -24,4 +55,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;
}
24 changes: 22 additions & 2 deletions move-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,30 @@
];
*/
function validateMove(move, board) {
// Implement this at the end if you have time, otherwise you can help your teammates!
// Implement this at the end if you have time, otherwise you can help your teammates!
if (move[0] !== "1" || move[0] !== "2" || move[0] !== "3") {
console.log("Try again...");
return false;
} else if (move[2] !== "1" || move[2] !== "2" || move[2] !== "3") {
console.log("Try again...");
return false;
} else if (move[1] !== ",") {
console.log("Try again...");
return false;
}
return true;
}
// clean version validateMove
/*
function validateMove(move, board){
if(move.length !=3 || move[0]<'1' || move[0]>'3' || move[2]<'1' || move[2]>'3' || move[1] !==',' || board [move[0]-1][move[2]-1] !=='_'){
console.log('Try again...');
return false;
}
return true;
}


/*
Given 3 parameters:
- a board (an array of arrays)
Expand All @@ -32,5 +52,5 @@ function validateMove(move, board) {
- Return true
*/
export function makeMove(board, move, player) {
return false;
return false;
}
30 changes: 27 additions & 3 deletions status-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import { checkIfNoMovesLeft } from './board-printer.js';
Otherwise, return false
*/
function checkRow(board, player, rowNumber) {

for (let rowNumber of board) {
if ((board[rowNumber] === 0 && board[rowNumber] === player)
&& (board[rowNumber] === 1 && board[rowNumber] === player)
&& (board[rowNumber] === 2 && board[rowNumber] === player)) {
return true;
} return false;
}
}

/*
Given 3 parameters:
- a tic-tac-toe board (array of arrays)
Expand All @@ -29,8 +36,14 @@ function checkRow(board, player, rowNumber) {
Otherwise, return false
*/
function checkColumn(board, player, columnNumber) {
for (let columnNumber of board) {
if ((board[columnNumber] === 0 && board[columnNumber] === player)
&& (board[columnNumber] === 1 && board[columnNumber] === player)
&& (board[columnNumber] === 2 && board[columnNumber] === player)) {
return true;
} return false;
}
}

/*
Given 2 parameters:
- a tic-tac-toe board (array of arrays)
Expand All @@ -40,9 +53,20 @@ function checkColumn(board, player, columnNumber) {
*/
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 ||
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.
*/
Expand Down