Add the random-walk and spirograph challenges to the command line

turtle-challenges random-walk and turtle-challenges spirograph, the latter with
--gap for the degrees between two circles.

Task: MIL-004#4
This commit is contained in:
2026-10-07 23:59:15 +08:00
parent 2328490945
commit b5483a7373
3 changed files with 45 additions and 4 deletions
+18 -3
View File
@@ -10,12 +10,17 @@ from functools import partial
from turtle_challenges.constants import (
CHALLENGE_DASHED_LINE,
CHALLENGE_NAMES,
CHALLENGE_RANDOM_WALK,
CHALLENGE_SHAPES,
CHALLENGE_SPIROGRAPH,
CHALLENGE_SQUARE,
SPIROGRAPH_GAP_DEGREES,
)
from turtle_challenges.dashed_line import draw_dashed_line
from turtle_challenges.pen import Pen
from turtle_challenges.random_walk import random_walk
from turtle_challenges.shapes import draw_shapes
from turtle_challenges.spirograph import draw_spirograph
from turtle_challenges.square import draw_square
from turtle_challenges.window import (
TurtleUnavailableError,
@@ -28,7 +33,7 @@ def build_parser() -> argparse.ArgumentParser:
"""!
@brief Build the argument parser of the command line.
@return A parser with the challenge name.
@return A parser with the challenge name and the spirograph gap option.
"""
parser = argparse.ArgumentParser(
prog="turtle-challenges",
@@ -40,14 +45,22 @@ def build_parser() -> argparse.ArgumentParser:
choices=CHALLENGE_NAMES,
help="the challenge to run",
)
parser.add_argument(
"--gap",
type=float,
default=SPIROGRAPH_GAP_DEGREES,
help="spirograph only: degrees between two circles "
f"(default: {SPIROGRAPH_GAP_DEGREES})",
)
return parser
def build_challenges(pen: Pen) -> dict[str, Callable[[], None]]:
def build_challenges(pen: Pen, gap: float) -> dict[str, Callable[[], None]]:
"""!
@brief Bind every challenge to a turtle.
@param pen The turtle that draws.
@param gap Degrees between two circles of the spirograph.
@return A mapping from the command line name to a function without
arguments that draws the challenge.
"""
@@ -55,6 +68,8 @@ def build_challenges(pen: Pen) -> dict[str, Callable[[], None]]:
CHALLENGE_SQUARE: partial(draw_square, pen),
CHALLENGE_DASHED_LINE: partial(draw_dashed_line, pen),
CHALLENGE_SHAPES: partial(draw_shapes, pen),
CHALLENGE_RANDOM_WALK: partial(random_walk, pen),
CHALLENGE_SPIROGRAPH: partial(draw_spirograph, pen, gap),
}
@@ -72,6 +87,6 @@ def main(argv: Sequence[str] | None = None) -> int:
tim = create_pen()
except TurtleUnavailableError as error:
parser.exit(1, f"{parser.prog}: error: {error}\n")
build_challenges(tim)[args.challenge]()
build_challenges(tim, args.gap)[args.challenge]()
window.exitonclick()
return 0
+8
View File
@@ -92,9 +92,17 @@ CHALLENGE_DASHED_LINE = "dashed-line"
## Command line name of challenge 3.
CHALLENGE_SHAPES = "shapes"
## Command line name of challenge 4.
CHALLENGE_RANDOM_WALK = "random-walk"
## Command line name of challenge 5.
CHALLENGE_SPIROGRAPH = "spirograph"
## Names of the challenges on the command line, in the order of the course.
CHALLENGE_NAMES: tuple[str, ...] = (
CHALLENGE_SQUARE,
CHALLENGE_DASHED_LINE,
CHALLENGE_SHAPES,
CHALLENGE_RANDOM_WALK,
CHALLENGE_SPIROGRAPH,
)