Skip to content

Commit

Permalink
Refactor isCorrect function to use isAnswerCorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatehito committed Jan 29, 2024
1 parent 78cc14f commit 37d613a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
35 changes: 35 additions & 0 deletions src/app/lib/solveProblem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,38 @@ export function solveProblem(program: string): SolveProblemResult {
};
return result;
}

export function isAnswerCorrect(
problemProgram: string,
answerCharacters: CharacterClass[],
answerBoard: BoardClass
): boolean {
const answer = solveProblem(problemProgram);

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

// 順番は関係なく、id以外のキャラクターの状態が一致しているかチェック
const isCorrectCharacters: boolean = answer.characters.every((correctCharacter) => {
const character = answerCharacters.find(
(answerCharacter) =>
answerCharacter.name === correctCharacter.name &&
answerCharacter.x === correctCharacter.x &&
answerCharacter.y === correctCharacter.y &&
answerCharacter.direction === correctCharacter.direction &&
answerCharacter.color === correctCharacter.color &&
answerCharacter.penDown === correctCharacter.penDown &&
answerCharacter.path?.join(',') === correctCharacter.path?.join(',')
);
return character;
});

// すべてのセルの色が一致しているかチェック
const isCorrectBoard: boolean = answer.board.grid.every((rows, rowIndex) =>
rows.every((column, columnIndex) => {
const cell = answerBoard.grid[rowIndex][columnIndex];
return cell.color === column.color;
})
);

return isCorrectCharacters && isCorrectBoard;
}
33 changes: 2 additions & 31 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, useMemo } from 'react

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

Expand Down Expand Up @@ -79,36 +79,7 @@ export const TurtleGraphics = forwardRef<TurtleGraphicsHandle, TurtleGraphicsPro
};

const isCorrect = (): boolean => {
const answer = solveProblem(problemProgram);

// TODO: 正答を取得する処理ができたら置き換える
const correctCharacters = answer.characters;
if (!correctCharacters) return false;
// 順番は関係なく、name, x, y, direction, color、penDownが一致していれば正解
const isCorrectCharacters = correctCharacters.every((correctCharacter) => {
const character = characters.find((character) => character.name === correctCharacter.name);

if (!character) return false;

return (
character.x === correctCharacter.x &&
character.y === correctCharacter.y &&
character.direction === correctCharacter.direction &&
character.color === correctCharacter.color &&
character.penDown === correctCharacter.penDown
);
});

const correctBoard = answer.board;
// すべてのセルの色が一致していれば正解
const isCorrectBoard = correctBoard.grid.every((rows, rowIndex) =>
rows.every((column, columnIndex) => {
const cell = board.grid[rowIndex][columnIndex];
return cell.color === column.color;
})
);

return isCorrectCharacters && isCorrectBoard;
return isAnswerCorrect(problemProgram, characters, board);
};

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

0 comments on commit 37d613a

Please sign in to comment.