Skip to content

Commit

Permalink
Refactor isAnswerCorrect function to include step parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatehito committed Jan 30, 2024
1 parent d7ccddb commit 0ef45b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/app/lib/solveProblem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ export function solveProblem(program: string): SolveProblemResult {
export function isAnswerCorrect(
problemProgram: string,
answerCharacters: CharacterClass[],
answerBoard: BoardClass
answerBoard: BoardClass,
step?: number
): boolean {
const answer = solveProblem(problemProgram);
const correctAnswer = solveProblem(problemProgram).histories?.at(step || -1);

if (!answer.characters || !answer.board) return false;
if (!correctAnswer || !correctAnswer.characters) return false;

// 順番は関係なく、id以外のキャラクターの状態が一致しているかチェック
const isCorrectCharacters: boolean = answer.characters.every((correctCharacter) => {
const isCorrectCharacters: boolean = correctAnswer.characters.every((correctCharacter) => {
const character = answerCharacters.find(
(answerCharacter) =>
answerCharacter.name === correctCharacter.name &&
Expand All @@ -102,7 +103,7 @@ export function isAnswerCorrect(
});

// すべてのセルの色が一致しているかチェック
const isCorrectBoard: boolean = answer.board.grid.every((rows, rowIndex) =>
const isCorrectBoard: boolean = correctAnswer.board.grid.every((rows, rowIndex) =>
rows.every((column, columnIndex) => {
const cell = answerBoard.grid[rowIndex][columnIndex];
return cell.color === column.color;
Expand Down
6 changes: 3 additions & 3 deletions src/components/organisms/TurtleGraphics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useState, forwardRef, useImperativeHandle, useEffect, useCallbac

import { Board } from '../../app/lib/Board';
import { Character } from '../../app/lib/Character';
import { isAnswerCorrect } from '../../app/lib/solveProblem';
import { isAnswerCorrect, solveProblem } from '../../app/lib/solveProblem';
import type { CellColor, CharacterDirection, SelectedCell } from '../../types';
import { TurtleGraphicsController } from '../molecules/TurtleGraphicsController';

Expand All @@ -31,7 +31,7 @@ export interface TurtleGraphicsHandle {
}

export const TurtleGraphics = forwardRef<TurtleGraphicsHandle, TurtleGraphicsProps>(
({ beforeCheckPointLine = 0, isEnableOperation = false, problemProgram }, ref) => {
({ beforeCheckPointLine = 0, currentCheckPointLine, isEnableOperation = false, problemProgram }, ref) => {
const [board, setBoard] = useState<Board>(new Board());
const [characters, setCharacters] = useState<Character[]>([]);
const [selectedCharacter, setSelectedCharacter] = useState<Character>();
Expand Down Expand Up @@ -80,7 +80,7 @@ export const TurtleGraphics = forwardRef<TurtleGraphicsHandle, TurtleGraphicsPro
};

const isCorrect = (): boolean => {
return isAnswerCorrect(problemProgram, characters, board);
return isAnswerCorrect(problemProgram, characters, board, currentCheckPointLine);
};

const handleClickCharacter = (character: Character): void => {
Expand Down

0 comments on commit 0ef45b6

Please sign in to comment.