From 24ecc88cff682f7a0ca544a42e60d6ca0185f988 Mon Sep 17 00:00:00 2001 From: Nataponchobjai Date: Fri, 14 Jul 2023 01:16:19 -0500 Subject: [PATCH 1/2] worked on ttt --- index.html | 16 ++++++++++++++-- scripts.js | 11 ++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 19d0dee..5b7700d 100644 --- a/index.html +++ b/index.html @@ -20,8 +20,20 @@

Welcome to Tic Tac Toe

- - + + + + + + + + + + + + + + diff --git a/scripts.js b/scripts.js index ba09e74..065c3bc 100644 --- a/scripts.js +++ b/scripts.js @@ -18,9 +18,11 @@ 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('element:', 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 +console.log('!document.getElementById(element.id).innerHTML', !document.getElementById(element.id).innerHTML) if(!document.getElementById(element.id).innerHTML){ addMarker(element.id) } @@ -38,13 +40,17 @@ const handleClick = (element) => { // this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function. const addMarker = (id) => { + console.log('inside addMarker 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" - + // ger element + document.getElementById(id).innerHTML = currentMarker + // get the element innerHTML + // element.innerHTML = currentMarker // @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line: // = currentMarker // .getElementById(id) @@ -65,9 +71,12 @@ const addMarker = (id) => { // This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X" const changeMarker = () => { + console. log('IN CHANGMARKER currentMarker:', currentMarker) if(currentMarker === "X"){ + console.log('hello changing to O') currentMarker = "O" } else { + console.log('hello changing to X') currentMarker = "X" } } From 2167f7c9ea386e9d45b90d7833fa8880ca7a6b08 Mon Sep 17 00:00:00 2001 From: Nataponchobjai Date: Fri, 4 Aug 2023 00:53:27 -0500 Subject: [PATCH 2/2] done --- index.html | 26 ++++---- scripts.js | 161 +++++++++++++++++++------------------------------- tictactoe.css | 77 +++++++++++++++++++++--- 3 files changed, 140 insertions(+), 124 deletions(-) diff --git a/index.html b/index.html index 5b7700d..0ab4ab1 100644 --- a/index.html +++ b/index.html @@ -3,10 +3,10 @@ Tic Tac Toe - + - - + +
@@ -19,24 +19,22 @@

Welcome to Tic Tac Toe

- - - + + + - - - + + + - - - + + + - -
diff --git a/scripts.js b/scripts.js index 065c3bc..23279de 100644 --- a/scripts.js +++ b/scripts.js @@ -1,115 +1,74 @@ -// *********************** -// INSTRUCTIONS -// *********************** +let currentMarker = 'X'; -// 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' - - - - -// 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('element:', 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 -console.log('!document.getElementById(element.id).innerHTML', !document.getElementById(element.id).innerHTML) - if(!document.getElementById(element.id).innerHTML){ - addMarker(element.id) + if (!element.innerHTML) { + addMarker(element); + changeMarker(); } +}; + +const addMarker = (element) => { + const row = parseInt(element.id.charAt(0)); + const column = parseInt(element.id.charAt(2)); + board[row][column] = currentMarker; + element.innerHTML = currentMarker; + checkForWin(); } - - - - - - - - - - -// this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function. -const addMarker = (id) => { - console.log('inside addMarker 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" - // ger element - document.getElementById(id).innerHTML = currentMarker - // get the element innerHTML - // element.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 = () => { - console. log('IN CHANGMARKER currentMarker:', currentMarker) - if(currentMarker === "X"){ - console.log('hello changing to O') - currentMarker = "O" + if(currentMarker === "X") { + currentMarker = "O"; } else { - console.log('hello changing to X') - 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 - - // @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 (let square of squares) { + square.innerHTML = ""; + } + board = [ + ["", "", ""], + ["", "", ""], + ["", "", ""] + ]; +} - // will log out the id of each square as it loops over them. - console.log(squares[i].id) +const checkForWin = () => { + if(horizontalWin() || verticalWin() || diagonalWin()) { + window.alert(`Player ${currentMarker} won!`); + } +} - // sets the innerHTML to null to replace the "X" or "O" - squares[i].innerHTML = null - } -} \ No newline at end of file +const horizontalWin = () => { + for (let row = 0; row < 3; row++) { + if (board[row].every(cell => cell === currentMarker)) { + return true; + } + } + return false; +}; + +const verticalWin = () => { + for (let col = 0; col < 3; col++) { + if (board[0][col] === currentMarker && board[1][col] === currentMarker && board[2][col] === currentMarker) { + return true; + } + } + return false; +}; + +const diagonalWin = () => { + if ((board[0][0] === currentMarker && board[1][1] === currentMarker && board[2][2] === currentMarker) || + (board[0][2] === currentMarker && board[1][1] === currentMarker && board[2][0] === currentMarker)) { + return true; + } + return false; +}; diff --git a/tictactoe.css b/tictactoe.css index 981de05..397f97f 100644 --- a/tictactoe.css +++ b/tictactoe.css @@ -1,16 +1,75 @@ +.container { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: #f5f5f5; +} + .jumbotron { - margin: 5% auto; - margin-left: 43%; + text-align: center; + margin-bottom: 20px; + width: 400px; +} + +table { + border-collapse: collapse; + width: 300px; + height: 300px; + background-color: #ffffff; + box-shadow: 0px 0px 10px rgba(0,0,0,0.2); } td { - height:150px; - width:150px; + width: 33.33%; + height: 33.33%; text-align: center; - border: 5px solid black; - font-size: 100px; + vertical-align: middle; + font-size: 24px; + font-weight: bold; + border: 1px solid #ddd; + transition: background-color 0.3s; } -table { - margin: auto auto; -} \ No newline at end of file +td:hover { + background-color: #f0f0f0; +} + +.top-left, .middle-left, .bottom-left { + border-left: none; +} + +.top-left, .top-middle, .top-right { + border-top: none; +} + +.btn { + margin-top: 15px; + padding: 10px 20px; + background-color: #007BFF; + border: none; + color: #fff; + cursor: pointer; + border-radius: 5px; + transition: background-color 0.3s; +} + +.btn:hover { + background-color: #0056b3; +} + +@keyframes bounce { + 0%, 20%, 50%, 80%, 100% { + transform: translateY(0); + } + 40% { + transform: translateY(-15px); + } + 60% { + transform: translateY(-7px); + } +} + +.bounce-effect { + animation: bounce 1s; +}