diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..4fb3fcf0b 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -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 @@ -14,6 +15,7 @@ let board = [ let playerTurn = 'X'; + function printBoard() { console.log(' 0 1 2'); console.log('0 ' + board[0].join(' | ')); @@ -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() { diff --git a/JS211_ArrayPractice b/JS211_ArrayPractice new file mode 160000 index 000000000..1e00855c6 --- /dev/null +++ b/JS211_ArrayPractice @@ -0,0 +1 @@ +Subproject commit 1e00855c6576c929c7efa4a62225a720d36dd08a