Skip to content

Commit

Permalink
feat: Finished first stage of chapter 5, needs more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LyonSyonII committed Mar 2, 2024
1 parent 45591fb commit 8b58fe4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
41 changes: 41 additions & 0 deletions frontend/src/validation/5-8-robot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Functions, type RobotGameProps } from "@components/RobotGame/RobotGame";
import { codeMess, replace } from "./CodeQuestion";
import { createRegExp } from "magic-regexp";
import { _, end, line, semicolon, start } from "./regex";

export default {
solveWithMinimumSteps: true,
functions: Functions.LOOK_HORIZONTAL,
rows: 3,
cols: 3,
boards: [
{
start: 7,
enemies: [3],
steps: 2
},
{
start: 7,
enemies: [1],
steps: 2
},
],
validator
} as RobotGameProps;

function validator(value: string): string | undefined {
const regex = createRegExp(
start, _,
"up()", semicolon,
"if", _, "is_slime_left()", _, "{", _, line, _, line.as("extra1"), _, "}", _, "else", _, "{", _, line, _, line.as("extra2"), _, "}", _,
end
);
const matches = value.match(regex);
if (!matches) return codeMess;

const { extra1, extra2 } = matches.groups;

return value.includes("?") && replace
|| (extra1 || extra2) && "You don't need more than one line in each condition!"
|| undefined
}
54 changes: 54 additions & 0 deletions frontend/src/validation/5-9-robot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Functions, type RobotGameProps } from "@components/RobotGame/RobotGame";
import { codeMess, replace } from "./CodeQuestion";
import { createRegExp, exactly, maybe, word } from "magic-regexp";
import { _, end, line, semicolon, start } from "./regex";

export default {
solveWithMinimumSteps: true,
functions: Functions.LOOK_HORIZONTAL,
rows: 3,
cols: 3,
boards: [
{
start: 7,
enemies: [3],
steps: 2
},
{
start: 7,
enemies: [1],
steps: 2
},
{
start: 7,
enemies: [5],
steps: 2
},
],
validator
} as RobotGameProps;

function validator(value: string): string | undefined {
const condition = exactly("?").or(word, "()", maybe(";"));
const regex = createRegExp(
start, _,
"up()", semicolon,
"if", _, condition.as("first"), _, "{", _, line, _, line.as("extra1"), _, "}", _,
"else if", _, condition.as("second"), _, "{", _, line, _, line.as("extra2"), _, "}", _,
"else", _, "{", _, line, _, line.as("extra3"), _, "}", _,
end
);
const matches = value.match(regex);
if (!matches) return codeMess;

const { first, second, extra1, extra2, extra3 } = matches.groups;
if (!first || !second) return codeMess;

const wrong = "Look closely at the instructions, you don't need a semicolon!";

return value.includes("?") && replace
|| (extra1 || extra2 || extra3) && "You don't need more than one line in each condition!"
|| first.includes(";") && `[first condition] ${wrong}`
|| second.includes(";") && `[second condition] ${wrong}`
|| undefined
}

0 comments on commit 8b58fe4

Please sign in to comment.