Skip to content

Commit 872206a

Browse files
authored
Merge pull request #17 from elpddev/elpddev/issue8
fix space not considered char in the game
2 parents 49028a6 + 109b688 commit 872206a

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

src/components/LetterCard.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function styleLetterCard(
4343
backgroundColor,
4444
fontSize: "28px",
4545
fontFamily: "monospace",
46+
whiteSpace: "pre",
4647
};
4748
}
4849

src/components/TypingGame.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function TypingGame() {
2525
);
2626
}
2727

28-
const gameLength = 10_000;
28+
const gameLength = 60_000;
2929

3030
function useTypingGame() {
3131
const {
@@ -46,7 +46,7 @@ function useTypingGame() {
4646
[]
4747
);
4848

49-
const { startCapture, stopCapture, isCapturing } = useTypingCapture(onTyping);
49+
const { startCapture, stopCapture } = useTypingCapture(onTyping);
5050

5151
const restart = useCallback(() => {
5252
setBoard(init());

src/components/WordsCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function WordsCard({ board }: { board: Board }) {
1212
}}
1313
p={15}
1414
>
15-
<Group position="center" spacing={12}>
15+
<Group spacing={0} align="center">
1616
{board.words.map((word, index) => (
1717
<WordCard
1818
key={index}

src/models/Board.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, expect, it } from "vitest";
2+
import { moveByKey } from "./Board";
3+
4+
describe("Board", () => {
5+
describe("moveByKey", () => {
6+
it("", () => {});
7+
});
8+
});

src/models/Board.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as WordMod from "./Word";
55
import { Word } from "./Word";
66
import * as LetterMod from "./Letter";
77
import { SuccessStatus } from "./SuccessStatus";
8+
import { Letter } from "./Letter";
89

910
export interface Board {
1011
words: Word[];
@@ -35,15 +36,24 @@ export function clone(board: Board): Board {
3536
}
3637

3738
export function generateWords(amount: number): Word[] {
38-
const gameWords = [];
39+
const gameWords: Word[] = [];
3940

4041
for (let i = 0; i < amount; i += 1) {
4142
const nextWord = getRandomItem(wordBank);
4243
const nextGameWord = WordMod.init(nextWord);
4344
gameWords.push(nextGameWord);
4445
}
4546

46-
return gameWords;
47+
const first = gameWords.shift();
48+
49+
const gameWordsWithSpaces = gameWords.reduce(
50+
(all, word) => {
51+
return [...all, WordMod.init(" "), word];
52+
},
53+
[first] as Word[]
54+
);
55+
56+
return gameWordsWithSpaces;
4757
}
4858

4959
export function moveByKey(char: string, code: KeyCode, board: Board): Board {
@@ -55,7 +65,9 @@ export function moveByKey(char: string, code: KeyCode, board: Board): Board {
5565
return moveBackward(board);
5666
}
5767

58-
if (code === KeyCode.Space) {
68+
const currLetter = getCurrentLetter(board);
69+
70+
if (code === KeyCode.Space && currLetter.char !== " ") {
5971
if (board.currentWordLetterIndex === 0) {
6072
return board;
6173
}
@@ -216,3 +228,10 @@ export function getLetterSuccessAmount(board: Board) {
216228
return wordsAcc + wordSum;
217229
}, 0);
218230
}
231+
232+
function getCurrentLetter(board: Board): Letter {
233+
const currWord = board.words[board.currentWordIndex];
234+
const currLetter = currWord.letters[board.currentWordLetterIndex];
235+
236+
return currLetter;
237+
}

src/utils/useTypingCapture.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export function useTypingCapture(
77
const [isCapturing, setIsCapturing] = useState(false);
88

99
const typingCallback = useCallback((event: KeyboardEvent) => {
10+
event.stopPropagation();
1011
onCapture({ code: event.code as KeyCode, char: event.key });
1112
}, []);
1213

0 commit comments

Comments
 (0)