forked from akkupy/codeDump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.html
116 lines (110 loc) · 3.38 KB
/
tic-tac-toe.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!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>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(to bottom right, #333, #000);
color: #fff;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 2px;
background-color: #222;
border-radius: 5px;
position: relative;
user-select: none; /* Disable text selection on the board */
}
.cell {
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
cursor: pointer;
border: 1px solid #444;
}
.cell:hover {
background-color: #444;
}
.cell::after {
content: "";
position: absolute;
width: 100%;
height: 100%;
border: 1px solid #444;
pointer-events: none;
}
</style>
</head>
<body>
<div class="board" id="board" oncontextmenu="return false;">
<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>
<!-- Bootstrap JS and Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
let currentPlayer = 'X';
const cells = document.querySelectorAll('.cell');
const board = document.getElementById('board');
// Function to handle player moves
function makeMove(index) {
if (!cells[index].textContent) {
cells[index].textContent = currentPlayer;
if (checkWin()) {
setTimeout(() => {
alert(currentPlayer + " wins!");
resetBoard();
}, 100);
} else if (checkDraw()) {
setTimeout(() => {
alert("It's a draw!");
resetBoard();
}, 100);
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
}
// Function to check if any player has won
function checkWin() {
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Horizontal
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Vertical
[0, 4, 8], [2, 4, 6] // Diagonal
];
return winPatterns.some(pattern =>
pattern.every(index => cells[index].textContent === currentPlayer)
);
}
// Function to check if it's a draw
function checkDraw() {
return [...cells].every(cell => cell.textContent !== '');
}
// Function to reset the board
function resetBoard() {
cells.forEach(cell => cell.textContent = '');
currentPlayer = 'X';
}
</script>
</body>
</html>