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
102 changes: 95 additions & 7 deletions 03week/ticTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const readline = require('readline');
const { truncate } = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
Expand All @@ -14,6 +15,7 @@ let board = [

let playerTurn = 'X';


function printBoard() {
console.log(' 0 1 2');
console.log('0 ' + board[0].join(' | '));
Expand All @@ -22,25 +24,111 @@ function printBoard() {
console.log(' ---------');
console.log('2 ' + board[2].join(' | '));
}

/*
*if a player won horizontally on any row, return true
*Otherwise return false
*/
function horizontalWin() {
// Your code here
}

if(board[0][0] === "X" && board[0][1] === "X" && board[0][2] === "X"){
return true
} else if (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"){
return true
} else if(board[1][0] === "O" && board[1][1] === "O" && board[1][2] === "O"){
return true
} else if(board[2][0] === "X" && board[2][1] === "X" && board[2][2] === "X"){
return true
} else if(board[2][0] === "O" && board[2][1] === "O" && board[2][2] === "O"){
return true
} else{
return false
}

}











/*
*if a player won vertically on any row, return true
*Otherwise return false
*
*/
function verticalWin() {
// Your code here
if(board[0][0] === "X" && board[1][0] === "X" && board[2][0] === "X"){
return true
} else if (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"){
return true
} else if(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"){
return true
} else if(board[0][2] === "O" && board[1][2] === "O" && board[2][2] === "O"){
return true
} else{
return false
}
}
/*
*if a player won diagonally on any row, return true
*Otherwise return false
*/

function diagonalWin() {
// Your code here
if(board[0][0] === "X" && board[1][1] === "X" && board[2][2] === "X"){
return true
} else if (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[0][2] === "X"){
return true
} else if (board[0][2] === "O" && board[1][1] === "O" && board[0][2] === "O"){
return true
} else {
return false
}

}

// if they won H V D return true else return false
function checkForWin() {
// Your code here
// code should call each check for types of wins
if(horizontalWin() == true || verticalWin() == true || diagonalWin == true ){
return true
} else {
return false
}
}

function ticTacToe(row, column) {
// Your code here
// check that row and column are valid(optional)
// check that row and column are empty(optional)

//update the board
board[row][column] = playerTurn;
if(playerTurn == "x") {
playerTurn = "O"
}
checkForWin()
//
//check if they won? if they did print nice message, (optional)
//maybe clear board(optional)
// set the player turn to be the other player(still need to figure out how to swap)



//code here should set marker on the board(CHECK)
// then check for win
}

function getPrompt() {
Expand Down
1 change: 1 addition & 0 deletions JS211_ArrayPractice
Submodule JS211_ArrayPractice added at 1e0085