From 12388aa971906a06d119d0e7a0bb8f091df42b62 Mon Sep 17 00:00:00 2001 From: 4621553 Date: Fri, 10 Feb 2023 21:31:14 -0600 Subject: [PATCH 1/5] created javascript --- index.html | 34 +++++++++++++++++++++++----------- scripts.js | 9 ++++++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 19d0dee..e15b36e 100644 --- a/index.html +++ b/index.html @@ -1,11 +1,9 @@ - + Tic Tac Toe - - - + @@ -14,17 +12,31 @@

Welcome to Tic Tac Toe

Get ready to play!

- + - - - - + + + + + + + + + + + + + - -
diff --git a/scripts.js b/scripts.js index ba09e74..8fc42d3 100644 --- a/scripts.js +++ b/scripts.js @@ -51,8 +51,11 @@ const addMarker = (id) => { // document // .innerHTML - changeMarker() -} + changeMarker( document.getElementById(id).innerHTML = currentMarker) + + +} + @@ -86,7 +89,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 25d68125062f30b8e891090d40e833517079018f Mon Sep 17 00:00:00 2001 From: 4621553 Date: Wed, 22 Feb 2023 20:28:14 -0600 Subject: [PATCH 2/5] added logic --- index.html | 18 +++---- scripts.js | 142 ++++++++++++++++++++++++++++------------------------- 2 files changed, 85 insertions(+), 75 deletions(-) diff --git a/index.html b/index.html index e15b36e..e323d49 100644 --- a/index.html +++ b/index.html @@ -23,19 +23,19 @@

Welcome to Tic Tac Toe

- - - + + + - - - + + + - - - + + +
diff --git a/scripts.js b/scripts.js index 8fc42d3..6702eff 100644 --- a/scripts.js +++ b/scripts.js @@ -4,106 +4,116 @@ // 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' - - - - -// this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML. +let currentMarker = "X"; +let board = [ + ["", "", ""], + ["", "", ""], + ["", "", ""], +]; + +// 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}`) + 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) + 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. + console.log(`*** The current marker is: ${currentMarker}. ***`); + console.log( + `Therefore, a "${currentMarker}" should be placed in the square with the id: ${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 - - changeMarker( document.getElementById(id).innerHTML = currentMarker) - - -} - - - - - - - - - - + // .innerHTML + const row = parseInt(id.charAt(0)); + const column = parseInt(id.charAt(2)); + board[row][column] = currentMarker; + changeMarker((document.getElementById(id).innerHTML = currentMarker)); +}; + +const checkForWin = () => { + if (horizontalWin() || verticalWin() || diagonalWin()) { + window.alert(`Player ${currentMarker} won!`); + } else { + changeMarker(); + } +}; + +// function 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 +// ((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; +// } +// }; + +// 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} +// }; + +// 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} +// }; // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { - if(currentMarker === "X"){ - currentMarker = "O" + if (currentMarker === "X") { + currentMarker = "O"; } else { - currentMarker = "X" + currentMarker = "X"; } -} - - - - - - - - - +}; // 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") + // 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++) { + // 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) + 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 + squares[i].innerHTML = null; + } +}; From 415856e6a10cbee71e265ca55148336924fb6d03 Mon Sep 17 00:00:00 2001 From: 4621553 Date: Wed, 22 Feb 2023 21:02:57 -0600 Subject: [PATCH 3/5] added board --- scripts.js | 131 ++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/scripts.js b/scripts.js index 6702eff..d41e55b 100644 --- a/scripts.js +++ b/scripts.js @@ -4,116 +4,115 @@ // 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"; +let currentMarker = 'X' let board = [ ["", "", ""], ["", "", ""], ["", "", ""], ]; -// this "handleClick" function is called when a box is clicked. Here, "element" will hold the same value as "this" does in the HTML. + + + +// 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}`); + 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); + 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. - console.log(`*** The current marker is: ${currentMarker}. ***`); - console.log( - `Therefore, a "${currentMarker}" should be placed in the square with the id: ${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 + // .innerHTML + const row = parseInt(id.charAt(0)); const column = parseInt(id.charAt(2)); board[row][column] = currentMarker; - changeMarker((document.getElementById(id).innerHTML = currentMarker)); -}; -const checkForWin = () => { - if (horizontalWin() || verticalWin() || diagonalWin()) { - window.alert(`Player ${currentMarker} won!`); - } else { - changeMarker(); - } -}; - -// function 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 -// ((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; -// } -// }; - -// 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} -// }; - -// 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} -// }; + changeMarker(document.getElementById(id).innerHTML = currentMarker) + + +} + + + + + + + + + + // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { - if (currentMarker === "X") { - currentMarker = "O"; + if(currentMarker === "X"){ + currentMarker = "O" } else { - currentMarker = "X"; + currentMarker = "X" } -}; +} + + + + + + + + + // 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"); + // 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++) { + 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); + console.log(squares[i].id) // sets the innerHTML to null to replace the "X" or "O" - squares[i].innerHTML = null; - } -}; + squares[i].innerHTML = null + } +} \ No newline at end of file From 0c22e1aadc06db6a55a6fa563b352a9cab5ff1c1 Mon Sep 17 00:00:00 2001 From: 4621553 Date: Fri, 24 Feb 2023 22:22:02 -0600 Subject: [PATCH 4/5] the game is running --- scripts.js | 177 ++++++++++++++++++++++++++--------------------------- 1 file changed, 87 insertions(+), 90 deletions(-) diff --git a/scripts.js b/scripts.js index d41e55b..e0dc391 100644 --- a/scripts.js +++ b/scripts.js @@ -1,118 +1,115 @@ -// *********************** -// 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 currentMarker = "X"; let board = [ ["", "", ""], ["", "", ""], ["", "", ""], ]; - - - -// 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) => { + console.log(`The element you clicked on has an id: ${element.id}`); - // 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) + 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. - 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 + console.log(`*** The current marker is: ${currentMarker}. ***`); + console.log( + `Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}` + ); const row = parseInt(id.charAt(0)); const column = parseInt(id.charAt(2)); board[row][column] = currentMarker; - changeMarker(document.getElementById(id).innerHTML = currentMarker) - - -} - - - - + // changeMarker(document.getElementById(id).innerHTML = currentMarker) + checkForWin((document.getElementById(id).innerHTML = currentMarker)); +}; - - - - - -// This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" -const changeMarker = () => { - if(currentMarker === "X"){ - currentMarker = "O" +const checkForWin = () => { + if (horizontalWin() || verticalWin() || diagonalWin()) { + window.alert(`Player ${currentMarker} won!`); } else { - currentMarker = "X" + 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; + } + 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; + } +}; + +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; + } + 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; + } +}; + +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 +} 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 } +}; +const changeMarker = () => { + if (currentMarker === "X") { + currentMarker = "O"; + } else { + currentMarker = "X"; + } +}; - - - - - - - - -// 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++) { + const squares = document.getElementsByTagName("TD"); + 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) + 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 + squares[i].innerHTML = null; + + board = [ + ["", "", ""], + ["", "", ""], + ["", "", ""], + ]; + } +}; From 08b6cf4068f3e9180838ca3e71a06f2fbb7855af Mon Sep 17 00:00:00 2001 From: 4621553 Date: Wed, 1 Mar 2023 15:59:02 -0600 Subject: [PATCH 5/5] change code as instructed --- scripts.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts.js b/scripts.js index e0dc391..b1b29bc 100644 --- a/scripts.js +++ b/scripts.js @@ -25,7 +25,8 @@ const addMarker = (id) => { // changeMarker(document.getElementById(id).innerHTML = currentMarker) - checkForWin((document.getElementById(id).innerHTML = currentMarker)); + document.getElementById(id).innerHTML = currentMarker; + checkForWin(); }; const checkForWin = () => {