Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
209 lines (183 loc) · 5.52 KB
/
Copy pathindex.html
File metadata and controls
209 lines (183 loc) · 5.52 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Subitizing Game</title>
<style>
body { font-family: Arial, sans-serif; }
#game-container { display: flex; position: relative; }
#canvas { border: 1px solid black; width: 300px; height: 300px; margin-right: 20px; }
#controls { display: flex; flex-direction: column; }
.mode-btn { margin: 10px 0; }
#guess { margin: 10px 0; width: 60px; }
#feedback { margin: 10px 0; font-weight: bold; }
#timer-container {
position: absolute;
top: 10px;
right: 10px;
background: rgba(0,0,0,0.1);
padding: 4px 8px;
border-radius: 4px;
text-align: right;
font-size: 0.9rem;
}
#avg-time {
font-size: 0.8rem;
color: #555;
}
</style>
</head>
<body>
<h1>Subitizing Game</h1>
<div id="instructions">
<p>Instructions:</p>
<ul>
<li>Select a mode: Perceptual or Conceptual.</li>
<li>Look at the circles displayed in the box.</li>
<li>Type your guess of the total number of circles and press Enter.</li>
<li>If correct, a new problem will appear automatically!</li>
</ul>
</div>
<div id="game-container">
<canvas id="canvas" width="300" height="300"></canvas>
<div id="timer-container">
<div id="timer">Time: 0s</div>
<div id="avg-time">Avg: 0s</div>
</div>
<div id="controls">
<button id="perceptual-btn" class="mode-btn">Perceptual Mode</button>
<button id="conceptual-btn" class="mode-btn">Conceptual Mode</button>
<input type="number" id="guess" placeholder="Guess">
<div id="feedback"></div>
</div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const guessInput = document.getElementById('guess');
const feedback = document.getElementById('feedback');
const timerDisplay = document.getElementById('timer');
const avgTimeDisplay = document.getElementById('avg-time');
let mode = null;
let correctAnswer = 0;
const radius = 17; // bigger circles
const minDistance = radius * 2 + 4; // adjust spacing for new radius
let timer = 0;
let timerInterval;
let times = []; // store times for average
document.getElementById('perceptual-btn').addEventListener('click', () => {
mode = 'perceptual';
startGame();
});
document.getElementById('conceptual-btn').addEventListener('click', () => {
mode = 'conceptual';
startGame();
});
guessInput.addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
checkAnswer();
}
});
function startGame() {
generateProblem();
feedback.textContent = '';
guessInput.value = '';
guessInput.focus();
startTimer();
}
function generateProblem() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (mode === 'perceptual') {
correctAnswer = getRandomInt(1, 5);
const positions = generateNonOverlappingPositions(correctAnswer);
drawCircles(positions, '#1A85FF');
} else if (mode === 'conceptual') {
correctAnswer = getRandomInt(6, 8);
const redCount = getRandomInt(1, correctAnswer - 1);
const blueCount = correctAnswer - redCount;
const allPositions = generateNonOverlappingPositions(correctAnswer);
const bluePositions = allPositions.slice(0, blueCount);
const pinkPositions = allPositions.slice(blueCount);
drawCircles(bluePositions, '#1A85FF');
drawCircles(pinkPositions, '#D41159');
}
}
function generateNonOverlappingPositions(count) {
const positions = [];
let tries = 0;
const maxTries = 5000;
while (positions.length < count && tries < maxTries) {
let x = getRandomInt(radius, canvas.width - radius);
let y = getRandomInt(radius, canvas.height - radius);
let isFarEnough = true;
for (let p of positions) {
if (Math.hypot(p.x - x, p.y - y) < minDistance) {
isFarEnough = false;
break;
}
}
if (isFarEnough) {
positions.push({ x, y });
}
tries++;
}
// Fallback in case of too tight constraints
while (positions.length < count) {
positions.push({
x: getRandomInt(radius, canvas.width - radius),
y: getRandomInt(radius, canvas.height - radius)
});
}
return positions;
}
function drawCircles(positions, color) {
positions.forEach(pos => {
ctx.beginPath();
ctx.arc(pos.x, pos.y, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
});
}
function checkAnswer() {
const guess = parseInt(guessInput.value);
if (guess === correctAnswer) {
feedback.textContent = '✔️ Correct!';
stopTimer();
times.push(timer);
updateAverageTime();
setTimeout(() => {
generateProblem();
feedback.textContent = '';
guessInput.value = '';
guessInput.focus();
startTimer();
}, 500);
} else {
feedback.textContent = `❌ Incorrect. Try again!`;
guessInput.focus();
}
}
function startTimer() {
timer = 0;
timerDisplay.textContent = 'Time: 0s';
clearInterval(timerInterval);
timerInterval = setInterval(() => {
timer++;
timerDisplay.textContent = `Time: ${timer}s`;
}, 1000);
}
function stopTimer() {
clearInterval(timerInterval);
}
function updateAverageTime() {
const sum = times.reduce((a, b) => a + b, 0);
const avg = (sum / times.length).toFixed(1);
avgTimeDisplay.textContent = `Avg: ${avg}s`;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
</body>
</html>