-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic4.html
154 lines (135 loc) · 4.6 KB
/
tic4.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Super Tic Tac Toe</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom right, #81c784, #4caf50);
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
#gameContainer {
width: 600px;
height: 600px;
display: flex;
justify-content: center;
align-items: center;
border: 4px solid rgba(255, 255, 255, 0.2);
border-radius: 20px;
position: relative;
}
canvas {
width: 100%;
height: 100%;
}
#turnIndicator {
margin-top: 20px;
font-size: 24px;
color: #fff;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="ticTacToeCanvas" width="600" height="600"></canvas>
</div>
<div id="turnIndicator">Current Turn: X</div>
<script>
const canvas = document.getElementById("ticTacToeCanvas");
const ctx = canvas.getContext("2d");
const cellSize = 200;
let currentPlayer = "X";
let board = Array.from({ length: 9 }, () => Array.from({ length: 9 }, () => ""));
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 4;
ctx.strokeStyle = "rgba(255, 255, 255, 0.2)";
for (let i = 1; i < 9; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
}
function drawX(x, y) {
ctx.strokeStyle = "#fff";
ctx.lineWidth = 6;
const offsetX = 20;
const offsetY = 20;
ctx.beginPath();
ctx.moveTo(x * cellSize + offsetX, y * cellSize + offsetY);
ctx.lineTo(
(x + 1) * cellSize - offsetX,
(y + 1) * cellSize - offsetY
);
ctx.moveTo(
(x + 1) * cellSize - offsetX,
y * cellSize + offsetY
);
ctx.lineTo(
x * cellSize + offsetX,
(y + 1) * cellSize - offsetY
);
ctx.stroke();
}
function drawO(x, y) {
ctx.strokeStyle = "#fff";
ctx.lineWidth = 6;
const centerX = x * cellSize + cellSize / 2;
const centerY = y * cellSize + cellSize / 2;
const radius = cellSize / 2 - 20;
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
ctx.stroke();
}
function checkWinner() {
// Check for winner logic here
}
canvas.addEventListener("click", event => {
const x = Math.floor(event.offsetX / cellSize);
const y = Math.floor(event.offsetY / cellSize);
if (board[y][x] === "" && isValidMove(x, y)) {
board[y][x] = currentPlayer;
currentPlayer = currentPlayer === "X" ? "O" : "X";
drawBoard();
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === "X") {
drawX(j, i);
} else if (board[i][j] === "O") {
drawO(j, i);
}
}
}
const winner = checkWinner();
if (winner) {
alert(`${winner} wins!`);
resetGame();
}
document.getElementById("turnIndicator").innerText = `Current Turn: ${currentPlayer}`;
}
});
function isValidMove(x, y) {
// Check if the move is valid based on the last move
return true; // Implement your logic here
}
function resetGame() {
board = Array.from({ length: 9 }, () => Array.from({ length: 9 }, () => ""));
currentPlayer = "X";
drawBoard();
}
drawBoard();
</script>
</body>
</html>