Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"liveServer.settings.port": 5501
"liveServer.settings.port": 5502
}
32 changes: 26 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@
<meta charset="utf-8">
<title>Tic Tac Toe</title>
<!-- link to CSS code -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Honk&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poor+Story&display=swap" rel="stylesheet">
<link rel="stylesheet" href="tictactoe.css">
<!-- link to JavaScript code -->
<script src="scripts.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Welcome to Tic Tac Toe</h1>
<p>Get ready to play!</p>
<h1>Welcome to Tic Tac Toe!</h1>
<p>Get ready to play</p>
<!-- uses the built-in onclick attribute to call the resetBoard function -->
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart</button>
<div class="reset-button">
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart</button>
</div>
</div>
<table>
<tr>
<!-- the onclick calls a function called "handleClick" and passes itself to it -->
<td id='top-left' onclick="handleClick(this)" ></td>
<td id='top-middle'></td>
<td></td>
<td id='0-0' onclick="handleClick(this)" ></td>
<td id='0-1' onclick="handleClick(this)" ></td>
<td id='0-2' onclick="handleClick(this)" ></td>
</tr>
<!-- @TODO create new rows and cells for the rest of your TTT board. -->

<tr>
<td id='1-0' onclick="handleClick(this)" ></td>
<td id='1-1' onclick="handleClick(this)" ></td>
<td id='1-2' onclick="handleClick(this)" ></td>
</tr>

<tr>
<td id='2-0' onclick="handleClick(this)" ></td>
<td id='2-1' onclick="handleClick(this)" ></td>
<td id='2-2' onclick="handleClick(this)" ></td>
</tr>
<!-- @TODO give each new cell an id & an event listener to call the "handleClick" function -->
</table>
</div>
Expand Down
94 changes: 56 additions & 38 deletions scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
// 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 = [
["", "", ""],
["", "", ""],
["", "", ""]
]

// 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
Expand All @@ -26,43 +29,28 @@ const handleClick = (element) => {
}
}











// this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function.
const addMarker = (id) => {

const row = parseInt(id.charAt(0))
const column = parseInt(id.charAt(2))
board[row][column] = currentMarker

// @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"

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()
checkForWin()
}










// This "changeMarker" function changes "X" to "O" in the "currentMarker" variable or "O" to "X"
const changeMarker = () => {
if(currentMarker === "X"){
Expand All @@ -72,21 +60,12 @@ 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")
Expand All @@ -95,12 +74,51 @@ const resetBoard = () => {
// 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)

// sets the innerHTML to null to replace the "X" or "O"
squares[i].innerHTML = null
}
console.log("Board has been reset!")
}

//Reset board array
board = [
["", "", ""],
["", "", ""],
["", "", ""]
]

console.log("Board has been reset!")
}

const checkForWin = () => {
if(horizontalWin() || verticalWin() || diagonalWin()) {
setTimeout(() => {
window.alert(`Player ${currentMarker} won!`)
}, 100);
} else {
changeMarker()
}
}

const horizontalWin = () => {
return (
(board[0][0] === currentMarker && board[0][1] === currentMarker && board[0][2] === currentMarker) ||
(board[1][0] === currentMarker && board[1][1] === currentMarker && board[1][2] === currentMarker) ||
(board[2][0] === currentMarker && board[2][1] === currentMarker && board[2][2] === currentMarker)
)
}
const verticalWin = () => {
return (
(board[0][0] === currentMarker && board[1][0] === currentMarker && board[2][0] === currentMarker) ||
(board[0][1] === currentMarker && board[1][1] === currentMarker && board[2][1] === currentMarker) ||
(board[0][2] === currentMarker && board[1][2] === currentMarker && board[2][2] === currentMarker)
)
}
const diagonalWin = () => {
return (
(board[0][0] === currentMarker && board[1][1] === currentMarker && board[2][2] === currentMarker) ||
(board[0][2] === currentMarker && board[1][1] === currentMarker && board[2][0] === currentMarker)
)
}
33 changes: 32 additions & 1 deletion tictactoe.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
body {
background-color: rgb(47, 147, 0);
}

.jumbotron {
margin: 5% auto;
margin-left: 43%;
/* margin-left: 43%; */
text-align: center;
}

.jumbotron h1 {
font-family: 'Honk', system-ui;
font-size: 100px;
}

.jumbotron p {
font-family: 'Poor Story', system-ui;
font-size: 50px;
}

.reset-button button {
font-size: 50px;
font-family: 'Poor Story', system-ui;
border: none;
outline: none;
color: black;
background-color: transparent;
}

.reset-button button:hover {
cursor: pointer;
font-size: 55px;
color: blue;
}

td {
Expand All @@ -9,6 +39,7 @@ td {
text-align: center;
border: 5px solid black;
font-size: 100px;
font-family: Arial, sans-serif
}

table {
Expand Down