From 097d42f65ffbce15be9abf93167d14e5459cd8a8 Mon Sep 17 00:00:00 2001 From: James Messina Date: Sun, 7 Feb 2021 13:54:07 -0600 Subject: [PATCH 1/2] tictactoe game working and passing all tests --- dom-tictactoe.js | 7 ++--- main.js | 73 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 62 insertions(+), 18 deletions(-) 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..b857545 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][2] === '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."); From 68fbe04c8c520b28b50ce9fc1adbe45159e4db7f Mon Sep 17 00:00:00 2001 From: James Messina Date: Sun, 7 Feb 2021 14:11:04 -0600 Subject: [PATCH 2/2] functions working, tests passing, made 1 fix --- main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.js b/main.js index b857545..0fc04a8 100644 --- a/main.js +++ b/main.js @@ -60,7 +60,7 @@ 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][2] === 'O')){ + }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;