-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (116 loc) · 4.03 KB
/
script.js
File metadata and controls
138 lines (116 loc) · 4.03 KB
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
document.addEventListener("DOMContentLoaded", function() {
const canvas = document.querySelector("#gameCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 6;
const cellSize = 400 / gridSize;
let points = [];
let animations = [];
let slopes = new Set(); // Set to keep track of slopes
// Event Listeners
canvas.addEventListener("click", handleCanvasClick);
document.querySelector(".start-button").addEventListener("click", startGame);
document.querySelector(".reset-button").addEventListener("click", resetGame);
function handleCanvasClick(event) {
const clickedPoint = getClickedPoint(event);
if (isPointValid(clickedPoint)) {
for (let existingPoint of points) {
const slope = calculateSlope(clickedPoint, existingPoint);
slopes.add(slope);
}
points.push(clickedPoint);
animations.push({point: clickedPoint, radius: 0});
animateDot();
} else {
alert("Invalid move! Try again.");
}
}
function getClickedPoint(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
return {
x: Math.floor(x / cellSize),
y: Math.floor(y / cellSize)
};
}
function isPointValid(newPoint) {
// Check if point is in the same row or column as any existing point
if (points.some(p => p.x === newPoint.x || p.y === newPoint.y)) return false;
for (let existingPoint of points) {
const slope = calculateSlope(newPoint, existingPoint);
if (slopes.has(slope)) {
return false;
}
}
return true;
}
function calculateSlope(p1, p2) {
if (p2.x === p1.x) return Infinity;
return (p2.y - p1.y) / (p2.x - p1.x);
}
function animateDot() {
let animationFinished = true;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGridLines();
for (let animation of animations) {
if (animation.radius < cellSize / 3) {
animation.radius += cellSize / 30;
animationFinished = false;
}
ctx.beginPath();
ctx.arc((animation.point.x + 0.5) * cellSize, (animation.point.y + 0.5) * cellSize, animation.radius, 0, Math.PI * 2);
ctx.fillStyle = "#FF3000";
ctx.fill();
}
drawPoints();
if (!animationFinished) {
requestAnimationFrame(animateDot);
}
}
function drawGame() {
clearCanvas();
drawGridLines();
drawPoints();
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function drawGridLines() {
for (let i = 1; i < gridSize; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
}
function drawPoints() {
for (const point of points) {
if (!animations.some(a => a.point === point)) {
ctx.beginPath();
ctx.arc((point.x + 0.5) * cellSize, (point.y + 0.5) * cellSize, cellSize / 3, 0, Math.PI * 2);
ctx.fillStyle = "#FF3000";
ctx.fill();
}
}
}
function startGame() {
points = [];
animations = [];
slopes.clear(); // Clear the set of slopes
drawGame();
updateMessage("Game has started! Click on the grid to place your points.");
}
function resetGame() {
points = [];
animations = [];
slopes.clear(); // Clear the set of slopes
drawGame();
updateMessage("Game has been reset.");
}
function updateMessage(message) {
document.querySelector(".message").textContent = message;
}
startGame();
});