Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle hint mode refactor #252

Merged
merged 7 commits into from
Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions app/Components/Sudoku Board/SudokuBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ const SudokuBoard = (props: any) => {
};

const toggleHintMode = () => {
// Create new board variable to store the temporary hint board state
let newBoard = board;
let newHintMode = !newBoard.get("inHintMode");
// Stores whether or not the board is in hint mode
let newHintMode: boolean = !newBoard.get("inHintMode");
newBoard = newBoard.set("inHintMode", newHintMode);

// Increment global hint value by one
Expand All @@ -326,6 +328,7 @@ const SudokuBoard = (props: any) => {
}
}

// If they are exiting hint mode, update board state by either reverting (if they prematurely exit hint mode) or updating (if they are on the final step)
if (!newHintMode) {
let hintStepsLength = newBoard.get("hintSteps").length;
let currentStep = newBoard.get("currentStep");
Expand All @@ -343,43 +346,33 @@ const SudokuBoard = (props: any) => {
else setBoard(newBoard);
return;
}

// If they are entering hint mode, update board state by adding the hint

newBoard = newBoard.set("currentStep", 0);
let hint = solution
? getNextHint(newBoard, solution, props.strategies)
: getNextHint(newBoard, null, props.strategies);
let hint = getNextHint(newBoard, solution, props.strategies);

if (!hint) return;
const words = hint.strategy.toLowerCase().replaceAll("_", " ").split(" ");
for (let i = 0; i < words.length; i++)
words[i] = words[i][0].toUpperCase() + words[i].substr(1);
let hintStratName = words.join(" ");
newBoard = newBoard.set("hintStratName", hintStratName);
const hintInfo = hint.info;
newBoard = newBoard.set("hintInfo", hintInfo);
const hintAction = hint.action;
newBoard = newBoard.set("hintAction", hintAction);

let causes = [];
let groups = [];
let placements = [];
let removals = [];
if (hint) {
if (hint.cause) causes = getCausesFromHint(hint);
if (hint.groups) groups = getGroupsFromHint(hint);
if (hint.placements) placements = getPlacementsFromHint(hint);
if (hint.removals) removals = getRemovalsFromHint(board, hint);
}
newBoard = newBoard.set("hintInfo", hint.info);
newBoard = newBoard.set("hintAction", hint.action);

let removals = getRemovalsFromHint(board, hint);

if (hint.strategy === "AMEND_NOTES") {
newBoard = addEveryRemovalNoteToBoard(newBoard, removals);
}

let hintObject: Hint = getHintObject(
hint.strategy,
groups,
causes,
getGroupsFromHint(hint),
getCausesFromHint(hint),
removals,
placements
getPlacementsFromHint(hint)
);

let hintSteps: any[] = hintObject.getHintSteps();
Expand Down