@tsparticles/template-tictactoe 4.1.3 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +3 -0
- package/package.json +4 -3
- package/template/angular/package.json +34 -0
- package/template/angular/src/app/app.component.css +97 -0
- package/template/angular/src/app/app.component.html +21 -0
- package/template/angular/src/app/app.component.ts +94 -0
- package/template/angular-confetti/package.json +34 -0
- package/template/angular-confetti/src/app/app.component.css +97 -0
- package/template/angular-confetti/src/app/app.component.html +21 -0
- package/template/angular-confetti/src/app/app.component.ts +94 -0
- package/template/angular-fireworks/package.json +34 -0
- package/template/angular-fireworks/src/app/app.component.css +97 -0
- package/template/angular-fireworks/src/app/app.component.html +21 -0
- package/template/angular-fireworks/src/app/app.component.ts +94 -0
- package/template/astro/package.json +15 -0
- package/template/astro/src/pages/index.astro +158 -0
- package/template/ember/app/controllers/application.js +94 -0
- package/template/ember/app/templates/application.hbs +39 -0
- package/template/ember/package.json +17 -0
- package/template/inferno/package.json +20 -0
- package/template/inferno/src/App.css +97 -0
- package/template/inferno/src/App.tsx +149 -0
- package/template/jquery/package.json +20 -0
- package/template/jquery/src/main.ts +126 -0
- package/template/jquery/src/style.css +97 -0
- package/template/lit/package.json +19 -0
- package/template/lit/src/my-app.ts +133 -0
- package/template/nextjs/package.json +20 -0
- package/template/nextjs/src/app/page.tsx +173 -0
- package/template/nextjs/src/app/providers.tsx +13 -0
- package/template/nuxt2/package.json +14 -0
- package/template/nuxt2/pages/index.vue +203 -0
- package/template/nuxt3/app.vue +204 -0
- package/template/nuxt3/package.json +16 -0
- package/template/nuxt4/app.vue +202 -0
- package/template/nuxt4/package.json +16 -0
- package/template/preact/package.json +20 -0
- package/template/preact/src/App.css +97 -0
- package/template/preact/src/App.tsx +141 -0
- package/template/qwik/package.json +19 -0
- package/template/qwik/src/App.tsx +132 -0
- package/template/react/package.json +19 -0
- package/template/react/src/App.css +97 -0
- package/template/react/src/App.tsx +141 -0
- package/template/riot/package.json +20 -0
- package/template/riot/src/app.riot +139 -0
- package/template/solid/package.json +18 -0
- package/template/solid/src/App.tsx +151 -0
- package/template/solid/src/index.css +97 -0
- package/template/solid/src/main.tsx +9 -0
- package/template/stencil/package.json +14 -0
- package/template/stencil/src/components/app-home/app-home.css +97 -0
- package/template/stencil/src/components/app-home/app-home.tsx +122 -0
- package/template/svelte/package.json +20 -0
- package/template/svelte/src/App.svelte +256 -0
- package/template/svelte/src/main.ts +12 -0
- package/template/vanilla/LICENSE +21 -0
- package/template/vue2/package.json +19 -0
- package/template/vue2/src/App.vue +204 -0
- package/template/vue3/package.json +19 -0
- package/template/vue3/src/App.vue +215 -0
- package/template/vue3/src/main.ts +14 -0
- package/template/webcomponents/package.json +18 -0
- package/template/webcomponents/src/main.ts +134 -0
- package/template/webcomponents/src/style.css +97 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Component } from "@angular/core";
|
|
2
|
+
import { confetti } from "@tsparticles/confetti";
|
|
3
|
+
import type { ISourceOptions } from "@tsparticles/engine";
|
|
4
|
+
|
|
5
|
+
type Player = "X" | "O";
|
|
6
|
+
|
|
7
|
+
const WIN_PATTERNS = [
|
|
8
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
9
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
10
|
+
[0, 4, 8], [2, 4, 6],
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
@Component({
|
|
14
|
+
selector: "app-root",
|
|
15
|
+
templateUrl: "./app.component.html",
|
|
16
|
+
styleUrls: ["./app.component.css"],
|
|
17
|
+
})
|
|
18
|
+
export class AppComponent {
|
|
19
|
+
board: (Player | null)[] = Array(9).fill(null);
|
|
20
|
+
currentPlayer: Player = "X";
|
|
21
|
+
winner: Player | "draw" | null = null;
|
|
22
|
+
winLine: number[] | null = null;
|
|
23
|
+
scores = { X: 0, O: 0, draw: 0 };
|
|
24
|
+
|
|
25
|
+
particlesOptions: ISourceOptions = {
|
|
26
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
27
|
+
background: { color: { value: "#0a0a1a" } },
|
|
28
|
+
fpsLimit: 60,
|
|
29
|
+
particles: {
|
|
30
|
+
number: { value: 30, density: { enable: true } },
|
|
31
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
32
|
+
shape: { type: "circle" },
|
|
33
|
+
opacity: { value: 0.2, random: true },
|
|
34
|
+
size: { value: { min: 1, max: 2 } },
|
|
35
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
36
|
+
},
|
|
37
|
+
detectRetina: true,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
get status(): string {
|
|
41
|
+
if (!this.winner) return `${this.currentPlayer}'s turn`;
|
|
42
|
+
if (this.winner === "draw") return "It's a draw!";
|
|
43
|
+
return `${this.winner} wins!`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
47
|
+
for (const pattern of WIN_PATTERNS) {
|
|
48
|
+
const [a, b, c] = pattern;
|
|
49
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
50
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw" as const, winLine: [] };
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fireConfettiEffect(): void {
|
|
58
|
+
const count = 200;
|
|
59
|
+
const defaults = { origin: { y: 0.7 } };
|
|
60
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
61
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
62
|
+
};
|
|
63
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
64
|
+
fire(0.2, { spread: 60 });
|
|
65
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
66
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
67
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
handleMove(index: number): void {
|
|
71
|
+
if (this.board[index] || this.winner) return;
|
|
72
|
+
const board = [...this.board];
|
|
73
|
+
board[index] = this.currentPlayer;
|
|
74
|
+
const result = this.checkWin(board);
|
|
75
|
+
if (result) {
|
|
76
|
+
this.board = board;
|
|
77
|
+
this.winner = result.winner;
|
|
78
|
+
this.winLine = result.winLine;
|
|
79
|
+
if (result.winner === "draw") this.scores.draw++;
|
|
80
|
+
else this.scores[result.winner]++;
|
|
81
|
+
if (result.winner !== "draw") setTimeout(() => this.fireConfettiEffect(), 100);
|
|
82
|
+
} else {
|
|
83
|
+
this.board = board;
|
|
84
|
+
this.currentPlayer = this.currentPlayer === "X" ? "O" : "X";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
handleReset(): void {
|
|
89
|
+
this.board = Array(9).fill(null);
|
|
90
|
+
this.currentPlayer = "X";
|
|
91
|
+
this.winner = null;
|
|
92
|
+
this.winLine = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "astro dev",
|
|
8
|
+
"build": "astro build",
|
|
9
|
+
"preview": "astro preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"astro": "^5.0.0",
|
|
13
|
+
"@tsparticles/astro": "^4.1.3"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
---
|
|
2
|
+
import Particles from "@tsparticles/astro";
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<!doctype html>
|
|
6
|
+
<html lang="en">
|
|
7
|
+
<head>
|
|
8
|
+
<meta charset="UTF-8" />
|
|
9
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
10
|
+
<title>Tic-Tac-Toe</title>
|
|
11
|
+
<style>
|
|
12
|
+
* { box-sizing: border-box; }
|
|
13
|
+
body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #0a0a1a; font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; color: #fff; }
|
|
14
|
+
#app { text-align: center; z-index: 10; padding: 1rem; }
|
|
15
|
+
h1 { font-size: 2.5em; margin-bottom: 1rem; background: linear-gradient(135deg, #6c5ce7, #fd79a8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
|
|
16
|
+
.scoreboard { display: flex; justify-content: center; gap: 2rem; margin-bottom: 1rem; font-size: 1.1em; opacity: 0.8; }
|
|
17
|
+
.turn { font-size: 1.2em; margin-bottom: 1rem; opacity: 0.7; }
|
|
18
|
+
.board { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 4px; background: rgba(255,255,255,0.1); border-radius: 12px; overflow: hidden; margin: 0 auto; width: fit-content; }
|
|
19
|
+
.cell { display: flex; align-items: center; justify-content: center; font-size: 2.5em; font-weight: 700; background: #12122a; cursor: pointer; transition: background 0.2s; user-select: none; }
|
|
20
|
+
.cell:hover { background: #1a1a3a; }
|
|
21
|
+
.cell.taken { cursor: default; }
|
|
22
|
+
.cell.win { background: rgba(108,92,231,0.3); }
|
|
23
|
+
.reset-btn { margin-top: 1.5rem; padding: 0.7em 2em; font-size: 1em; font-weight: 600; border: none; border-radius: 8px; background: linear-gradient(135deg, #6c5ce7, #a29bfe); color: #fff; cursor: pointer; transition: transform 0.2s; }
|
|
24
|
+
.reset-btn:hover { transform: scale(1.05); }
|
|
25
|
+
</style>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<Particles id="tsparticles" options={{
|
|
29
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
30
|
+
background: { color: { value: "#0a0a1a" } },
|
|
31
|
+
fpsLimit: 60,
|
|
32
|
+
particles: {
|
|
33
|
+
number: { value: 30, density: { enable: true } },
|
|
34
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
35
|
+
shape: { type: "circle" },
|
|
36
|
+
opacity: { value: 0.2, random: true },
|
|
37
|
+
size: { value: { min: 1, max: 2 } },
|
|
38
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
39
|
+
},
|
|
40
|
+
detectRetina: true,
|
|
41
|
+
}} client:load />
|
|
42
|
+
|
|
43
|
+
<div id="app">
|
|
44
|
+
<h1>Tic-Tac-Toe</h1>
|
|
45
|
+
<div class="scoreboard">
|
|
46
|
+
<span>X: <strong id="scoreX">0</strong></span>
|
|
47
|
+
<span>Draws: <strong id="scoreDraw">0</strong></span>
|
|
48
|
+
<span>O: <strong id="scoreO">0</strong></span>
|
|
49
|
+
</div>
|
|
50
|
+
<div class="turn" id="turn">X's turn</div>
|
|
51
|
+
<div class="board" id="board">
|
|
52
|
+
<div class="cell" data-index="0"></div>
|
|
53
|
+
<div class="cell" data-index="1"></div>
|
|
54
|
+
<div class="cell" data-index="2"></div>
|
|
55
|
+
<div class="cell" data-index="3"></div>
|
|
56
|
+
<div class="cell" data-index="4"></div>
|
|
57
|
+
<div class="cell" data-index="5"></div>
|
|
58
|
+
<div class="cell" data-index="6"></div>
|
|
59
|
+
<div class="cell" data-index="7"></div>
|
|
60
|
+
<div class="cell" data-index="8"></div>
|
|
61
|
+
</div>
|
|
62
|
+
<button class="reset-btn" id="resetBtn">New Game</button>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<script>
|
|
66
|
+
import { confetti } from "@tsparticles/confetti";
|
|
67
|
+
|
|
68
|
+
const WIN_PATTERNS = [
|
|
69
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
70
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
71
|
+
[0, 4, 8], [2, 4, 6],
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
let board = Array(9).fill(null);
|
|
75
|
+
let currentPlayer = "X";
|
|
76
|
+
let winner = null;
|
|
77
|
+
let winLine = null;
|
|
78
|
+
let scores = { X: 0, O: 0, draw: 0 };
|
|
79
|
+
|
|
80
|
+
function checkWin(b) {
|
|
81
|
+
for (const pattern of WIN_PATTERNS) {
|
|
82
|
+
const [a, b_, c] = pattern;
|
|
83
|
+
if (b[a] && b[a] === b[b_] && b[a] === b[c]) {
|
|
84
|
+
return { winner: b[a], winLine: pattern };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (b.every((cell) => cell !== null)) return { winner: "draw", winLine: [] };
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fireConfettiEffect() {
|
|
92
|
+
const count = 200;
|
|
93
|
+
const defaults = { origin: { y: 0.7 } };
|
|
94
|
+
const fire = (particleRatio, opts) => {
|
|
95
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
96
|
+
};
|
|
97
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
98
|
+
fire(0.2, { spread: 60 });
|
|
99
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
100
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
101
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function render() {
|
|
105
|
+
document.querySelectorAll(".cell").forEach((cell, i) => {
|
|
106
|
+
cell.textContent = board[i] ?? "";
|
|
107
|
+
cell.classList.toggle("taken", board[i] !== null);
|
|
108
|
+
cell.classList.toggle("win", winLine?.includes(i) ?? false);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const turnEl = document.getElementById("turn");
|
|
112
|
+
if (!winner) turnEl.textContent = `${currentPlayer}'s turn`;
|
|
113
|
+
else if (winner === "draw") turnEl.textContent = "It's a draw!";
|
|
114
|
+
else turnEl.textContent = `${winner} wins!`;
|
|
115
|
+
|
|
116
|
+
document.getElementById("scoreX").textContent = String(scores.X);
|
|
117
|
+
document.getElementById("scoreO").textContent = String(scores.O);
|
|
118
|
+
document.getElementById("scoreDraw").textContent = String(scores.draw);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function handleMove(index) {
|
|
122
|
+
if (board[index] || winner) return;
|
|
123
|
+
const b = [...board];
|
|
124
|
+
b[index] = currentPlayer;
|
|
125
|
+
const result = checkWin(b);
|
|
126
|
+
if (result) {
|
|
127
|
+
board = b;
|
|
128
|
+
winner = result.winner;
|
|
129
|
+
winLine = result.winLine;
|
|
130
|
+
if (result.winner === "draw") scores.draw++;
|
|
131
|
+
else scores[result.winner]++;
|
|
132
|
+
if (result.winner !== "draw") setTimeout(fireConfettiEffect, 100);
|
|
133
|
+
} else {
|
|
134
|
+
board = b;
|
|
135
|
+
currentPlayer = currentPlayer === "X" ? "O" : "X";
|
|
136
|
+
}
|
|
137
|
+
render();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
document.getElementById("board").addEventListener("click", (e) => {
|
|
141
|
+
const cell = e.target.closest(".cell");
|
|
142
|
+
if (!cell) return;
|
|
143
|
+
const index = parseInt(cell.dataset.index, 10);
|
|
144
|
+
if (!isNaN(index)) handleMove(index);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
document.getElementById("resetBtn").addEventListener("click", () => {
|
|
148
|
+
board = Array(9).fill(null);
|
|
149
|
+
currentPlayer = "X";
|
|
150
|
+
winner = null;
|
|
151
|
+
winLine = null;
|
|
152
|
+
render();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
render();
|
|
156
|
+
</script>
|
|
157
|
+
</body>
|
|
158
|
+
</html>
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import Controller from "@ember/controller";
|
|
2
|
+
import { action } from "@ember/object";
|
|
3
|
+
import { tracked } from "@glimmer/tracking";
|
|
4
|
+
import { confetti } from "@tsparticles/confetti";
|
|
5
|
+
|
|
6
|
+
const WIN_PATTERNS = [
|
|
7
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
8
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
9
|
+
[0, 4, 8], [2, 4, 6],
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export default class ApplicationController extends Controller {
|
|
13
|
+
@tracked board = Array(9).fill(null);
|
|
14
|
+
@tracked currentPlayer = "X";
|
|
15
|
+
@tracked winner = null;
|
|
16
|
+
@tracked winLine = null;
|
|
17
|
+
@tracked scores = { X: 0, O: 0, draw: 0 };
|
|
18
|
+
|
|
19
|
+
particlesOptions = {
|
|
20
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
21
|
+
background: { color: { value: "#0a0a1a" } },
|
|
22
|
+
fpsLimit: 60,
|
|
23
|
+
particles: {
|
|
24
|
+
number: { value: 30, density: { enable: true } },
|
|
25
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
26
|
+
shape: { type: "circle" },
|
|
27
|
+
opacity: { value: 0.2, random: true },
|
|
28
|
+
size: { value: { min: 1, max: 2 } },
|
|
29
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
30
|
+
},
|
|
31
|
+
detectRetina: true,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
get status() {
|
|
35
|
+
if (!this.winner) return `${this.currentPlayer}'s turn`;
|
|
36
|
+
if (this.winner === "draw") return "It's a draw!";
|
|
37
|
+
return `${this.winner} wins!`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
isWinLine(index) {
|
|
41
|
+
return this.winLine?.includes(index) ?? false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
checkWin(board) {
|
|
45
|
+
for (const pattern of WIN_PATTERNS) {
|
|
46
|
+
const [a, b, c] = pattern;
|
|
47
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
48
|
+
return { winner: board[a], winLine: pattern };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw", winLine: [] };
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
fireConfettiEffect() {
|
|
56
|
+
const count = 200;
|
|
57
|
+
const defaults = { origin: { y: 0.7 } };
|
|
58
|
+
const fire = (particleRatio, opts) => {
|
|
59
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
60
|
+
};
|
|
61
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
62
|
+
fire(0.2, { spread: 60 });
|
|
63
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
64
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
65
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@action
|
|
69
|
+
handleMove(index) {
|
|
70
|
+
if (this.board[index] || this.winner) return;
|
|
71
|
+
const board = [...this.board];
|
|
72
|
+
board[index] = this.currentPlayer;
|
|
73
|
+
const result = this.checkWin(board);
|
|
74
|
+
if (result) {
|
|
75
|
+
this.board = board;
|
|
76
|
+
this.winner = result.winner;
|
|
77
|
+
this.winLine = result.winLine;
|
|
78
|
+
if (result.winner === "draw") this.scores = { ...this.scores, draw: this.scores.draw + 1 };
|
|
79
|
+
else this.scores = { ...this.scores, [result.winner]: this.scores[result.winner] + 1 };
|
|
80
|
+
if (result.winner !== "draw") setTimeout(() => this.fireConfettiEffect(), 100);
|
|
81
|
+
} else {
|
|
82
|
+
this.board = board;
|
|
83
|
+
this.currentPlayer = this.currentPlayer === "X" ? "O" : "X";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@action
|
|
88
|
+
handleReset() {
|
|
89
|
+
this.board = Array(9).fill(null);
|
|
90
|
+
this.currentPlayer = "X";
|
|
91
|
+
this.winner = null;
|
|
92
|
+
this.winLine = null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<Particles @id="tsparticles" @options={{this.particlesOptions}} />
|
|
2
|
+
|
|
3
|
+
<div id="app">
|
|
4
|
+
<h1>Tic-Tac-Toe</h1>
|
|
5
|
+
<div class="scoreboard">
|
|
6
|
+
<span>X: <strong>{{this.scores.X}}</strong></span>
|
|
7
|
+
<span>Draws: <strong>{{this.scores.draw}}</strong></span>
|
|
8
|
+
<span>O: <strong>{{this.scores.O}}</strong></span>
|
|
9
|
+
</div>
|
|
10
|
+
<div class="turn">{{this.status}}</div>
|
|
11
|
+
<div class="board">
|
|
12
|
+
{{#each this.board as |cell i|}}
|
|
13
|
+
<div
|
|
14
|
+
class="cell{{if cell " taken"}}{{if (this.isWinLine i) " win"}}"
|
|
15
|
+
role="button"
|
|
16
|
+
{{on "click" (fn this.handleMove i)}}
|
|
17
|
+
>
|
|
18
|
+
{{cell}}
|
|
19
|
+
</div>
|
|
20
|
+
{{/each}}
|
|
21
|
+
</div>
|
|
22
|
+
<button class="reset-btn" {{on "click" this.handleReset}}>New Game</button>
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
<style>
|
|
26
|
+
* { box-sizing: border-box; }
|
|
27
|
+
body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #0a0a1a; font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; color: #fff; }
|
|
28
|
+
#app { text-align: center; z-index: 10; padding: 1rem; }
|
|
29
|
+
h1 { font-size: 2.5em; margin-bottom: 1rem; background: linear-gradient(135deg, #6c5ce7, #fd79a8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
|
|
30
|
+
.scoreboard { display: flex; justify-content: center; gap: 2rem; margin-bottom: 1rem; font-size: 1.1em; opacity: 0.8; }
|
|
31
|
+
.turn { font-size: 1.2em; margin-bottom: 1rem; opacity: 0.7; }
|
|
32
|
+
.board { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 4px; background: rgba(255,255,255,0.1); border-radius: 12px; overflow: hidden; margin: 0 auto; width: fit-content; }
|
|
33
|
+
.cell { display: flex; align-items: center; justify-content: center; font-size: 2.5em; font-weight: 700; background: #12122a; cursor: pointer; transition: background 0.2s; user-select: none; }
|
|
34
|
+
.cell:hover { background: #1a1a3a; }
|
|
35
|
+
.cell.taken { cursor: default; }
|
|
36
|
+
.cell.win { background: rgba(108,92,231,0.3); }
|
|
37
|
+
.reset-btn { margin-top: 1.5rem; padding: 0.7em 2em; font-size: 1em; font-weight: 600; border: none; border-radius: 8px; background: linear-gradient(135deg, #6c5ce7, #a29bfe); color: #fff; cursor: pointer; transition: transform 0.2s; }
|
|
38
|
+
.reset-btn:hover { transform: scale(1.05); }
|
|
39
|
+
</style>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "ember serve",
|
|
7
|
+
"build": "ember build",
|
|
8
|
+
"preview": "ember serve"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"ember-source": "^6.0.0",
|
|
12
|
+
"@tsparticles/ember": "^4.1.3"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"ember-cli": "^6.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "tsc && vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"inferno": "^9.0.0",
|
|
13
|
+
"inferno-hydrate": "^9.0.0",
|
|
14
|
+
"@tsparticles/inferno": "^4.1.3"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"typescript": "~5.7.0",
|
|
18
|
+
"vite": "^6.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
* {
|
|
2
|
+
box-sizing: border-box;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
body {
|
|
6
|
+
margin: 0;
|
|
7
|
+
min-height: 100vh;
|
|
8
|
+
display: flex;
|
|
9
|
+
align-items: center;
|
|
10
|
+
justify-content: center;
|
|
11
|
+
background: #0a0a1a;
|
|
12
|
+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
|
|
13
|
+
color: #fff;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#app {
|
|
17
|
+
text-align: center;
|
|
18
|
+
z-index: 10;
|
|
19
|
+
padding: 1rem;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
h1 {
|
|
23
|
+
font-size: 2.5em;
|
|
24
|
+
margin-bottom: 1rem;
|
|
25
|
+
background: linear-gradient(135deg, #6c5ce7, #fd79a8);
|
|
26
|
+
-webkit-background-clip: text;
|
|
27
|
+
-webkit-text-fill-color: transparent;
|
|
28
|
+
background-clip: text;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.scoreboard {
|
|
32
|
+
display: flex;
|
|
33
|
+
justify-content: center;
|
|
34
|
+
gap: 2rem;
|
|
35
|
+
margin-bottom: 1rem;
|
|
36
|
+
font-size: 1.1em;
|
|
37
|
+
opacity: 0.8;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.turn {
|
|
41
|
+
font-size: 1.2em;
|
|
42
|
+
margin-bottom: 1rem;
|
|
43
|
+
opacity: 0.7;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.board {
|
|
47
|
+
display: grid;
|
|
48
|
+
grid-template-columns: repeat(3, 100px);
|
|
49
|
+
grid-template-rows: repeat(3, 100px);
|
|
50
|
+
gap: 4px;
|
|
51
|
+
background: rgba(255, 255, 255, 0.1);
|
|
52
|
+
border-radius: 12px;
|
|
53
|
+
overflow: hidden;
|
|
54
|
+
margin: 0 auto;
|
|
55
|
+
width: fit-content;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.cell {
|
|
59
|
+
display: flex;
|
|
60
|
+
align-items: center;
|
|
61
|
+
justify-content: center;
|
|
62
|
+
font-size: 2.5em;
|
|
63
|
+
font-weight: 700;
|
|
64
|
+
background: #12122a;
|
|
65
|
+
cursor: pointer;
|
|
66
|
+
transition: background 0.2s;
|
|
67
|
+
user-select: none;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.cell:hover {
|
|
71
|
+
background: #1a1a3a;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.cell.taken {
|
|
75
|
+
cursor: default;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.cell.win {
|
|
79
|
+
background: rgba(108, 92, 231, 0.3);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.reset-btn {
|
|
83
|
+
margin-top: 1.5rem;
|
|
84
|
+
padding: 0.7em 2em;
|
|
85
|
+
font-size: 1em;
|
|
86
|
+
font-weight: 600;
|
|
87
|
+
border: none;
|
|
88
|
+
border-radius: 8px;
|
|
89
|
+
background: linear-gradient(135deg, #6c5ce7, #a29bfe);
|
|
90
|
+
color: #fff;
|
|
91
|
+
cursor: pointer;
|
|
92
|
+
transition: transform 0.2s;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.reset-btn:hover {
|
|
96
|
+
transform: scale(1.05);
|
|
97
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { Component } from "inferno";
|
|
2
|
+
import Particles from "@tsparticles/inferno";
|
|
3
|
+
import { loadSlim } from "@tsparticles/slim";
|
|
4
|
+
import { confetti } from "@tsparticles/confetti";
|
|
5
|
+
import type { Engine, ISourceOptions } from "@tsparticles/engine";
|
|
6
|
+
import "./App.css";
|
|
7
|
+
|
|
8
|
+
type Player = "X" | "O";
|
|
9
|
+
|
|
10
|
+
interface GameState {
|
|
11
|
+
board: (Player | null)[];
|
|
12
|
+
currentPlayer: Player;
|
|
13
|
+
winner: Player | "draw" | null;
|
|
14
|
+
winLine: number[] | null;
|
|
15
|
+
scores: { X: number; O: number; draw: number };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const WIN_PATTERNS = [
|
|
19
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
20
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
21
|
+
[0, 4, 8], [2, 4, 6],
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function createInitialState(): GameState {
|
|
25
|
+
return {
|
|
26
|
+
board: Array(9).fill(null),
|
|
27
|
+
currentPlayer: "X",
|
|
28
|
+
winner: null,
|
|
29
|
+
winLine: null,
|
|
30
|
+
scores: { X: 0, O: 0, draw: 0 },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
35
|
+
for (const pattern of WIN_PATTERNS) {
|
|
36
|
+
const [a, b, c] = pattern;
|
|
37
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
38
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (board.every((cell) => cell !== null)) {
|
|
42
|
+
return { winner: "draw" as const, winLine: [] };
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fireConfettiEffect() {
|
|
48
|
+
const count = 200;
|
|
49
|
+
const defaults = { origin: { y: 0.7 } };
|
|
50
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
51
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
52
|
+
};
|
|
53
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
54
|
+
fire(0.2, { spread: 60 });
|
|
55
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
56
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
57
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function init(engine: Engine): Promise<void> {
|
|
61
|
+
await loadSlim(engine);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const particlesOptions: ISourceOptions = {
|
|
65
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
66
|
+
background: { color: { value: "#0a0a1a" } },
|
|
67
|
+
fpsLimit: 60,
|
|
68
|
+
particles: {
|
|
69
|
+
number: { value: 30, density: { enable: true } },
|
|
70
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
71
|
+
shape: { type: "circle" },
|
|
72
|
+
opacity: { value: 0.2, random: true },
|
|
73
|
+
size: { value: { min: 1, max: 2 } },
|
|
74
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
75
|
+
},
|
|
76
|
+
detectRetina: true,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export default class App extends Component<any, GameState> {
|
|
80
|
+
constructor(props: any) {
|
|
81
|
+
super(props);
|
|
82
|
+
this.state = createInitialState();
|
|
83
|
+
this.handleMove = this.handleMove.bind(this);
|
|
84
|
+
this.handleReset = this.handleReset.bind(this);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
handleMove(index: number) {
|
|
88
|
+
this.setState((prev) => {
|
|
89
|
+
if (prev.board[index] || prev.winner) return prev;
|
|
90
|
+
const board = [...prev.board];
|
|
91
|
+
board[index] = prev.currentPlayer;
|
|
92
|
+
const result = checkWin(board);
|
|
93
|
+
if (result) {
|
|
94
|
+
const scores = { ...prev.scores };
|
|
95
|
+
if (result.winner === "draw") scores.draw++;
|
|
96
|
+
else scores[result.winner]++;
|
|
97
|
+
if (result.winner !== "draw") {
|
|
98
|
+
setTimeout(fireConfettiEffect, 100);
|
|
99
|
+
}
|
|
100
|
+
return { ...prev, board, winner: result.winner, winLine: result.winLine, scores };
|
|
101
|
+
}
|
|
102
|
+
return { ...prev, board, currentPlayer: prev.currentPlayer === "X" ? "O" : "X" };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
handleReset() {
|
|
107
|
+
this.setState((prev) => ({
|
|
108
|
+
...prev,
|
|
109
|
+
board: Array(9).fill(null),
|
|
110
|
+
currentPlayer: "X",
|
|
111
|
+
winner: null,
|
|
112
|
+
winLine: null,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
render() {
|
|
117
|
+
const state = this.state;
|
|
118
|
+
const status = state.winner
|
|
119
|
+
? state.winner === "draw" ? "It's a draw!" : `${state.winner} wins!`
|
|
120
|
+
: `${state.currentPlayer}'s turn`;
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<div>
|
|
124
|
+
<Particles id="tsparticles" init={init} options={particlesOptions} />
|
|
125
|
+
<div id="app">
|
|
126
|
+
<h1>Tic-Tac-Toe</h1>
|
|
127
|
+
<div className="scoreboard">
|
|
128
|
+
<span>X: <strong>{state.scores.X}</strong></span>
|
|
129
|
+
<span>Draws: <strong>{state.scores.draw}</strong></span>
|
|
130
|
+
<span>O: <strong>{state.scores.O}</strong></span>
|
|
131
|
+
</div>
|
|
132
|
+
<div className="turn">{status}</div>
|
|
133
|
+
<div className="board">
|
|
134
|
+
{state.board.map((cell, i) => (
|
|
135
|
+
<div
|
|
136
|
+
key={i}
|
|
137
|
+
className={`cell${cell ? " taken" : ""}${state.winLine?.includes(i) ? " win" : ""}`}
|
|
138
|
+
onClick={() => this.handleMove(i)}
|
|
139
|
+
>
|
|
140
|
+
{cell}
|
|
141
|
+
</div>
|
|
142
|
+
))}
|
|
143
|
+
</div>
|
|
144
|
+
<button className="reset-btn" onClick={this.handleReset}>New Game</button>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|