diff --git a/dom-tictactoe.js b/dom-tictactoe.js index b585e75..cee1683 100644 --- a/dom-tictactoe.js +++ b/dom-tictactoe.js @@ -28,6 +28,7 @@ const handleClick = (element) => { const addMarker = (id) => { console.log(`We'll place a mark on square: ${id}`) + document.getElementById(id).innerHTML = currentMarker // @TODO, Mix & Match. // You will need the following pieces: @@ -45,12 +46,12 @@ const updateBoard = (id) => { // parses the id string into a number then captures the first and last part the newly create number as row & column const row = parseInt(id.charAt(0)) const column = parseInt(id.charAt(2)) - console.log(`you clicked the sq at ${row} and ${column}`) console.log(board) // @TODO, Your code here: use the above information to change the board variable(array of arrays) // HINT: in your browser open up the dev tools -> console + board[row][column] = currentMarker } const checkForWin = () => { @@ -63,17 +64,50 @@ const checkForWin = () => { changeMarker() } } +// if(horizontalWin()|| verticalWin() || diagonalWin()) { +// console.log(`Player ${playerTurn} won!`) +// return true +// } else { +// changeMarker() +// } +// } 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" && board[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 + } // @TODO, 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 + } // @TODO, Your code here: to check for vertical wins } const diagonalWin = () => { - // @TODO, Your code here: to check for diagonal wins + 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[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 + } + // @TODO, Your code here: to check for diagonal win + } const changeMarker = () => { @@ -92,6 +126,12 @@ const resetBoard = () => { for (i=0; i { const horizontalWin = () => { // Your code here to check for horizontal wins + 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" && board[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 + } } const verticalWin = () => { // Your code here to check for vertical wins + 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 + } } const diagonalWin = () => { // Your code here to check for diagonal wins + 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[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 + } } const checkForWin = () => { // Your code here call each of the check for types of wins + if(horizontalWin()|| verticalWin() || diagonalWin()) { + console.log(`Player ${playerTurn} won!`) + return true + } else { + changeMarker() + } } const ticTacToe = (row, column) => { + board[row][column] = playerTurn + changeMarker() // Your code here to place a marker on the board // then check for a win } +const changeMarker = () => { + if(playerTurn === "X"){ + playerTurn = "O" + } else { + playerTurn = "X" + } +} + const getPrompt = () => { printBoard(); console.log("It's Player " + playerTurn + "'s turn.");