Added a small mental arithmatic game
All checks were successful
/ publish (push) Successful in 1m30s
All checks were successful
/ publish (push) Successful in 1m30s
This commit is contained in:
parent
ca67aadac0
commit
70a782d0e4
|
|
@ -28,6 +28,7 @@
|
||||||
<li><a href="/scorecard-4p/">Generic Scorecard - 4 Players</a></li>
|
<li><a href="/scorecard-4p/">Generic Scorecard - 4 Players</a></li>
|
||||||
<li><a href="/mahjong/">Mahjong Scorecard</a></li>
|
<li><a href="/mahjong/">Mahjong Scorecard</a></li>
|
||||||
<li><a href="/finska/">Finska Scorecard</a></li>
|
<li><a href="/finska/">Finska Scorecard</a></li>
|
||||||
|
<li><a href="/mental-arithmatic/">Mental Arithmatic Game</a></li>
|
||||||
<li><a href="/neon-snake/">Neon Snake</a>: vibe-coded by Google Gemini</li>
|
<li><a href="/neon-snake/">Neon Snake</a>: vibe-coded by Google Gemini</li>
|
||||||
</ul>
|
</ul>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
||||||
42
site/mental-arithmatic/index.html
Normal file
42
site/mental-arithmatic/index.html
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Mental Arithmatic - Tools</title>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css">
|
||||||
|
<link rel="stylesheet" href="./style.css">
|
||||||
|
<script src="./scripts/main.js" type="module"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="welcome-card" class="container" data-controller="welcome"
|
||||||
|
data-action="endGame@window->welcome#gameEnded">
|
||||||
|
<header>
|
||||||
|
<hgroup>
|
||||||
|
<h1>Mental Arithmatic</h1>
|
||||||
|
<p>A simple mental arithmetic game</p>
|
||||||
|
</hgroup>
|
||||||
|
<main>
|
||||||
|
<button data-action="click->welcome#startGame">Start Game</button>
|
||||||
|
</main>
|
||||||
|
</header>
|
||||||
|
</div>
|
||||||
|
<div id="game-card" class="container game-card hidden" data-controller="game"
|
||||||
|
data-action="startGame@window->game#start">
|
||||||
|
<header>
|
||||||
|
<span data-game-target="clock">1:00</span>
|
||||||
|
<span data-game-target="score">0</span>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<div class="question">
|
||||||
|
<div data-game-target="problem"></div>
|
||||||
|
<div class="answer" data-game-target="indicators">
|
||||||
|
<span class="indicator right">✔︎</span>
|
||||||
|
<span class="indicator wrong">❌</span>
|
||||||
|
<input type="number" data-game-target="answer" value="0" data-action="keydown.enter->game#submitAnswer">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
104
site/mental-arithmatic/scripts/game.js
Normal file
104
site/mental-arithmatic/scripts/game.js
Normal file
|
|
@ -0,0 +1,104 @@
|
||||||
|
import { Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
|
||||||
|
|
||||||
|
export class GameController extends Controller {
|
||||||
|
static targets = [
|
||||||
|
"clock",
|
||||||
|
"score",
|
||||||
|
"problem",
|
||||||
|
"indicators",
|
||||||
|
"answer",
|
||||||
|
];
|
||||||
|
|
||||||
|
start() {
|
||||||
|
this.startGame();
|
||||||
|
this.element.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
submitAnswer(ev) {
|
||||||
|
ev.preventDefault();
|
||||||
|
|
||||||
|
this._checkAnswer();
|
||||||
|
}
|
||||||
|
|
||||||
|
startGame() {
|
||||||
|
this._clock = 120;
|
||||||
|
this._score = 0;
|
||||||
|
|
||||||
|
this._generateProblem();
|
||||||
|
this._startClock();
|
||||||
|
|
||||||
|
this._updateClock();
|
||||||
|
}
|
||||||
|
|
||||||
|
_startClock() {
|
||||||
|
this._clockInterval = window.setInterval(() => {
|
||||||
|
if (this._clock <= 0) {
|
||||||
|
this._gameOver();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._clock -= 1;
|
||||||
|
this._updateClock();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
_gameOver() {
|
||||||
|
window.clearInterval(this._clockInterval);
|
||||||
|
|
||||||
|
this.element.classList.add('hidden');
|
||||||
|
window.dispatchEvent(new CustomEvent("endGame"));
|
||||||
|
}
|
||||||
|
|
||||||
|
_generateProblem() {
|
||||||
|
const num1 = Math.floor(Math.random() * 99) + 1;
|
||||||
|
const num2 = Math.floor(Math.random() * 99) + 1;
|
||||||
|
|
||||||
|
this._problem = [num1, num2];
|
||||||
|
this._answer = num1 + num2;
|
||||||
|
|
||||||
|
this.indicatorsTarget.classList.remove('right', 'wrong');
|
||||||
|
this.problemTarget.innerHTML = '';
|
||||||
|
|
||||||
|
for (let i = 0; i < this._problem.length; i++) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
|
||||||
|
if (i === this._problem.length - 1) {
|
||||||
|
div.textContent = '+ ' + this._problem[i];
|
||||||
|
} else {
|
||||||
|
div.textContent = this._problem[i];
|
||||||
|
}
|
||||||
|
this.problemTarget.appendChild(div);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.answerTarget.disabled = false;
|
||||||
|
this.answerTarget.value = "";
|
||||||
|
this.answerTarget.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
_checkAnswer() {
|
||||||
|
let isRight = parseInt(this.answerTarget.value) === this._answer;
|
||||||
|
|
||||||
|
this.answerTarget.disabled = true;
|
||||||
|
|
||||||
|
let delay = 500;
|
||||||
|
|
||||||
|
if (isRight) {
|
||||||
|
this._score += 1;
|
||||||
|
this.indicatorsTarget.classList.add('right');
|
||||||
|
} else {
|
||||||
|
this.indicatorsTarget.classList.add('wrong');
|
||||||
|
this.answerTarget.value = this._answer;
|
||||||
|
delay = 800;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.scoreTarget.textContent = this._score;
|
||||||
|
|
||||||
|
window.setTimeout(() => { this._generateProblem(); }, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
_updateClock() {
|
||||||
|
let m = Math.floor(this._clock / 60);
|
||||||
|
let s = this._clock % 60;
|
||||||
|
this.clockTarget.textContent = `${m}:${s < 10 ? '0' : ''}${s}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
site/mental-arithmatic/scripts/main.js
Normal file
8
site/mental-arithmatic/scripts/main.js
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Application } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
|
||||||
|
import { WelcomeController } from "./welcome.js"
|
||||||
|
import { GameController } from "./game.js"
|
||||||
|
|
||||||
|
window.Stimulus = Application.start();
|
||||||
|
|
||||||
|
window.Stimulus.register('welcome', WelcomeController);
|
||||||
|
window.Stimulus.register('game', GameController);
|
||||||
14
site/mental-arithmatic/scripts/welcome.js
Normal file
14
site/mental-arithmatic/scripts/welcome.js
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
|
||||||
|
|
||||||
|
export class WelcomeController extends Controller {
|
||||||
|
startGame(ev) {
|
||||||
|
ev.preventDefault();
|
||||||
|
|
||||||
|
this.element.classList.add('hidden');
|
||||||
|
window.dispatchEvent(new CustomEvent("startGame"));
|
||||||
|
}
|
||||||
|
|
||||||
|
gameEnded(ev) {
|
||||||
|
this.element.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
}
|
||||||
99
site/mental-arithmatic/style.css
Normal file
99
site/mental-arithmatic/style.css
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
.hidden {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
min-height: 100dvh;
|
||||||
|
--pico-form-element-disabled-opacity: 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 80dvh;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card header,
|
||||||
|
.game-card footer {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
|
||||||
|
font-size: 2em;
|
||||||
|
margin-block: 12px;
|
||||||
|
margin-inline: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card main {
|
||||||
|
flex-grow: 1;
|
||||||
|
flex-shrink: 1;
|
||||||
|
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .question {
|
||||||
|
margin: auto;
|
||||||
|
width: 60vw;
|
||||||
|
|
||||||
|
text-align: right;
|
||||||
|
font-size: 3em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer {
|
||||||
|
border-block: 2px solid black;
|
||||||
|
padding-block: 2px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.right {
|
||||||
|
border-color: green;
|
||||||
|
color: green;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.wrong {
|
||||||
|
border-color: red;
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer .indicator {
|
||||||
|
display: none;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.right .indicator.right {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.wrong .indicator.wrong {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .question input {
|
||||||
|
border-inline: none;
|
||||||
|
border-radius: 0;
|
||||||
|
box-shadow: none;
|
||||||
|
|
||||||
|
border: none;
|
||||||
|
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
|
||||||
|
text-align: right;
|
||||||
|
font-size: 1em;
|
||||||
|
height: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .question input:focus {
|
||||||
|
border-none: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.right input {
|
||||||
|
color: green;
|
||||||
|
}
|
||||||
|
|
||||||
|
.game-card .answer.wrong input {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue