diff --git a/frontend/src/validation/5-8-robot.ts b/frontend/src/validation/5-8-robot.ts new file mode 100644 index 0000000..f0f137b --- /dev/null +++ b/frontend/src/validation/5-8-robot.ts @@ -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 +} \ No newline at end of file diff --git a/frontend/src/validation/5-9-robot.ts b/frontend/src/validation/5-9-robot.ts new file mode 100644 index 0000000..94d5f90 --- /dev/null +++ b/frontend/src/validation/5-9-robot.ts @@ -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 +} \ No newline at end of file