Skip to content

Commit

Permalink
support letter removal
Browse files Browse the repository at this point in the history
  • Loading branch information
skedwards88 committed Nov 16, 2023
1 parent 85a95a1 commit b771148
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ function Letter({letter, letterAvailability, index, dispatchGameState}) {
function handlePointerEnter(e, index, letterAvailability) {
e.preventDefault();
if (!letterAvailability) {
return;
dispatchGameState({
action: "removeLetter",
letterIndex: index,
});
} else {
// Add the letter to the list of letters
dispatchGameState({
action: "addLetter",
letterIndex: index,
});
}

// Add the letter to the list of letters
dispatchGameState({
action: "addLetter",
letterIndex: index,
});
}

function handlePointerUp(e) {
Expand Down
20 changes: 20 additions & 0 deletions src/logic/gameReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ export function gameReducer(currentGameState, payload) {
playedIndexes: newPlayedIndexes,
result: "",
};
} else if (payload.action === "removeLetter") {
if (!currentGameState.wordInProgress) {
return currentGameState;
}
// Don't remove a letter if the player didn't go back to the letter before the last letter
let newPlayedIndexes = [...currentGameState.playedIndexes];
const lastIndexPlayed = newPlayedIndexes[newPlayedIndexes.length - 2];
if (lastIndexPlayed !== payload.letterIndex) {
return currentGameState;
}

newPlayedIndexes = currentGameState.playedIndexes.slice(
0,
newPlayedIndexes.length - 1
);

return {
...currentGameState,
playedIndexes: newPlayedIndexes,
};
} else if (payload.action === "endWord") {
// Since we end the word on board up or on app up (in case the user swipes off the board), we can end up calling this case twice.
// Return early if we no longer have a word in progress.
Expand Down

0 comments on commit b771148

Please sign in to comment.