Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tic-Tac-Toe game on DAY-25 #19

Merged
merged 2 commits into from
Jun 17, 2024
Merged
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
40 changes: 40 additions & 0 deletions public/TicTacToe/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://unpkg.com/shepherd.js/dist/css/shepherd.css">
<script src="https://unpkg.com/shepherd.js/dist/js/shepherd.min.js"></script>
</head>
<body>
<div class="container">
<h1>Tic Tac Toe</h1>
<div class="board" id="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<button id="restart">Restart Game</button>
</div>

<!-- Modal for Win/Draw Screen -->
<div class="modal" id="resultModal">
<div class="modal-content">
<span class="close" id="closeModal">&times;</span>
<p id="resultMessage"></p>
<button id="newGame">New Game</button>
</div>
</div>

<script src="script.js"></script>
<script src="tour.js"></script>
</body>
</html>
79 changes: 79 additions & 0 deletions public/TicTacToe/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
document.addEventListener('DOMContentLoaded', () => {
const board = document.getElementById('board');
const cells = Array.from(document.querySelectorAll('.cell'));
const restartButton = document.getElementById('restart');
const modal = document.getElementById('resultModal');
const resultMessage = document.getElementById('resultMessage');
const closeModal = document.getElementById('closeModal');
const newGameButton = document.getElementById('newGame');
let currentPlayer = 'X';
let gameActive = true;
let boardState = Array(9).fill('');

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 handleCellClick(e) {
const cell = e.target;
const index = cell.getAttribute('data-index');

if (boardState[index] !== '' || !gameActive) {
return;
}

cell.textContent = currentPlayer;
boardState[index] = currentPlayer;

if (checkWin()) {
gameActive = false;
showResult(`${currentPlayer} wins!`);
return;
}

if (boardState.every(cell => cell !== '')) {
gameActive = false;
showResult('Draw!');
return;
}

currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}

function checkWin() {
return winningConditions.some(condition => {
return condition.every(index => boardState[index] === currentPlayer);
});
}

function showResult(message) {
resultMessage.textContent = message;
modal.style.display = 'block';
}

function restartGame() {
boardState = Array(9).fill('');
cells.forEach(cell => cell.textContent = '');
currentPlayer = 'X';
gameActive = true;
modal.style.display = 'none';
}

cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartButton.addEventListener('click', restartGame);
closeModal.addEventListener('click', () => { modal.style.display = 'none'; });
newGameButton.addEventListener('click', restartGame);

window.addEventListener('click', (event) => {
if (event.target == modal) {
modal.style.display = 'none';
}
});
});
85 changes: 85 additions & 0 deletions public/TicTacToe/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}

body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, rgb(46, 46, 46), rgb(60, 59, 59));
}

.container {
text-align: center;
}

h1 {
margin-bottom: 20px;
color: white;
}

.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin-bottom: 20px;
}

.cell {
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
border: 1px solid #ccc;
font-size: 2em;
cursor: pointer;
}

.cell:hover {
background-color: #e0e0e0;
}

button {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
}

.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}

.modal-content {
background-color: #fff;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 300px;
text-align: center;
}

.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}

.close:hover,
.close:focus {
color: black;
}
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
<div class="table">DAY-21.] Video bg slider using react <a href="https://github.com/dhairyagothi/50_days_50_web_project/tree/Main/public/travel_website">DEMO</a></div>
<div class="table">DAY-22.] Page loader <a href="./pageloader/pageloader.html">DEMO</a></div>
<div class="table">DAY-24.] Chat Bot <a href="./AI ChatBot/chatbot.html">DEMO</a></div>
<div class="table">DAY-25.] Maze game <a href="./Maze-Game-main/index.html">DEMO</a></div>
<div class="table">DAY-25.] Tic-Tac-Toe<a href="TicTacToe/index.html">DEMO</a></div>
<div class="table">DAY-26.] Maze game <a href="./Maze-Game-main/index.html">DEMO</a></div>


</div>

<script>
Expand Down