From 296a32e206b7e1bbee889a3b6c119449e82af4b8 Mon Sep 17 00:00:00 2001 From: EvanKelley Date: Wed, 8 Feb 2023 19:08:50 -0600 Subject: [PATCH 1/5] pushing it up --- index.html | 16 +++++++++++++--- scripts.js | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 19d0dee..6798057 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@

Welcome to Tic Tac Toe

-

Get ready to play!

+

Get ready to draw!

@@ -20,8 +20,18 @@

Welcome to Tic Tac Toe

- - + + + + + + + + + + + + diff --git a/scripts.js b/scripts.js index ba09e74..aa31f75 100644 --- a/scripts.js +++ b/scripts.js @@ -44,7 +44,7 @@ const addMarker = (id) => { 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) @@ -70,7 +70,9 @@ const changeMarker = () => { } else { currentMarker = "X" } + console.log('AFTER currentMarker:' ,currentMarker) } +// BEFORE @@ -86,7 +88,7 @@ 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 squares = document.getElementsByTagName("TD") // @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line: // squares // .getElementsByTagName("TD") From f1d51dbaf6a7c33263a0538fe0388fcbe284e416 Mon Sep 17 00:00:00 2001 From: EvanKelley Date: Wed, 22 Feb 2023 19:30:55 -0600 Subject: [PATCH 2/5] add --- scripts.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts.js b/scripts.js index aa31f75..7b67349 100644 --- a/scripts.js +++ b/scripts.js @@ -70,9 +70,8 @@ const changeMarker = () => { } else { currentMarker = "X" } - console.log('AFTER currentMarker:' ,currentMarker) + console.log('BEFORE currentMarker:' ,currentMarker) } -// BEFORE From 89d6ed0f9c82afd2ca8437b7d86f326628ae9b74 Mon Sep 17 00:00:00 2001 From: EvanKelley Date: Wed, 22 Feb 2023 20:23:51 -0600 Subject: [PATCH 3/5] started to add functions to look for winner and removed comments --- index.html | 35 +++++++++++++----------- scripts.js | 78 +++++------------------------------------------------- 2 files changed, 25 insertions(+), 88 deletions(-) diff --git a/index.html b/index.html index 6798057..7d5c701 100644 --- a/index.html +++ b/index.html @@ -3,39 +3,42 @@ Tic Tac Toe - - +
+

Welcome to Tic Tac Toe

Get ready to draw!

- - +
+ + - - - - + + + + - - - + + + + - - - + + + - - +
+
+ diff --git a/scripts.js b/scripts.js index 7b67349..0e644e5 100644 --- a/scripts.js +++ b/scripts.js @@ -1,69 +1,27 @@ -// *********************** -// 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 board = [ +// ['', '', ''] +// ['', '', ''] +// ['', '', ''] +// ] 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) => { - - // 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) => { - - // @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. + // board[row][column] = currentMarker 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 - changeMarker() } - - - - - - - - -// This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { if(currentMarker === "X"){ currentMarker = "O" @@ -74,34 +32,10 @@ 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 const squares = document.getElementsByTagName("TD") - // @TODO-3.5: MIX & MATCH, You will need the following pieces of code to build that line: - // squares - // .getElementsByTagName("TD") - // = - // document - // const - - // 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 } } \ No newline at end of file From cbb2b0f5b5266e8399d5673cd551b184273c3b3b Mon Sep 17 00:00:00 2001 From: EvanKelley Date: Fri, 24 Feb 2023 11:53:52 -0600 Subject: [PATCH 4/5] completed win condition --- scripts.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/scripts.js b/scripts.js index 0e644e5..04470a6 100644 --- a/scripts.js +++ b/scripts.js @@ -1,11 +1,14 @@ -// let board = [ -// ['', '', ''] -// ['', '', ''] -// ['', '', ''] -// ] +let board = [ + ['', '', ''], + ['', '', ''], + ['', '', ''], +] let currentMarker = 'X' const handleClick = (element) => { + const row = parseInt(element.id.charAt(0)) + const column = parseInt(element.id.charAt(2)) + board[row][column] = currentMarker console.log(`The element you clicked on has an id: ${element.id}`) if(!document.getElementById(element.id).innerHTML){ addMarker(element.id) @@ -14,11 +17,10 @@ const handleClick = (element) => { const addMarker = (id) => { - // board[row][column] = currentMarker console.log(`*** The current marker is: ${currentMarker}. ***`) console.log(`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`) document.getElementById(id).innerHTML = currentMarker - changeMarker() + checkForWin() } @@ -38,4 +40,49 @@ const resetBoard = () => { console.log(squares[i].id) squares[i].innerHTML = null } +} + + +const checkForWin = () => { + if(horizontalWin() || verticalWin() || diagonalWin()) { + window.alert(`Player ${currentMarker} won!`) + } 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} +} + +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} +} + +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[2][0] == "X" && board[1][1] == "X" && board[0][2] == "X") + || (board[2][0] == "O" && board[1][1] == "O" && board[0][2] == "O")) + {return true} + else {return false} } \ No newline at end of file From 3a7aee804bc0b1d23ed9984f7ede037be7bb4527 Mon Sep 17 00:00:00 2001 From: EvanKelley Date: Sun, 26 Feb 2023 10:42:13 -0600 Subject: [PATCH 5/5] final draft --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index aef8443..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "liveServer.settings.port": 5501 -} \ No newline at end of file