diff --git a/dom-tictactoe.js b/dom-tictactoe.js index b5c261b..165c71d 100644 --- a/dom-tictactoe.js +++ b/dom-tictactoe.js @@ -9,11 +9,8 @@ // 4. GET THIS GAME WORKING!! let currentMarker = 'X' -let board = [ - ['','',''], - ['','',''], - ['','',''] -]; +let board = [['','',''],['','',''],['','','']]; +console.log(board[0][0]) // is called when a square is clicked. "this" = element here const handleClick = (element) => { diff --git a/main.js b/main.js index 96293a9..0fc04a8 100644 --- a/main.js +++ b/main.js @@ -23,7 +23,7 @@ let board = [ let playerTurn = 'X'; // is a function that print the current status of the board using the variable - board -const printBoard = () => { +function printBoard(){ console.log(' 0 1 2'); console.log('0 ' + board[0].join(' | ')); console.log(' ---------'); @@ -32,27 +32,74 @@ const printBoard = () => { console.log('2 ' + board[2].join(' | ')); } -const horizontalWin = () => { - // Your code here to check for horizontal wins +function horizontalWin(){ + + for(let i = 0; i < board.length; i++){ + if((board[i][0] === 'X' && board[i][1] === 'X' && board[i][2] === 'X') || (board[i][0] === 'O' && board[i][1] === 'O' && board[i][2] === 'O')){ + return true; + } + } + return false; } - -const verticalWin = () => { - // Your code here to check for vertical wins + +function 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][0] === 'X') || (board[0][2] === 'O' && board[1][2] === 'O' && board[2][0] === 'O')){ + return true; + }else{ + return false; + } } -const diagonalWin = () => { - // Your code here to check for diagonal wins +function 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][0] === 'X') || (board[0][2] === 'O' && board[1][1] === 'O' && board[2][0] === 'O')){ + return true; + }else{ + return false; + } } -const checkForWin = () => { - // Your code here call each of the check for types of wins +function checkForWin(){ + + if(horizontalWin() || verticalWin() || diagonalWin()){ + return true; + }else{ + return false; + } } -const ticTacToe = (row, column) => { - // Your code here to place a marker on the board - // then check for a win +function ticTacToe(row, column){ + + let moves = 1; + + while(moves > 0 && moves < 5){ + board[row][column] = playerTurn; + moves++; + } + + if(moves >= 5 && checkForWin()){ + console.log('Congratulations, player ' + playerTurn + ' Wins!'); + console.log('Reset Board'); + }else{ + board[row][column] = playerTurn; + } + + if(playerTurn === 'X'){ + playerTurn = 'O'; + }else{ + playerTurn = 'X'; + } } + const getPrompt = () => { printBoard(); console.log("It's Player " + playerTurn + "'s turn.");