From d6e9a7d65f2609ffc08f6566064c6f6a6c4d1a7e Mon Sep 17 00:00:00 2001 From: ruru-dev Date: Wed, 22 Feb 2023 20:23:47 -0600 Subject: [PATCH 1/2] added functions for checking winner --- index.html | 18 +++++++++--------- scripts.js | 52 +++++++++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/index.html b/index.html index ca9daec..361c57a 100644 --- a/index.html +++ b/index.html @@ -19,19 +19,19 @@

Welcome to Tic Tac Toe

- - - + + + - - - + + + - - - + + + diff --git a/scripts.js b/scripts.js index 1f20764..f914c07 100644 --- a/scripts.js +++ b/scripts.js @@ -1,10 +1,12 @@ -// *********************** -// INSTRUCTIONS -// *********************** +// Go to the top your scripts.js file and create an array called board with three arrays inside of it. +// Each of these inner arrays should have three places held with empty quotes: "", separated by commas, ,. -// 1. Read the code below one block at a time. -// 2. Look for the @TODOs, and figure out how to fix them. - // next to each @TODO you will find tasks that need to be finished +let board = [ + ["", "", ""], + ["", "", ""], + ["", "", ""] +] +; // The variable will change from X to O based on what player turn it is. We need to hold this so we can place an X or O on the board when they're clicked. let currentMarker = 'X' @@ -55,14 +57,6 @@ const addMarker = (id) => { } - - - - - - - - // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { if(currentMarker === "X"){ @@ -73,14 +67,6 @@ const changeMarker = () => { } - - - - - - - - // This "resetBoard" function is called when the user clicks on the "Restart" button. const resetBoard = () => { @@ -104,4 +90,24 @@ const resetBoard = () => { // sets the innerHTML to null to replace the "X" or "O" squares[i].innerHTML = null } -} \ No newline at end of file +} + +// Set of Rules + +// Horizontal Win, a.k.a, If row 0 equals all "X"s or "O"s: + +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") +) + +// Vertical Wins, a.k.a, if column 0 equals all "X"s or "O"s: + +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") +) + +// Diagonal Wins, a.k.a, If there is a line created diagonally across the board with "X"s or with "O"s: + +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") +) \ No newline at end of file From 0287998919dfa2785e61e77aa5dfacb541ebd57f Mon Sep 17 00:00:00 2001 From: ruru-dev Date: Sun, 26 Feb 2023 23:28:49 -0600 Subject: [PATCH 2/2] add winner logic --- scripts.js | 118 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 40 deletions(-) diff --git a/scripts.js b/scripts.js index f914c07..5729b15 100644 --- a/scripts.js +++ b/scripts.js @@ -5,15 +5,13 @@ let board = [ ["", "", ""], ["", "", ""], ["", "", ""] -] -; +]; + +let winner; // The variable will change from X to O based on what player turn it is. We need to hold this so we can place an X or O on the board when they're clicked. let currentMarker = 'X' - - - // this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML. // "this" is a special word in JS but "element" could have been "thing" or "el" or whatever we wanted it to be as long as we use it again in the "console.log" statement const handleClick = (element) => { @@ -23,37 +21,35 @@ const handleClick = (element) => { // this next line prevents an X being changed to an O or an O being changed to an X by... // checking to see if the square clicked has anything in it, if not continue - if(!document.getElementById(element.id).innerHTML){ + if(!winner && !document.getElementById(element.id).innerHTML){ addMarker(element.id) } } - - - - - - - - - - // this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function. const addMarker = (id) => { - // @TODO-1: Open the console tab in your Chrome Inspector Tool and click on the top-left square to see what's logged to the console. + // Log the current marker console.log(`*** The current marker is: ${currentMarker}. ***`) console.log(`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`) - // @TODO-2: Build a line of code that will set the innerHTML property of the element that was clicked to the "currentMarker" - document.getElementById(id).innerHTML = currentMarker; - // @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line: - // = currentMarker - // .getElementById(id) - // document - // .innerHTML + // Get the square that was clicked + const clickedSquare = document.getElementById(id); - changeMarker() + // Determine what row/column the square was on based on its id + const row = parseInt(clickedSquare.id.charAt(0)); + const column = parseInt(clickedSquare.id.charAt(2)); + + // Mark it in the data structure for the board + board[row][column] = currentMarker; + + // Mark it in the html for the board + clickedSquare.innerHTML = currentMarker; + + // Log the current state of the board + console.log(board); + + checkForWin(); } @@ -89,25 +85,67 @@ const resetBoard = () => { // sets the innerHTML to null to replace the "X" or "O" squares[i].innerHTML = null - } -} + } -// Set of Rules + board = [ + ["", "", ""], + ["", "", ""], + ["", "", ""] + ]; + currentMarker = 'X'; + winner = null; -// Horizontal Win, a.k.a, If row 0 equals all "X"s or "O"s: + console.log(board); +} -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") -) +const checkForWin = () => { + if(horizontalWin() || verticalWin() || diagonalWin()) { + winner = currentMarker; + window.alert(`Player ${currentMarker} won!`) + } else { + changeMarker() + } +} -// Vertical Wins, a.k.a, if column 0 equals all "X"s or "O"s: +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") + || (board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") + || (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O") + || (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; + } + + return false; +} -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") -) +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") + || (board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") + || (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O") + || (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; + } -// Diagonal Wins, a.k.a, If there is a line created diagonally across the board with "X"s or with "O"s: + 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") + || (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; + } -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") -) \ No newline at end of file + return false; +} \ No newline at end of file