mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-27 06:40:42 -05:00
feat: game logic complete
This commit is contained in:
parent
4902ed48f9
commit
a19e72afaf
2 changed files with 26 additions and 18 deletions
|
@ -17,8 +17,10 @@ export default function GameBoard(props) {
|
|||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (props.gameStarted) {
|
||||
fetchCards({ setCards });
|
||||
}, []);
|
||||
}
|
||||
}, [props.gameStarted]);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
|
|
@ -54,29 +54,35 @@ class GameLogic {
|
|||
}
|
||||
|
||||
updateScore(key) {
|
||||
if (this.score + 1 === this.cards.length) {
|
||||
alert("You won! 12/12 right");
|
||||
this.resetGame();
|
||||
return;
|
||||
}
|
||||
|
||||
// go through game
|
||||
if (!this.checkClick(key)) {
|
||||
this.setScore(this.score + 1);
|
||||
} else {
|
||||
console.log("Game Over");
|
||||
this.handleLoss();
|
||||
}
|
||||
}
|
||||
handleLoss() {
|
||||
alert(
|
||||
`Game Over! ${this.score + 1}/${this.cards.length} answers correcet!`,
|
||||
);
|
||||
if (this.score + 1 > this.highScore) {
|
||||
this.setHighScore(this.score);
|
||||
}
|
||||
this.resetGame();
|
||||
}
|
||||
|
||||
resetGame() {
|
||||
// reset state
|
||||
this.score = 0;
|
||||
this.setScore(this.score);
|
||||
this.setGameStarted(false);
|
||||
}
|
||||
}
|
||||
async fetchCards() {
|
||||
const cards = await fetch("http://localhost:8000/cards");
|
||||
const jsonCards = await cards.json();
|
||||
|
||||
let gameCards = await jsonCards.map((c) => {
|
||||
return { ...c, clicked: false, key: crypto.randomUUID() };
|
||||
});
|
||||
|
||||
this.setCards(gameCards);
|
||||
}
|
||||
}
|
||||
|
||||
export { GameLogic };
|
||||
|
|
Loading…
Reference in a new issue