@tsparticles/template-tictactoe 4.1.3 → 4.2.1
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 +16 -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/vanilla/package.json +1 -1
- 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,139 @@
|
|
|
1
|
+
<app>
|
|
2
|
+
<riot-particles id="tsparticles"></riot-particles>
|
|
3
|
+
<div id="app">
|
|
4
|
+
<h1>Tic-Tac-Toe</h1>
|
|
5
|
+
<div class="scoreboard">
|
|
6
|
+
<span>X: <strong>{ state.scores.X }</strong></span>
|
|
7
|
+
<span>Draws: <strong>{ state.scores.draw }</strong></span>
|
|
8
|
+
<span>O: <strong>{ state.scores.O }</strong></span>
|
|
9
|
+
</div>
|
|
10
|
+
<div class="turn">{ statusText }</div>
|
|
11
|
+
<div class="board">
|
|
12
|
+
<div each="{ cell, i in state.board }"
|
|
13
|
+
class="cell{ cell ? ' taken' : '' }{ state.winLine && state.winLine.includes(i) ? ' win' : '' }"
|
|
14
|
+
onclick="{ () => handleMove(i) }">
|
|
15
|
+
{ cell }
|
|
16
|
+
</div>
|
|
17
|
+
</div>
|
|
18
|
+
<button class="reset-btn" onclick="{ handleReset }">New Game</button>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<script>
|
|
22
|
+
import { confetti } from "@tsparticles/confetti";
|
|
23
|
+
import { loadSlim } from "@tsparticles/slim";
|
|
24
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
25
|
+
|
|
26
|
+
const WIN_PATTERNS = [
|
|
27
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
28
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
29
|
+
[0, 4, 8], [2, 4, 6],
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
export default {
|
|
33
|
+
state: {
|
|
34
|
+
board: Array(9).fill(null),
|
|
35
|
+
currentPlayer: "X",
|
|
36
|
+
winner: null,
|
|
37
|
+
winLine: null,
|
|
38
|
+
scores: { X: 0, O: 0, draw: 0 },
|
|
39
|
+
},
|
|
40
|
+
get statusText() {
|
|
41
|
+
const w = this.state.winner;
|
|
42
|
+
if (!w) return `${this.state.currentPlayer}'s turn`;
|
|
43
|
+
if (w === "draw") return "It's a draw!";
|
|
44
|
+
return `${w} wins!`;
|
|
45
|
+
},
|
|
46
|
+
checkWin(board) {
|
|
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], winLine: pattern };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw", winLine: [] };
|
|
54
|
+
return null;
|
|
55
|
+
},
|
|
56
|
+
fireConfettiEffect() {
|
|
57
|
+
const count = 200;
|
|
58
|
+
const defaults = { origin: { y: 0.7 } };
|
|
59
|
+
const fire = (particleRatio, opts) => {
|
|
60
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
61
|
+
};
|
|
62
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
63
|
+
fire(0.2, { spread: 60 });
|
|
64
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
65
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
66
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
67
|
+
},
|
|
68
|
+
handleMove(index) {
|
|
69
|
+
if (this.state.board[index] || this.state.winner) return;
|
|
70
|
+
const board = [...this.state.board];
|
|
71
|
+
board[index] = this.state.currentPlayer;
|
|
72
|
+
const result = this.checkWin(board);
|
|
73
|
+
if (result) {
|
|
74
|
+
this.update({
|
|
75
|
+
board,
|
|
76
|
+
winner: result.winner,
|
|
77
|
+
winLine: result.winLine,
|
|
78
|
+
scores: {
|
|
79
|
+
...this.state.scores,
|
|
80
|
+
...(result.winner === "draw"
|
|
81
|
+
? { draw: this.state.scores.draw + 1 }
|
|
82
|
+
: { [result.winner]: this.state.scores[result.winner] + 1 }),
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
if (result.winner !== "draw") setTimeout(() => this.fireConfettiEffect(), 100);
|
|
86
|
+
} else {
|
|
87
|
+
this.update({
|
|
88
|
+
board,
|
|
89
|
+
currentPlayer: this.state.currentPlayer === "X" ? "O" : "X",
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
handleReset() {
|
|
94
|
+
this.update({
|
|
95
|
+
board: Array(9).fill(null),
|
|
96
|
+
currentPlayer: "X",
|
|
97
|
+
winner: null,
|
|
98
|
+
winLine: null,
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
async onMounted() {
|
|
102
|
+
await loadSlim(tsParticles);
|
|
103
|
+
await tsParticles.load({
|
|
104
|
+
id: "tsparticles",
|
|
105
|
+
options: {
|
|
106
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
107
|
+
background: { color: { value: "#0a0a1a" } },
|
|
108
|
+
fpsLimit: 60,
|
|
109
|
+
particles: {
|
|
110
|
+
number: { value: 30, density: { enable: true } },
|
|
111
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
112
|
+
shape: { type: "circle" },
|
|
113
|
+
opacity: { value: 0.2, random: true },
|
|
114
|
+
size: { value: { min: 1, max: 2 } },
|
|
115
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
116
|
+
},
|
|
117
|
+
detectRetina: true,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
</script>
|
|
123
|
+
|
|
124
|
+
<style>
|
|
125
|
+
* { box-sizing: border-box; }
|
|
126
|
+
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; }
|
|
127
|
+
#app { text-align: center; z-index: 10; padding: 1rem; }
|
|
128
|
+
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; }
|
|
129
|
+
.scoreboard { display: flex; justify-content: center; gap: 2rem; margin-bottom: 1rem; font-size: 1.1em; opacity: 0.8; }
|
|
130
|
+
.turn { font-size: 1.2em; margin-bottom: 1rem; opacity: 0.7; }
|
|
131
|
+
.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; }
|
|
132
|
+
.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; }
|
|
133
|
+
.cell:hover { background: #1a1a3a; }
|
|
134
|
+
.cell.taken { cursor: default; }
|
|
135
|
+
.cell.win { background: rgba(108,92,231,0.3); }
|
|
136
|
+
.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; }
|
|
137
|
+
.reset-btn:hover { transform: scale(1.05); }
|
|
138
|
+
</style>
|
|
139
|
+
</app>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{packageName}}",
|
|
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
|
+
"solid-js": "^1.8.11",
|
|
13
|
+
"@tsparticles/solid": "^4.1.3"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"vite-plugin-solid": "^2.8.2"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createSignal, onMount } from "solid-js";
|
|
2
|
+
import Particles, { initParticlesEngine } from "@tsparticles/solid";
|
|
3
|
+
import { loadSlim } from "@tsparticles/slim";
|
|
4
|
+
import { confetti } from "@tsparticles/confetti";
|
|
5
|
+
import type { ISourceOptions } from "@tsparticles/engine";
|
|
6
|
+
|
|
7
|
+
type Player = "X" | "O";
|
|
8
|
+
|
|
9
|
+
interface GameState {
|
|
10
|
+
board: (Player | null)[];
|
|
11
|
+
currentPlayer: Player;
|
|
12
|
+
winner: Player | "draw" | null;
|
|
13
|
+
winLine: number[] | null;
|
|
14
|
+
scores: { X: number; O: number; draw: number };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const WIN_PATTERNS = [
|
|
18
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
19
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
20
|
+
[0, 4, 8], [2, 4, 6],
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function createInitialState(): GameState {
|
|
24
|
+
return {
|
|
25
|
+
board: Array(9).fill(null),
|
|
26
|
+
currentPlayer: "X",
|
|
27
|
+
winner: null,
|
|
28
|
+
winLine: null,
|
|
29
|
+
scores: { X: 0, O: 0, draw: 0 },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
34
|
+
for (const pattern of WIN_PATTERNS) {
|
|
35
|
+
const [a, b, c] = pattern;
|
|
36
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
37
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (board.every((cell) => cell !== null)) {
|
|
41
|
+
return { winner: "draw" as const, winLine: [] };
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fireConfettiEffect() {
|
|
47
|
+
const count = 200;
|
|
48
|
+
const defaults = { origin: { y: 0.7 } };
|
|
49
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
50
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
51
|
+
};
|
|
52
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
53
|
+
fire(0.2, { spread: 60 });
|
|
54
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
55
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
56
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default function App() {
|
|
60
|
+
const [state, setState] = createSignal<GameState>(createInitialState());
|
|
61
|
+
|
|
62
|
+
onMount(() => {
|
|
63
|
+
void initParticlesEngine(async (engine) => {
|
|
64
|
+
await loadSlim(engine);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const options: ISourceOptions = {
|
|
69
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
70
|
+
background: { color: { value: "#0a0a1a" } },
|
|
71
|
+
fpsLimit: 60,
|
|
72
|
+
particles: {
|
|
73
|
+
number: { value: 30, density: { enable: true } },
|
|
74
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
75
|
+
shape: { type: "circle" },
|
|
76
|
+
opacity: { value: 0.2, random: true },
|
|
77
|
+
size: { value: { min: 1, max: 2 } },
|
|
78
|
+
move: {
|
|
79
|
+
enable: true,
|
|
80
|
+
speed: 0.5,
|
|
81
|
+
direction: "none",
|
|
82
|
+
random: true,
|
|
83
|
+
straight: false,
|
|
84
|
+
outModes: { default: "out" },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
detectRetina: true,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
function handleMove(index: number) {
|
|
91
|
+
setState((prev) => {
|
|
92
|
+
if (prev.board[index] || prev.winner) return prev;
|
|
93
|
+
const board = [...prev.board];
|
|
94
|
+
board[index] = prev.currentPlayer;
|
|
95
|
+
const result = checkWin(board);
|
|
96
|
+
if (result) {
|
|
97
|
+
const scores = { ...prev.scores };
|
|
98
|
+
if (result.winner === "draw") scores.draw++;
|
|
99
|
+
else scores[result.winner]++;
|
|
100
|
+
if (result.winner !== "draw") {
|
|
101
|
+
setTimeout(fireConfettiEffect, 100);
|
|
102
|
+
}
|
|
103
|
+
return { ...prev, board, winner: result.winner, winLine: result.winLine, scores };
|
|
104
|
+
}
|
|
105
|
+
return { ...prev, board, currentPlayer: prev.currentPlayer === "X" ? "O" : "X" };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function handleReset() {
|
|
110
|
+
setState((prev) => ({
|
|
111
|
+
...prev,
|
|
112
|
+
board: Array(9).fill(null),
|
|
113
|
+
currentPlayer: "X",
|
|
114
|
+
winner: null,
|
|
115
|
+
winLine: null,
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const status = (() => {
|
|
120
|
+
const s = state();
|
|
121
|
+
if (!s.winner) return `${s.currentPlayer}'s turn`;
|
|
122
|
+
if (s.winner === "draw") return "It's a draw!";
|
|
123
|
+
return `${s.winner} wins!`;
|
|
124
|
+
})();
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<>
|
|
128
|
+
<div id="app">
|
|
129
|
+
<h1>Tic-Tac-Toe</h1>
|
|
130
|
+
<div class="scoreboard">
|
|
131
|
+
<span>X: <strong>{state().scores.X}</strong></span>
|
|
132
|
+
<span>Draws: <strong>{state().scores.draw}</strong></span>
|
|
133
|
+
<span>O: <strong>{state().scores.O}</strong></span>
|
|
134
|
+
</div>
|
|
135
|
+
<div class="turn">{status}</div>
|
|
136
|
+
<div class="board">
|
|
137
|
+
{state().board.map((cell, i) => (
|
|
138
|
+
<div
|
|
139
|
+
class={`cell${cell ? " taken" : ""}${state().winLine?.includes(i) ? " win" : ""}`}
|
|
140
|
+
onClick={() => handleMove(i)}
|
|
141
|
+
>
|
|
142
|
+
{cell}
|
|
143
|
+
</div>
|
|
144
|
+
))}
|
|
145
|
+
</div>
|
|
146
|
+
<button class="reset-btn" onClick={handleReset}>New Game</button>
|
|
147
|
+
</div>
|
|
148
|
+
<Particles id="tsparticles" options={options} />
|
|
149
|
+
</>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
@@ -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,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{packageName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "stencil build --dev --watch",
|
|
7
|
+
"build": "stencil build",
|
|
8
|
+
"preview": "stencil build --dev"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@stencil/core": "^4.20.0",
|
|
12
|
+
"@tsparticles/stencil": "^4.1.3"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -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,122 @@
|
|
|
1
|
+
import { Component, State, h } from "@stencil/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
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
14
|
+
for (const pattern of WIN_PATTERNS) {
|
|
15
|
+
const [a, b, c] = pattern;
|
|
16
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
17
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw" as const, winLine: [] };
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function fireConfettiEffect(): void {
|
|
25
|
+
const count = 200;
|
|
26
|
+
const defaults = { origin: { y: 0.7 } };
|
|
27
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
28
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
29
|
+
};
|
|
30
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
31
|
+
fire(0.2, { spread: 60 });
|
|
32
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
33
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
34
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const particlesOptions: ISourceOptions = {
|
|
38
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
39
|
+
background: { color: { value: "#0a0a1a" } },
|
|
40
|
+
fpsLimit: 60,
|
|
41
|
+
particles: {
|
|
42
|
+
number: { value: 30, density: { enable: true } },
|
|
43
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
44
|
+
shape: { type: "circle" },
|
|
45
|
+
opacity: { value: 0.2, random: true },
|
|
46
|
+
size: { value: { min: 1, max: 2 } },
|
|
47
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
48
|
+
},
|
|
49
|
+
detectRetina: true,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
@Component({
|
|
53
|
+
tag: "app-home",
|
|
54
|
+
styleUrl: "app-home.css",
|
|
55
|
+
scoped: true,
|
|
56
|
+
})
|
|
57
|
+
export class AppHome {
|
|
58
|
+
@State() board: (Player | null)[] = Array(9).fill(null);
|
|
59
|
+
@State() currentPlayer: Player = "X";
|
|
60
|
+
@State() winner: Player | "draw" | null = null;
|
|
61
|
+
@State() winLine: number[] | null = null;
|
|
62
|
+
@State() scores = { X: 0, O: 0, draw: 0 };
|
|
63
|
+
|
|
64
|
+
get status(): string {
|
|
65
|
+
if (!this.winner) return `${this.currentPlayer}'s turn`;
|
|
66
|
+
if (this.winner === "draw") return "It's a draw!";
|
|
67
|
+
return `${this.winner} wins!`;
|
|
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 = 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 = { ...this.scores, draw: this.scores.draw + 1 };
|
|
80
|
+
else this.scores = { ...this.scores, [result.winner]: this.scores[result.winner] + 1 };
|
|
81
|
+
if (result.winner !== "draw") setTimeout(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
|
+
|
|
95
|
+
render() {
|
|
96
|
+
return (
|
|
97
|
+
<div>
|
|
98
|
+
<stencil-particles id="tsparticles" options={particlesOptions} />
|
|
99
|
+
<div id="app">
|
|
100
|
+
<h1>Tic-Tac-Toe</h1>
|
|
101
|
+
<div class="scoreboard">
|
|
102
|
+
<span>X: <strong>{this.scores.X}</strong></span>
|
|
103
|
+
<span>Draws: <strong>{this.scores.draw}</strong></span>
|
|
104
|
+
<span>O: <strong>{this.scores.O}</strong></span>
|
|
105
|
+
</div>
|
|
106
|
+
<div class="turn">{this.status}</div>
|
|
107
|
+
<div class="board">
|
|
108
|
+
{this.board.map((cell, i) => (
|
|
109
|
+
<div
|
|
110
|
+
class={`cell${cell ? " taken" : ""}${this.winLine?.includes(i) ? " win" : ""}`}
|
|
111
|
+
onClick={() => this.handleMove(i)}
|
|
112
|
+
>
|
|
113
|
+
{cell}
|
|
114
|
+
</div>
|
|
115
|
+
))}
|
|
116
|
+
</div>
|
|
117
|
+
<button class="reset-btn" onClick={() => this.handleReset()}>New Game</button>
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{packageName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@tsparticles/svelte": "^4.1.3"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
|
16
|
+
"svelte": "^5.55.7",
|
|
17
|
+
"svelte-check": "^3.7.1",
|
|
18
|
+
"tslib": "^2.8.1"
|
|
19
|
+
}
|
|
20
|
+
}
|