Skip to content

Commit c4394bd

Browse files
committed
WIP: Implement Counter game
1 parent 85bb280 commit c4394bd

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed
Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,86 @@
1-
import React, { useState } from 'react';
1+
import React from "react";
2+
import { create } from "zustand";
23

3-
function CounterFrame() {
4-
const numbers = [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15];
5-
const [currentIndex, setCurrentIndex] = useState(0);
6-
const [counters, setCounters] = useState([0, 0, 0, 0]);
7-
const [processedLast, setProcessedLast] = useState(false);
4+
const useCounterStore = create((set) => ({
5+
numbers: [1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1],
6+
step: 0,
7+
guesses: new Map([
8+
[
9+
1,
10+
{
11+
correct: 0,
12+
incorrect: 0,
13+
},
14+
],
15+
[
16+
2,
17+
{
18+
correct: 0,
19+
incorrect: 0,
20+
},
21+
],
22+
[
23+
3,
24+
{
25+
correct: 0,
26+
incorrect: 0,
27+
},
28+
],
29+
[
30+
4,
31+
{
32+
correct: 0,
33+
incorrect: 0,
34+
},
35+
],
36+
]),
37+
increment: (guess) =>
38+
set((state) => {
39+
if (state.step === state.numbers.length - 1) {
40+
return state;
41+
}
42+
if (![1, 2, 3, 4].includes(guess)) {
43+
return state;
44+
}
45+
const updatedGuesses = new Map(state.guesses);
46+
let v = state.guesses.get(state.numbers[state.step]);
47+
if (guess === state.numbers[state.step]) {
48+
v.correct++;
49+
} else {
50+
v.incorrect++;
51+
}
52+
updatedGuesses.set(guess, v);
53+
return { step: state.step + 1, guesses: updatedGuesses };
54+
}),
55+
}));
856

57+
function CounterFrame() {
58+
const currentNumber = useCounterStore((state) => state.numbers[state.step]);
59+
const increment = useCounterStore((state) => state.increment);
60+
const guesses = useCounterStore((state) => state.guesses);
961

1062
const handleButtonClick = (counterIndex) => {
11-
//Checks to make sure we haven't iterated through the last element yet
12-
if(!processedLast) {
13-
// Makes a copy of the current counters array
14-
const updatedCounters = [...counters];
15-
updatedCounters[counterIndex] = updatedCounters[counterIndex] + 1;
16-
setCounters(updatedCounters);
17-
18-
if (currentIndex < numbers.length - 1) {
19-
setCurrentIndex(currentIndex + 1);
20-
}
21-
else {
22-
setProcessedLast(true);
23-
}
24-
}
63+
increment(counterIndex);
2564
};
2665

2766
return (
2867
<div>
29-
<h2>Current Number: {numbers[currentIndex]}</h2>
68+
<h2>Current Number: {currentNumber}</h2>
3069
<div>
31-
{counters.map((count, index) => (
70+
{Array.from(guesses, ([val, { correct, incorrect }]) => (
3271
<button
33-
key={index}
34-
onClick={() => handleButtonClick(index)}
35-
style={{ marginRight: '10px', marginTop: '10px' }}
72+
key={val}
73+
onClick={() => handleButtonClick(val)}
74+
style={{ marginRight: "10px", marginTop: "10px" }}
3675
>
37-
Counter {index + 1}: {count}
76+
{val}
77+
correct: {correct}
78+
incorrect: {incorrect}
3879
</button>
3980
))}
4081
</div>
4182
</div>
4283
);
4384
}
4485

45-
export default CounterFrame;
86+
export default CounterFrame;

0 commit comments

Comments
 (0)