@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,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
|
+
"jquery": "^3.7.1",
|
|
13
|
+
"@tsparticles/jquery": "^4.1.3"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jquery": "^3.5.30",
|
|
17
|
+
"typescript": "~5.7.0",
|
|
18
|
+
"vite": "^6.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import "./style.css";
|
|
2
|
+
import $ from "jquery";
|
|
3
|
+
import "@tsparticles/jquery";
|
|
4
|
+
import { loadSlim } from "@tsparticles/slim";
|
|
5
|
+
import { confetti } from "@tsparticles/confetti";
|
|
6
|
+
|
|
7
|
+
type Player = "X" | "O";
|
|
8
|
+
|
|
9
|
+
const WIN_PATTERNS = [
|
|
10
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
11
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
12
|
+
[0, 4, 8], [2, 4, 6],
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
interface GameState {
|
|
16
|
+
board: (Player | null)[];
|
|
17
|
+
currentPlayer: Player;
|
|
18
|
+
winner: Player | "draw" | null;
|
|
19
|
+
winLine: number[] | null;
|
|
20
|
+
scores: { X: number; O: number; draw: number };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let state: GameState = {
|
|
24
|
+
board: Array(9).fill(null),
|
|
25
|
+
currentPlayer: "X",
|
|
26
|
+
winner: null,
|
|
27
|
+
winLine: null,
|
|
28
|
+
scores: { X: 0, O: 0, draw: 0 },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
32
|
+
for (const pattern of WIN_PATTERNS) {
|
|
33
|
+
const [a, b, c] = pattern;
|
|
34
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
35
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw" as const, winLine: [] };
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fireConfettiEffect(): void {
|
|
43
|
+
const count = 200;
|
|
44
|
+
const defaults = { origin: { y: 0.7 } };
|
|
45
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
46
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
47
|
+
};
|
|
48
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
49
|
+
fire(0.2, { spread: 60 });
|
|
50
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
51
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
52
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function render(): void {
|
|
56
|
+
const $cells = $(".cell");
|
|
57
|
+
$cells.each((i, el) => {
|
|
58
|
+
const $el = $(el);
|
|
59
|
+
$el.text(state.board[i] ?? "");
|
|
60
|
+
$el.toggleClass("taken", state.board[i] !== null);
|
|
61
|
+
$el.toggleClass("win", state.winLine?.includes(i) ?? false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const $turn = $("#turn");
|
|
65
|
+
if (!state.winner) $turn.text(`${state.currentPlayer}'s turn`);
|
|
66
|
+
else if (state.winner === "draw") $turn.text("It's a draw!");
|
|
67
|
+
else $turn.text(`${state.winner} wins!`);
|
|
68
|
+
|
|
69
|
+
$("#scoreX").text(String(state.scores.X));
|
|
70
|
+
$("#scoreO").text(String(state.scores.O));
|
|
71
|
+
$("#scoreDraw").text(String(state.scores.draw));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function handleMove(index: number): void {
|
|
75
|
+
if (state.board[index] || state.winner) return;
|
|
76
|
+
const board = [...state.board];
|
|
77
|
+
board[index] = state.currentPlayer;
|
|
78
|
+
const result = checkWin(board);
|
|
79
|
+
if (result) {
|
|
80
|
+
state = { ...state, board, winner: result.winner, winLine: result.winLine };
|
|
81
|
+
if (result.winner === "draw") state.scores.draw++;
|
|
82
|
+
else state.scores[result.winner]++;
|
|
83
|
+
if (result.winner !== "draw") setTimeout(fireConfettiEffect, 100);
|
|
84
|
+
} else {
|
|
85
|
+
state = { ...state, board, currentPlayer: state.currentPlayer === "X" ? "O" : "X" };
|
|
86
|
+
}
|
|
87
|
+
render();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function handleReset(): void {
|
|
91
|
+
state = {
|
|
92
|
+
...state,
|
|
93
|
+
board: Array(9).fill(null),
|
|
94
|
+
currentPlayer: "X",
|
|
95
|
+
winner: null,
|
|
96
|
+
winLine: null,
|
|
97
|
+
};
|
|
98
|
+
render();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
$(document).ready(async () => {
|
|
102
|
+
$("#board").on("click", ".cell", function () {
|
|
103
|
+
const index = parseInt($(this).data("index") as string, 10);
|
|
104
|
+
if (!isNaN(index)) handleMove(index);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
$("#resetBtn").on("click", handleReset);
|
|
108
|
+
|
|
109
|
+
render();
|
|
110
|
+
|
|
111
|
+
await loadSlim($("#tsparticles").particles() as any);
|
|
112
|
+
$("#tsparticles").particles({
|
|
113
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
114
|
+
background: { color: { value: "#0a0a1a" } },
|
|
115
|
+
fpsLimit: 60,
|
|
116
|
+
particles: {
|
|
117
|
+
number: { value: 30, density: { enable: true } },
|
|
118
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
119
|
+
shape: { type: "circle" },
|
|
120
|
+
opacity: { value: 0.2, random: true },
|
|
121
|
+
size: { value: { min: 1, max: 2 } },
|
|
122
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
123
|
+
},
|
|
124
|
+
detectRetina: true,
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -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,19 @@
|
|
|
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
|
+
"lit": "^3.2.0",
|
|
13
|
+
"@tsparticles/lit": "^4.1.3"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"vite": "^6.0.0",
|
|
17
|
+
"typescript": "~5.7.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { LitElement, html, css } from "lit";
|
|
2
|
+
import { state } from "lit/decorators.js";
|
|
3
|
+
import { confetti } from "@tsparticles/confetti";
|
|
4
|
+
import type { ISourceOptions } from "@tsparticles/engine";
|
|
5
|
+
|
|
6
|
+
type Player = "X" | "O";
|
|
7
|
+
|
|
8
|
+
const WIN_PATTERNS = [
|
|
9
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
10
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
11
|
+
[0, 4, 8], [2, 4, 6],
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
15
|
+
for (const pattern of WIN_PATTERNS) {
|
|
16
|
+
const [a, b, c] = pattern;
|
|
17
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
18
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (board.every((cell) => cell !== null)) return { winner: "draw" as const, winLine: [] };
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function fireConfettiEffect(): void {
|
|
26
|
+
const count = 200;
|
|
27
|
+
const defaults = { origin: { y: 0.7 } };
|
|
28
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
29
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
30
|
+
};
|
|
31
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
32
|
+
fire(0.2, { spread: 60 });
|
|
33
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
34
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
35
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const particlesOptions: ISourceOptions = {
|
|
39
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
40
|
+
background: { color: { value: "#0a0a1a" } },
|
|
41
|
+
fpsLimit: 60,
|
|
42
|
+
particles: {
|
|
43
|
+
number: { value: 30, density: { enable: true } },
|
|
44
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
45
|
+
shape: { type: "circle" },
|
|
46
|
+
opacity: { value: 0.2, random: true },
|
|
47
|
+
size: { value: { min: 1, max: 2 } },
|
|
48
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
49
|
+
},
|
|
50
|
+
detectRetina: true,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export class MyApp extends LitElement {
|
|
54
|
+
@state() private board: (Player | null)[] = Array(9).fill(null);
|
|
55
|
+
@state() private currentPlayer: Player = "X";
|
|
56
|
+
@state() private winner: Player | "draw" | null = null;
|
|
57
|
+
@state() private winLine: number[] | null = null;
|
|
58
|
+
@state() private scores = { X: 0, O: 0, draw: 0 };
|
|
59
|
+
|
|
60
|
+
get status(): string {
|
|
61
|
+
if (!this.winner) return `${this.currentPlayer}'s turn`;
|
|
62
|
+
if (this.winner === "draw") return "It's a draw!";
|
|
63
|
+
return `${this.winner} wins!`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
handleMove(index: number): void {
|
|
67
|
+
if (this.board[index] || this.winner) return;
|
|
68
|
+
const board = [...this.board];
|
|
69
|
+
board[index] = this.currentPlayer;
|
|
70
|
+
const result = checkWin(board);
|
|
71
|
+
if (result) {
|
|
72
|
+
this.board = board;
|
|
73
|
+
this.winner = result.winner;
|
|
74
|
+
this.winLine = result.winLine;
|
|
75
|
+
if (result.winner === "draw") this.scores = { ...this.scores, draw: this.scores.draw + 1 };
|
|
76
|
+
else this.scores = { ...this.scores, [result.winner]: this.scores[result.winner] + 1 };
|
|
77
|
+
if (result.winner !== "draw") setTimeout(fireConfettiEffect, 100);
|
|
78
|
+
} else {
|
|
79
|
+
this.board = board;
|
|
80
|
+
this.currentPlayer = this.currentPlayer === "X" ? "O" : "X";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
handleReset(): void {
|
|
85
|
+
this.board = Array(9).fill(null);
|
|
86
|
+
this.currentPlayer = "X";
|
|
87
|
+
this.winner = null;
|
|
88
|
+
this.winLine = null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
render() {
|
|
92
|
+
return html`
|
|
93
|
+
<lit-particles id="tsparticles" .options=${particlesOptions}></lit-particles>
|
|
94
|
+
<div id="app">
|
|
95
|
+
<h1>Tic-Tac-Toe</h1>
|
|
96
|
+
<div class="scoreboard">
|
|
97
|
+
<span>X: <strong>${this.scores.X}</strong></span>
|
|
98
|
+
<span>Draws: <strong>${this.scores.draw}</strong></span>
|
|
99
|
+
<span>O: <strong>${this.scores.O}</strong></span>
|
|
100
|
+
</div>
|
|
101
|
+
<div class="turn">${this.status}</div>
|
|
102
|
+
<div class="board">
|
|
103
|
+
${this.board.map((cell, i) => html`
|
|
104
|
+
<div
|
|
105
|
+
class="cell${cell ? " taken" : ""}${this.winLine?.includes(i) ? " win" : ""}"
|
|
106
|
+
@click=${() => this.handleMove(i)}
|
|
107
|
+
>
|
|
108
|
+
${cell ?? ""}
|
|
109
|
+
</div>
|
|
110
|
+
`)}
|
|
111
|
+
</div>
|
|
112
|
+
<button class="reset-btn" @click=${this.handleReset}>New Game</button>
|
|
113
|
+
</div>
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static styles = css`
|
|
118
|
+
:host { display: block; }
|
|
119
|
+
#app { text-align: center; z-index: 10; padding: 1rem; }
|
|
120
|
+
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; }
|
|
121
|
+
.scoreboard { display: flex; justify-content: center; gap: 2rem; margin-bottom: 1rem; font-size: 1.1em; opacity: 0.8; }
|
|
122
|
+
.turn { font-size: 1.2em; margin-bottom: 1rem; opacity: 0.7; }
|
|
123
|
+
.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; }
|
|
124
|
+
.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; }
|
|
125
|
+
.cell:hover { background: #1a1a3a; }
|
|
126
|
+
.cell.taken { cursor: default; }
|
|
127
|
+
.cell.win { background: rgba(108,92,231,0.3); }
|
|
128
|
+
.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; }
|
|
129
|
+
.reset-btn:hover { transform: scale(1.05); }
|
|
130
|
+
`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
customElements.define("my-app", MyApp);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "next dev",
|
|
8
|
+
"build": "next build",
|
|
9
|
+
"preview": "next start"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"next": "^15.0.0",
|
|
13
|
+
"react": "^19.2.5",
|
|
14
|
+
"react-dom": "^19.2.5",
|
|
15
|
+
"@tsparticles/react": "^4.1.3"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"typescript": "~5.7.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, useCallback } from "react";
|
|
4
|
+
import Particles from "@tsparticles/react";
|
|
5
|
+
import { confetti } from "@tsparticles/confetti";
|
|
6
|
+
import type { ISourceOptions } from "@tsparticles/engine";
|
|
7
|
+
import Providers from "./providers";
|
|
8
|
+
|
|
9
|
+
type Player = "X" | "O";
|
|
10
|
+
|
|
11
|
+
interface GameState {
|
|
12
|
+
board: (Player | null)[];
|
|
13
|
+
currentPlayer: Player;
|
|
14
|
+
winner: Player | "draw" | null;
|
|
15
|
+
winLine: number[] | null;
|
|
16
|
+
scores: { X: number; O: number; draw: number };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const WIN_PATTERNS = [
|
|
20
|
+
[0, 1, 2], [3, 4, 5], [6, 7, 8],
|
|
21
|
+
[0, 3, 6], [1, 4, 7], [2, 5, 8],
|
|
22
|
+
[0, 4, 8], [2, 4, 6],
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
function createInitialState(): GameState {
|
|
26
|
+
return {
|
|
27
|
+
board: Array(9).fill(null),
|
|
28
|
+
currentPlayer: "X",
|
|
29
|
+
winner: null,
|
|
30
|
+
winLine: null,
|
|
31
|
+
scores: { X: 0, O: 0, draw: 0 },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function checkWin(board: (Player | null)[]): { winner: Player | "draw"; winLine: number[] } | null {
|
|
36
|
+
for (const pattern of WIN_PATTERNS) {
|
|
37
|
+
const [a, b, c] = pattern;
|
|
38
|
+
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
|
|
39
|
+
return { winner: board[a] as Player, winLine: pattern };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (board.every((cell) => cell !== null)) {
|
|
43
|
+
return { winner: "draw" as const, winLine: [] };
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function fireConfettiEffect() {
|
|
49
|
+
const count = 200;
|
|
50
|
+
const defaults = { origin: { y: 0.7 } };
|
|
51
|
+
const fire = (particleRatio: number, opts: Record<string, unknown>) => {
|
|
52
|
+
confetti({ ...defaults, ...opts, particleCount: Math.floor(count * particleRatio) });
|
|
53
|
+
};
|
|
54
|
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
|
55
|
+
fire(0.2, { spread: 60 });
|
|
56
|
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
|
57
|
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
|
58
|
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const particlesOptions: ISourceOptions = {
|
|
62
|
+
fullScreen: { enable: true, zIndex: -1 },
|
|
63
|
+
background: { color: { value: "#0a0a1a" } },
|
|
64
|
+
fpsLimit: 60,
|
|
65
|
+
particles: {
|
|
66
|
+
number: { value: 30, density: { enable: true } },
|
|
67
|
+
color: { value: ["#6c5ce7", "#a29bfe"] },
|
|
68
|
+
shape: { type: "circle" },
|
|
69
|
+
opacity: { value: 0.2, random: true },
|
|
70
|
+
size: { value: { min: 1, max: 2 } },
|
|
71
|
+
move: { enable: true, speed: 0.5, direction: "none", random: true, straight: false, outModes: { default: "out" } },
|
|
72
|
+
},
|
|
73
|
+
detectRetina: true,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export default function Home() {
|
|
77
|
+
const [state, setState] = useState<GameState>(createInitialState);
|
|
78
|
+
|
|
79
|
+
const handleMove = useCallback((index: number) => {
|
|
80
|
+
setState((prev) => {
|
|
81
|
+
if (prev.board[index] || prev.winner) return prev;
|
|
82
|
+
const board = [...prev.board];
|
|
83
|
+
board[index] = prev.currentPlayer;
|
|
84
|
+
const result = checkWin(board);
|
|
85
|
+
if (result) {
|
|
86
|
+
const scores = { ...prev.scores };
|
|
87
|
+
if (result.winner === "draw") scores.draw++;
|
|
88
|
+
else scores[result.winner]++;
|
|
89
|
+
if (result.winner !== "draw") {
|
|
90
|
+
setTimeout(fireConfettiEffect, 100);
|
|
91
|
+
}
|
|
92
|
+
return { ...prev, board, winner: result.winner, winLine: result.winLine, scores };
|
|
93
|
+
}
|
|
94
|
+
return { ...prev, board, currentPlayer: prev.currentPlayer === "X" ? "O" : "X" };
|
|
95
|
+
});
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
function handleReset() {
|
|
99
|
+
setState((prev) => ({
|
|
100
|
+
...prev,
|
|
101
|
+
board: Array(9).fill(null),
|
|
102
|
+
currentPlayer: "X",
|
|
103
|
+
winner: null,
|
|
104
|
+
winLine: null,
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const status = state.winner
|
|
109
|
+
? state.winner === "draw" ? "It's a draw!" : `${state.winner} wins!`
|
|
110
|
+
: `${state.currentPlayer}'s turn`;
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<Providers>
|
|
114
|
+
<Particles id="tsparticles" options={particlesOptions} />
|
|
115
|
+
<div id="app" style={{
|
|
116
|
+
textAlign: "center", zIndex: 10, padding: "1rem",
|
|
117
|
+
}}>
|
|
118
|
+
<h1 style={{
|
|
119
|
+
fontSize: "2.5em", marginBottom: "1rem",
|
|
120
|
+
background: "linear-gradient(135deg, #6c5ce7, #fd79a8)",
|
|
121
|
+
WebkitBackgroundClip: "text", WebkitTextFillColor: "transparent",
|
|
122
|
+
backgroundClip: "text",
|
|
123
|
+
}}>
|
|
124
|
+
Tic-Tac-Toe
|
|
125
|
+
</h1>
|
|
126
|
+
<div className="scoreboard" style={{
|
|
127
|
+
display: "flex", justifyContent: "center", gap: "2rem",
|
|
128
|
+
marginBottom: "1rem", fontSize: "1.1em", opacity: 0.8,
|
|
129
|
+
}}>
|
|
130
|
+
<span>X: <strong>{state.scores.X}</strong></span>
|
|
131
|
+
<span>Draws: <strong>{state.scores.draw}</strong></span>
|
|
132
|
+
<span>O: <strong>{state.scores.O}</strong></span>
|
|
133
|
+
</div>
|
|
134
|
+
<div className="turn" style={{ fontSize: "1.2em", marginBottom: "1rem", opacity: 0.7 }}>
|
|
135
|
+
{status}
|
|
136
|
+
</div>
|
|
137
|
+
<div className="board" style={{
|
|
138
|
+
display: "grid", gridTemplateColumns: "repeat(3, 100px)",
|
|
139
|
+
gridTemplateRows: "repeat(3, 100px)", gap: "4px",
|
|
140
|
+
background: "rgba(255,255,255,0.1)", borderRadius: "12px",
|
|
141
|
+
overflow: "hidden", margin: "0 auto", width: "fit-content",
|
|
142
|
+
}}>
|
|
143
|
+
{state.board.map((cell, i) => (
|
|
144
|
+
<div
|
|
145
|
+
key={i}
|
|
146
|
+
style={{
|
|
147
|
+
display: "flex", alignItems: "center", justifyContent: "center",
|
|
148
|
+
fontSize: "2.5em", fontWeight: 700, background: "#12122a",
|
|
149
|
+
cursor: cell ? "default" : "pointer", transition: "background 0.2s",
|
|
150
|
+
userSelect: "none", color: "#fff",
|
|
151
|
+
...(state.winLine?.includes(i) ? { background: "rgba(108,92,231,0.3)" } : {}),
|
|
152
|
+
}}
|
|
153
|
+
onClick={() => handleMove(i)}
|
|
154
|
+
>
|
|
155
|
+
{cell}
|
|
156
|
+
</div>
|
|
157
|
+
))}
|
|
158
|
+
</div>
|
|
159
|
+
<button
|
|
160
|
+
style={{
|
|
161
|
+
marginTop: "1.5rem", padding: "0.7em 2em", fontSize: "1em",
|
|
162
|
+
fontWeight: 600, border: "none", borderRadius: "8px",
|
|
163
|
+
background: "linear-gradient(135deg, #6c5ce7, #a29bfe)",
|
|
164
|
+
color: "#fff", cursor: "pointer", transition: "transform 0.2s",
|
|
165
|
+
}}
|
|
166
|
+
onClick={handleReset}
|
|
167
|
+
>
|
|
168
|
+
New Game
|
|
169
|
+
</button>
|
|
170
|
+
</div>
|
|
171
|
+
</Providers>
|
|
172
|
+
);
|
|
173
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ParticlesProvider } from "@tsparticles/react";
|
|
4
|
+
import { loadSlim } from "@tsparticles/slim";
|
|
5
|
+
import type { Engine } from "@tsparticles/engine";
|
|
6
|
+
|
|
7
|
+
async function init(engine: Engine): Promise<void> {
|
|
8
|
+
await loadSlim(engine);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function Providers({ children }: { children: React.ReactNode }) {
|
|
12
|
+
return <ParticlesProvider init={init}>{children}</ParticlesProvider>;
|
|
13
|
+
}
|