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

added tic-tac-toe game #768

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions tic tac toe game/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">
<link rel="shortcut icon" href="tic-tac-toe.png" type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Tic Tac Toe</title>
</head>
<body>
<div class="container">
<audio id="moveSound" src="sound.mp3"></audio>
<h1 class="title">Tic Tac Toe</h1>
<div class="board">
<div class="cell" onclick="makeMove(0)"></div>
<div class="cell" onclick="makeMove(1)"></div>
<div class="cell" onclick="makeMove(2)"></div>
<div class="cell" onclick="makeMove(3)"></div>
<div class="cell" onclick="makeMove(4)"></div>
<div class="cell" onclick="makeMove(5)"></div>
<div class="cell" onclick="makeMove(6)"></div>
<div class="cell" onclick="makeMove(7)"></div>
<div class="cell" onclick="makeMove(8)"></div>
</div>
<div class="message"></div>
<button class="reset-button" onclick="resetBoard()">Reset</button>
</div>
<script src="script.js"></script>
</body>
</html>
73 changes: 73 additions & 0 deletions tic tac toe game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
let currentPlayer = 'X';
let gameBoard = ['', '', '', '', '', '', '', '', ''];
let gameActive = true;

function makeMove(cellIndex) {
if (gameBoard[cellIndex] === '' && gameActive) {
gameBoard[cellIndex] = currentPlayer;
document.getElementsByClassName('cell')[cellIndex].textContent = currentPlayer;
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
updateMessage();
checkWin();
playMoveSound(); // Call the function to play the sound
}
}

function playMoveSound() {
const moveSound = document.getElementById('moveSound');
moveSound.play();
}


function updateMessage() {
const message = document.querySelector('.message');
if (gameActive) {
message.textContent = `Current turn: ${currentPlayer}`;
} else {
if (!gameBoard.includes('') && !checkWinner('X') && !checkWinner('O')) {
message.textContent = "It's a draw!";
} else {
message.textContent = `${currentPlayer === 'X' ? 'O' : 'X'} wins!`;
}
}
}

function checkWin() {
if (checkWinner('X') || checkWinner('O')) {
gameActive = false;
} else if (!gameBoard.includes('')) {
gameActive = false;
}
updateMessage();
}

function checkWinner(player) {
const winningCombos = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];

for (let combo of winningCombos) {
const [a, b, c] = combo;
if (gameBoard[a] === player && gameBoard[b] === player && gameBoard[c] === player) {
return true;
}
}
return false;
}

function resetBoard() {
currentPlayer = 'X';
gameBoard = ['', '', '', '', '', '', '', '', ''];
gameActive = true;

const cells = document.getElementsByClassName('cell');
for (let cell of cells) {
cell.textContent = '';
}

updateMessage();
}

resetBoard();
Binary file added tic tac toe game/sound.mp3
Binary file not shown.
92 changes: 92 additions & 0 deletions tic tac toe game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f2f2f2;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}

.container {
width: 100%;
max-width: 400px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.title {
font-size: 30px;
margin-bottom: 20px;
color: #333;
}

.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
width: 100%;
max-width: 300px;
margin: 0 auto;
margin-bottom: 20px;
}

.cell {
width: 100%;
padding: 0;
font-size: 24px;
text-align: center;
background-color: #eee;
cursor: pointer;
border: 2px solid #ddd;
border-radius: 5px;
transition: background-color 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
height: 70px; /* Adjust the height as needed */
}

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

.message {
font-size: 18px;
margin-bottom: 10px;
color: #333;
}

.reset-button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px;
cursor: pointer;
border-radius: 5px;
}

.reset-button:hover {
background-color: #45a049;
}

/* Media query for smaller screens (phones) */
@media (max-width: 600px) {
.container {
margin: 0 20px; /* Add margin on the X-axis for smaller screens */
}
}
Binary file added tic tac toe game/tic-tac-toe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.