-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1e4a6bf
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!DOCTYPE html> | ||
|
||
<head> | ||
|
||
<title>Tic Tac Toe</title> | ||
|
||
<link rel="icon" href="Images/Icon.jpg"> | ||
|
||
<link rel="stylesheet" href="style.css"> | ||
|
||
</head> | ||
|
||
<body> | ||
|
||
<h1>Tic-Tac-Toe</h1> | ||
|
||
<hr> | ||
|
||
<div class="winner hidden"> | ||
|
||
<p id="winner"></p> | ||
|
||
</div> | ||
|
||
<hr> | ||
|
||
<div class="main-box"> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
<button class="box"></button> | ||
|
||
</div> | ||
|
||
<br> | ||
|
||
<div class="reset"> | ||
|
||
<button id="reset">RESET</button> | ||
|
||
</div> | ||
|
||
</body> | ||
|
||
<script src="script.js"></script> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
let boxes = document.querySelectorAll(".box"); | ||
let reset = document.querySelector("#reset"); | ||
let winner_box = document.querySelector(".winner"); | ||
let winner_result = document.querySelector("#winner"); | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
let turn_x = true; | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const winning_patterns = [ | ||
[0, 1, 2], | ||
[3, 4, 5], | ||
[6, 7, 8], | ||
[0, 3, 6], | ||
[1, 4, 7], | ||
[2, 5, 8], | ||
[0, 4, 8], | ||
[2, 4, 6] | ||
] | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
boxes.forEach((box) => { | ||
box.addEventListener("click", () => { | ||
if (turn_x) { | ||
box.innerText = "X"; | ||
turn_x = false; | ||
} | ||
else { | ||
box.innerText = "O"; | ||
turn_x = true; | ||
} | ||
box.disabled = true; | ||
check_winner(); | ||
}) | ||
}) | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const disable_boxes = () => { | ||
for (let box of boxes) { | ||
box.disabled = true; | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const enable_boxes = () => { | ||
for (let box of boxes) { | ||
box.disabled = false; | ||
box.innerText = ""; | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const check_winner = () => { | ||
for (let pattern of winning_patterns) { | ||
let position_1 = boxes[pattern[0]].innerText; | ||
let position_2 = boxes[pattern[1]].innerText; | ||
let position_3 = boxes[pattern[2]].innerText; | ||
|
||
if (position_1 != "" && position_2 != "" && position_3 != "") { | ||
if (position_1 === position_2 && position_2 === position_3) { | ||
show_winner(position_1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const show_winner = (winner) => { | ||
winner_result.innerText = `${winner} Wins!`; | ||
winner_box.classList.remove("hidden"); | ||
disable_boxes(); | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
const reset_game = () => { | ||
turn_x = true; | ||
enable_boxes(); | ||
winner_box.classList.add("hidden"); | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------- | ||
|
||
reset.addEventListener("click", () => { | ||
reset_game(); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
* { | ||
margin: 0px; | ||
padding: 0px; | ||
box-sizing: border-box; | ||
font-family: 'Times New Roman'; | ||
font-weight: bold; | ||
} | ||
|
||
body { | ||
background-color: grey; | ||
} | ||
|
||
h1 { | ||
height: auto; | ||
width: auto; | ||
font-size: 3rem; | ||
text-align: center; | ||
text-decoration: underline; | ||
margin-top: 3vh; | ||
} | ||
|
||
hr { | ||
margin: 0.5rem 0rem; | ||
} | ||
|
||
.winner { | ||
height: auto; | ||
width: auto; | ||
text-align: center; | ||
font-size: 2rem; | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} | ||
|
||
.main-box { | ||
border: 10px solid rgb(25, 25, 25); | ||
border-radius: 25px; | ||
height: 24rem; | ||
width: 24rem; | ||
margin: auto; | ||
background-color: grey; | ||
box-shadow: inset 3px 3px 50px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
flex-wrap: wrap; | ||
} | ||
|
||
.box { | ||
border-width: 0.5rem; | ||
border-color: rgb(50, 50, 50); | ||
height: 6rem; | ||
width: 6rem; | ||
background-color: rgb(220, 220, 220); | ||
margin: 0.5rem; | ||
cursor: pointer; | ||
font-size: 3rem; | ||
} | ||
|
||
.box:hover { | ||
box-shadow: inset 3px 3px 50px; | ||
} | ||
|
||
.reset { | ||
text-align: center; | ||
} | ||
|
||
#reset { | ||
border-width: 0.5rem; | ||
height: auto; | ||
width: auto; | ||
background-color: rgb(220, 220, 220); | ||
padding: 1rem; | ||
font-size: 1rem; | ||
cursor: pointer; | ||
} | ||
|
||
#reset:hover { | ||
box-shadow: inset 3px 3px 50px; | ||
} |