Skip to content
Open
Changes from 1 commit
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
63 changes: 50 additions & 13 deletions 02week/ticTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,81 @@ function printBoard() {
console.log(' ---------');
console.log('2 ' + board[2].join(' | '));
}

//code here
function horizontalWin() {
// if horizontal grid has equal items, there is a win

return board[0].every(square => square === playerTurn) || board[1].every(square => square === playerTurn) || board[2].every(square => square === playerTurn);
}

function verticalWin() {
// if vertical grid has equal items, there is a win

return [board[0][0], board[1][0], board[2][0]].every(square => square === playerTurn) || [board[0][1], board[1][1], board[2][1]].every(square => square === playerTurn) || [board[0][2], board[1][2], board[2][2]].every(square => square === playerTurn);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the every() js method here.

}

function diagonalWin() {
// if both diagonal grids have equal items, there is a win
return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn);
}

function checkForWin() {
//
if (horizontalWin()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consolidate your checkForWin() logic with || operators to reduce redundant code.

printBoard();
console.log(`Congratulations player ${playerTurn}. You won on the horizontal!`);
return true;
} else if (verticalWin()) {
printBoard();
console.log(`Congratulations player ${playerTurn}. You won on the vertical!`);
return true;
} else if (diagonalWin()) {
printBoard();
console.log(`Congratulations player ${playerTurn}. You won on the diagonal!`);
return true;
}
return false;
}

function ticTacToe(row, column) {
// Your code here
board[row][column] = playerTurn;

const validValue = (myIndex) => {
const valuesArr = [0,1,2];
return valuesArr.some(validIndex => myIndex == validIndex);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good data scrub.

}

if (validValue(row) && validValue(column)) {
if (!board[row][column].trim() ) {
board[row][column] = playerTurn;

if (!checkForWin()) {
if (playerTurn === 'X') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a ternary operator to toggle the playerTurn value.

playerTurn = 'O';
} else {
playerTurn = 'X';
}
return false;
} else {
console.log(`The winner is player ${playerTurn}. Start a new game`);
return true;
}
} else {
console.log('Please choose another square! That one is taken!');
}
} else {
console.log('Please enter a valid index. Valid values are 0, 1, 2');
}
}

function getPrompt() {
printBoard();
console.log("It's Player " + playerTurn + "'s turn.");
rl.question('row: ', (row) => {
rl.question('column: ', (column) => {
ticTacToe(row, column);
getPrompt();
if (!ticTacToe(row, column)) {
getPrompt();
} else {
process.exit(0);
}
});
});

}



// Tests

if (typeof describe === 'function') {
Expand Down