Skip to content
Open
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
50 changes: 45 additions & 5 deletions 03week/ticTacToe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
'use strict';

/*
Player x chooses a row and a column. Choice is stored in a multidimensional array.
Display an 'X' on board.
board[row][column] = 'X'
Player x changes to player y.
Player y chooses a row and a column. Choice is stored in a multidimensional array.
Display a 'Y' on board.
board[row][column] = 'Y'
*/
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
Expand All @@ -24,23 +33,54 @@ function printBoard() {
}

function horizontalWin() {
// Your code here
if (playerTurn === board[0][0] && playerTurn === board[0][1] && playerTurn === board[0][2]) {
return true;
}
if (playerTurn === board[1][0] && playerTurn === board[1][1] && playerTurn === board[1][2]) {
return true;
}
if (playerTurn === board[2][0] && playerTurn === board[2][1] && playerTurn === board[2][2]) {
return true;
}
}


function verticalWin() {
// Your code here
if (playerTurn === board[0][0] && playerTurn === board[1][0] && playerTurn === board[2][0]) {
return true;
}
if (playerTurn === board[0][1] && playerTurn === board[1][1] && playerTurn === board[2][1]) {
return true;
}
if (playerTurn === board[0][2] && playerTurn === board[1][2] && playerTurn === board[2][2]) {
return true;
}
}

function diagonalWin() {
// Your code here
if (playerTurn === board[0][0] && playerTurn === board[1][1] && playerTurn === board[2][2]) {
return true;
}
if (playerTurn === board[0][2] && playerTurn === board[1][1] && playerTurn === board[2][0]) {
return true;
}
}

function checkForWin() {
// Your code here
return diagonalWin() || horizontalWin() || verticalWin();
}

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

Choose a reason for hiding this comment

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

Your code does not check of a valid move meaning that i can input any number, letter, or negative number and the program with crash.

if (checkForWin()){
console.log(`${playerTurn} wins!`)

Choose a reason for hiding this comment

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

Your win command does work and stops the game but does not show the outcome of the final board.

process.exit();
}
if (playerTurn === 'X') {
playerTurn = 'O';
} else {
playerTurn = 'X';
}
}

function getPrompt() {
Expand Down