-
-
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.
feat: Finished first stage of chapter 5, needs more testing
- Loading branch information
1 parent
45591fb
commit 8b58fe4
Showing
2 changed files
with
95 additions
and
0 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,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 | ||
} |
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,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 | ||
} |