-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/add-program-langage-selection
- Loading branch information
Showing
6 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Cell } from '../../types'; | ||
|
||
import type { Character } from './Character'; | ||
|
||
export class Board { | ||
grid: Cell[][]; | ||
constructor({ | ||
gridSize = [8, 12], | ||
}: { | ||
gridSize?: [number, number]; | ||
} = {}) { | ||
this.grid = this.createGrid(gridSize[1], gridSize[0]); | ||
} | ||
|
||
createGrid(numRows: number, numColumns: number): Cell[][] { | ||
const grid: Cell[][] = []; | ||
for (let i = 0; i < numRows; i++) { | ||
grid.push([]); | ||
for (let j = 0; j < numColumns; j++) { | ||
grid[i].push({ color: undefined }); | ||
} | ||
} | ||
return grid; | ||
} | ||
|
||
updateGrid(character: Character): void { | ||
const { cellColor, penDown, x, y } = character; | ||
if (penDown) this.grid[y - 1][x - 1].color = cellColor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { SolveProblemResult } from '../../types'; | ||
|
||
import { Board as BoardClass } from './Board'; | ||
import { Character as CharacterClass } from './Character'; | ||
|
||
export function parseProgram(program: string): string[] { | ||
return program | ||
.split('\n') | ||
.map((line) => line.trim()) | ||
.filter((line) => line !== ''); | ||
} | ||
|
||
export function executeEval(command: string): CharacterClass { | ||
const Character = CharacterClass; // eslint-disable-line | ||
const Board = BoardClass; // eslint-disable-line | ||
const characterVariableName = 'character'; | ||
const semicolonEndedCommand = (() => { | ||
if (command.endsWith(';')) return command; | ||
command += ';'; | ||
})(); | ||
const returnValueCommand = ` | ||
${characterVariableName}; | ||
`; | ||
|
||
const mergedCommand = semicolonEndedCommand + '\n' + returnValueCommand; | ||
|
||
return eval(mergedCommand); | ||
} | ||
|
||
export function solveProblem(program: string): SolveProblemResult { | ||
const commands = parseProgram(program); | ||
const character = new CharacterClass(); | ||
const board = new BoardClass(); | ||
const histories = [{ step: 0, character, board }]; | ||
|
||
for (let i = 0; i < commands.length; i++) { | ||
if (i < commands.length) { | ||
let mergedCommand = ''; | ||
|
||
for (let j = 0; j <= i; j++) { | ||
mergedCommand += commands[j]; | ||
} | ||
|
||
const character = executeEval(mergedCommand); | ||
|
||
board.updateGrid(character); | ||
histories.push({ step: histories.length + 1, character, board }); | ||
} | ||
} | ||
|
||
const result = { | ||
character: histories?.at(-1)?.character || character, | ||
board: histories?.at(-1)?.board || board, | ||
histories, | ||
}; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
import type React from 'react'; | ||
|
||
import type { Board } from './app/lib/Board'; | ||
import type { Character } from './app/lib/Character'; | ||
|
||
export interface LayoutProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export type LayoutComponent = React.FC<LayoutProps>; | ||
|
||
export type CellColor = 'red' | 'green' | 'blue'; | ||
export type Cell = { | ||
color: CellColor | undefined; | ||
}; | ||
|
||
export type History = { | ||
step: number; | ||
character: Character; | ||
board: Board; | ||
}; | ||
|
||
export type SolveProblemResult = { | ||
character: Character; | ||
board: Board; | ||
histories: History[] | undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { solveProblem, parseProgram } from '../src/app/lib/solveProblem'; | ||
|
||
test('Parse a program', () => { | ||
const program = ` | ||
const character = new Character(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
`; | ||
const parsedProgram = parseProgram(program); | ||
|
||
expect(parsedProgram).not.toBeFalsy(); | ||
expect(parsedProgram).toEqual([ | ||
'const character = new Character();', | ||
'character.moveForward();', | ||
'character.moveForward();', | ||
'character.moveForward();', | ||
'character.moveForward();', | ||
'character.moveForward();', | ||
]); | ||
}); | ||
|
||
test('Solve a problem', () => { | ||
const problemProgram = ` | ||
const character = new Character(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
character.moveForward(); | ||
`; | ||
|
||
const answer = solveProblem(problemProgram); | ||
|
||
expect(answer).not.toBeFalsy(); | ||
expect(answer.character).not.toBeFalsy(); | ||
expect(answer.board).not.toBeFalsy(); | ||
expect(answer.character.direction).toEqual('down'); | ||
expect(answer.character.penDown).toEqual(true); | ||
|
||
// prettier-ignore | ||
expect(answer.board.grid).toEqual([ | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: 'red' }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
[{ color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }, { color: undefined }], | ||
]); | ||
expect(answer.histories).not.toBeFalsy(); | ||
expect(answer.histories?.at(-1)?.character.x).toEqual(1); | ||
expect(answer.histories?.at(-1)?.character.y).toEqual(6); | ||
}); |