Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermione69 committed Jul 9, 2024
0 parents commit 12de099
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
Binary file added 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tic-Tac-Toe</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section>
<h1 class="game--title">Tic-Tac-Toe</h1>
<div class="game--container">
<div data-cell-index="0" class="cell"></div>
<div data-cell-index="1" class="cell"></div>
<div data-cell-index="2" class="cell"></div>
<div data-cell-index="3" class="cell"></div>
<div data-cell-index="4" class="cell"></div>
<div data-cell-index="5" class="cell"></div>
<div data-cell-index="6" class="cell"></div>
<div data-cell-index="7" class="cell"></div>
<div data-cell-index="8" class="cell"></div>
</div>
<h2 class="game--status"></h2>
<button class="game--restart">Restart Game</button>
</section>
<script src="script.js"></script>
</body>
</html>
86 changes: 86 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const statusDisplay = document.querySelector('.game--status');

let gameActive = true;
let currentPlayer = "X";
let gameState = ["", "", "", "", "", "", "", "", ""];

const winningMessage = () => `Player ${currentPlayer} has won!`;
const drawMessage = () => `Game ended in a draw!`;
const currentPlayerTurn = () => `It's ${currentPlayer}'s turn`;

statusDisplay.innerHTML = currentPlayerTurn();

const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];

function handleCellPlayed(clickedCell, clickedCellIndex) {
gameState[clickedCellIndex] = currentPlayer;
clickedCell.innerHTML = currentPlayer;
}

function handlePlayerChange() {
currentPlayer = currentPlayer === "X" ? "O" : "X";
statusDisplay.innerHTML = currentPlayerTurn();
}

function handleResultValidation() {
let roundWon = false;
for(let i = 0; i <= 7; i++) {
const winCondition = winningConditions[i];
const a = gameState[winCondition[0]];
const b = gameState[winCondition[1]];
const c = gameState[winCondition[2]];
if(a === '' || b === '' || c === '')
continue;
if(a === b && b === c) {
roundWon = true;
break
}
}

if(roundWon) {
statusDisplay.innerHTML = winningMessage();
gameActive = false;
return;
}

const roundDraw = !gameState.includes("");
if(roundDraw) {
statusDisplay.innerHTML = drawMessage();
gameActive = false;
return;
}

handlePlayerChange();
}

function handleCellClick(clickedCellEvent) {
const clickedCell = clickedCellEvent.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-cell-index'));

if(gameState[clickedCellIndex] !== "" || !gameActive)
return;

handleCellPlayed(clickedCell, clickedCellIndex);
handleResultValidation();
}

function handleRestartGame() {
gameActive = true;
currentPlayer = "X";
gameState = ["", "", "", "", "", "", "", "", ""];
statusDisplay.innerHTML = currentPlayerTurn();
document.querySelectorAll('.cell').forEach(cell => cell.innerHTML = "");
}


document.querySelectorAll('.cell').forEach(cell => cell.addEventListener('click', handleCellClick));
document.querySelector('.game--restart').addEventListener('click', handleRestartGame);
49 changes: 49 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
body {
font-family: "Arial", sans-serif;
}

section {
text-align: center;
}

.cell {
font-family: "Permanent Marker", cursive;
width: 100px;
height: 100px;
box-shadow: 2px 2px 2px 2px #ecd7ba;
border: 2px solid #ecd7ba;
cursor: pointer;
line-height: 100px;
font-size: 60px;
}

.game--title {
font-size: 100px;
color: #d7a62f;
margin: 10px auto;
}

.game--container {
display: grid;
grid-template-columns: repeat(3, auto);
width: 306px;
margin: 10px auto;
background-color: #11213a;
color: #04c0b2;
}

.game--status {
font-size: 50px;
color: #d7a62f;
margin: 20px auto;
}

.game--restart {
background-color: #f7e4ac;
width: 200px;
height: 50px;
font-size: 25px;
color: #5586e2;
box-shadow: 2px 2px 2px 2px #d86c23;
border: 2px solid #d86c23;
}

0 comments on commit 12de099

Please sign in to comment.