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
107 changes: 104 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//start here//

// creates and empty "board" for the user to see where marks can be placed.
// using let because the variable is expected to change with more 'X's and 'O's to add
Expand All @@ -21,6 +22,16 @@ let board = [
// assigns the first mark as 'X'
// using let because the variable is expected to change from 'X' to 'O' and back
let playerTurn = 'X';
let turn = 0;


const changeMarker = () => {
if(playerTurn === "X"){
playerTurn = "O"
} else {
playerTurn = "X"
}
}

// is a function that print the current status of the board using the variable - board
const printBoard = () => {
Expand All @@ -33,26 +44,111 @@ const printBoard = () => {
}

const horizontalWin = () => {
if((board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X")
|| (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O")){
return true
} else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X")
|| (board[1][0] == "O" && board[1][1] == "O" && boar0d[1][2] == "O")){
return true
} else if((board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X")
|| (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O")){
return true
} else {
return false
}

// it('should check for horizontal wins', () => {
// board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ];
// assert.equal(horizontalWin(), true);
// });
// Your code here to check for horizontal wins
}

const verticalWin = () => {
if((board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X")
|| (board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O")){
return true
}else if((board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X")
|| (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")){
return true
} else if((board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X")
|| (board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O")){
return true
} else {
return false
}

// it('should check for vertical wins', () => {
// board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ];
// assert.equal(verticalWin(), true);
// });
// Your code here to check for vertical wins
}

const diagonalWin = () => {
if((board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X")
|| (board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O")){
return true
}else if((board[0][2] == "X" && board[1][1] == "X" && board[2][1] == "X")
|| (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")){
return true
} else {
return false
}
// it('should check for diagonal wins', () => {
// board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ];
// assert.equal(diagonalWin(), true);
// });
// Your code here to check for diagonal wins
}

const checkForWin = () => {
// Your code here call each of the check for types of wins
}


const ticTacToe = (row, column) => {
// Your code here to place a marker on the board
board[row][column] = playerTurn

changeMarker()
if (turn >=4){
checkForWin()
} else {
turn++;
}

}
// it('should place mark on the board', () => {
// ticTacToe(1, 1);
// assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]);
// });
// it('should alternate between players', () => {
// ticTacToe(0, 0);
// assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]);
// });

const checkForWin = () => {
if(horizontalWin() || verticalWin() || diagonalWin()){
console.log(`Player ${playerTurn} won!`)
return true
} else {
ticTacToe()
}




// then check for a win





// it('should detect a win', () => {
// assert.equal(checkForWin(), true);
// });
// Your code here call each of the check for types of wins
}


const getPrompt = () => {
printBoard();
console.log("It's Player " + playerTurn + "'s turn.");
Expand Down Expand Up @@ -92,6 +188,11 @@ if (typeof describe === 'function') {
assert.equal(diagonalWin(), true);
});
it('should detect a win', () => {
ticTacToe(0, 0)
ticTacToe(0, 1)
ticTacToe(1, 1)
ticTacToe(0, 2)
ticTacToe(2, 2)
assert.equal(checkForWin(), true);
});
});
Expand Down