From 315313dab52f190e513ebed722ee28b297f0e7f3 Mon Sep 17 00:00:00 2001 From: NikSankovich Date: Sun, 22 Jan 2023 15:47:02 -0600 Subject: [PATCH 1/2] first tictactoe commit --- .vscode/settings.json | 2 +- index.html | 76 +++++++++++++++++++++++++++---------------- scripts.js | 29 ++++++++++------- tictactoe.css | 11 +++++-- 4 files changed, 76 insertions(+), 42 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index aef8443..0f9429a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "liveServer.settings.port": 5501 + "liveServer.settings.port": 5503 } \ No newline at end of file diff --git a/index.html b/index.html index 19d0dee..ed022db 100644 --- a/index.html +++ b/index.html @@ -1,31 +1,51 @@ - - - Tic Tac Toe - - - - - - -
-
-

Welcome to Tic Tac Toe

-

Get ready to play!

- - -
- - - - - - - - - -
+ + + + Tic Tac Toe + + + + + + + +
+
+

Welcome to Tic Tac Toe

+

Get ready to play!

+ +
- - + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/scripts.js b/scripts.js index ba09e74..21770ef 100644 --- a/scripts.js +++ b/scripts.js @@ -4,7 +4,7 @@ // 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 +// next to each @TODO you will find tasks that need to be finished // 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' @@ -12,6 +12,8 @@ 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) => { @@ -21,7 +23,7 @@ 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 (!document.getElementById(element.id).innerHTML) { addMarker(element.id) } } @@ -42,15 +44,17 @@ 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. 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" - + // @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line: // = currentMarker // .getElementById(id) // document // .innerHTML + document.getElementById(id).innerHTML = currentMarker; + changeMarker() } @@ -65,7 +69,7 @@ const addMarker = (id) => { // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { - if(currentMarker === "X"){ + if (currentMarker === "X") { currentMarker = "O" } else { currentMarker = "X" @@ -83,24 +87,27 @@ const changeMarker = () => { // This "resetBoard" function is called when the user clicks on the "Restart" button. const resetBoard = () => { - + // @TODO-3: To make your "Restart" button work you'll need to build a line of code here that: - // collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp - + // collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp + // @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line: // squares // .getElementsByTagName("TD") // = // document // const - + + const squares = document.getElementsByTagName("TD") + // loops over the HTML Collection of TDs and clears out the Xs and Os - for (i=0; i < squares.length; i++) { + for (i = 0; i < squares.length; i++) { // will log out the id of each square as it loops over them. console.log(squares[i].id) // sets the innerHTML to null to replace the "X" or "O" squares[i].innerHTML = null - } + } + currentMarker = 'X' } \ No newline at end of file diff --git a/tictactoe.css b/tictactoe.css index 981de05..fcec4a2 100644 --- a/tictactoe.css +++ b/tictactoe.css @@ -1,11 +1,18 @@ +body { + background-color: rgb(131, 207, 131); +} + .jumbotron { margin: 5% auto; margin-left: 43%; + font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; + color: rgb(66, 66, 231); + } td { - height:150px; - width:150px; + height: 150px; + width: 150px; text-align: center; border: 5px solid black; font-size: 100px; From 5ebed89972c669ee3529dda133cde328e4ac195f Mon Sep 17 00:00:00 2001 From: NikSankovich Date: Tue, 24 Jan 2023 11:32:54 -0600 Subject: [PATCH 2/2] added win logic --- .vscode/settings.json | 2 +- index.html | 18 +++--- scripts.js | 141 ++++++++++++++++++++++++++---------------- 3 files changed, 96 insertions(+), 65 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0f9429a..ea90b0f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "liveServer.settings.port": 5503 + "liveServer.settings.port": 5504 } \ No newline at end of file diff --git a/index.html b/index.html index ed022db..4cca167 100644 --- a/index.html +++ b/index.html @@ -21,25 +21,25 @@

Welcome to Tic Tac Toe

- - - + + + - - - + + + - - - + + + diff --git a/scripts.js b/scripts.js index 21770ef..edea842 100644 --- a/scripts.js +++ b/scripts.js @@ -1,43 +1,37 @@ -// *********************** -// INSTRUCTIONS -// *********************** -// 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 // 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' +let board = [ + ["", "", ""], // <-- Row 1, index 0 + ["", "", ""], // <-- Row 2, index 1 + ["", "", ""] // <-- Row 3, index 2 +] +checkForWin = () => { + if (horizontalWin() || verticalWin() || diagonalWin()) { + window.alert("Player " + currentMarker + " won!") + } else { + changeMarker() + } +} + -// 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) => { // this uses the "log" method on the "console" to log out the element's id so we can see it with our human eyes console.log(`The element you clicked on has an id: ${element.id}`) - // 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) { addMarker(element.id) } } - - - - - - - - - - // this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function. const addMarker = (id) => { @@ -45,27 +39,18 @@ const addMarker = (id) => { 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" - - // @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line: - // = currentMarker - // .getElementById(id) - // document - // .innerHTML - document.getElementById(id).innerHTML = currentMarker; - changeMarker() -} - - - - - - + //find me the character at the index of 0 + const row = parseInt(id.charAt(0)); + const column = parseInt(id.charAt(2)); + board[row][column] = currentMarker; + checkForWin(); + //changeMarker() +} // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { @@ -77,37 +62,83 @@ const changeMarker = () => { } +// This "resetBoard" function is called when the user clicks on the "Restart" button. +const resetBoard = () => { + const squares = document.getElementsByTagName("TD") + // loops over the HTML Collection of TDs and clears out the Xs and Os + for (i = 0; i < squares.length; i++) { + // will log out the id of each square as it loops over them. + console.log(squares[i].id) + // sets the innerHTML to null to replace the "X" or "O" + squares[i].innerHTML = null + } + //currentMarker = 'X' + location.reload(); + 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; + } -// This "resetBoard" function is called when the user clicks on the "Restart" button. -const resetBoard = () => { +} - // @TODO-3: To make your "Restart" button work you'll need to build a line of code here that: - // collects all of the "td" elements into an HTML Collection: https://www.w3schools.com/jsref/dom_obj_htmlcollection.asp +const verticalWin = () => { - // @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line: - // squares - // .getElementsByTagName("TD") - // = - // document - // const - const squares = document.getElementsByTagName("TD") + 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][2] == "X" && board[1][1] == "X" && board[2][0] == "X") + || (board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O")) { + return true; + } - // loops over the HTML Collection of TDs and clears out the Xs and Os - for (i = 0; i < squares.length; i++) { + else { + return false; + } - // will log out the id of each square as it loops over them. - console.log(squares[i].id) +} - // sets the innerHTML to null to replace the "X" or "O" - squares[i].innerHTML = null + +const 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][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; } - currentMarker = 'X' -} \ No newline at end of file + + else { + return false; + } + +}