Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Josiah Wang
70053 Robot Project Sample Implementation
Commits
901082fa
Commit
901082fa
authored
Oct 29, 2021
by
Josiah Wang
Browse files
Added a LeapRobot class
parent
229ae251
Changes
2
Hide whitespace changes
Inline
Side-by-side
robot.py
View file @
901082fa
...
...
@@ -180,5 +180,38 @@ def _test_robot_init():
assert
robot
.
direction
==
direction
class
LeapingRobot
(
Robot
):
""" Robot that leaps straight to the edge of a grid. """
def
leap_forward
(
self
):
""" Make robot leap forward to the edge in the direction it is facing."""
direction
=
self
.
direction
row
=
self
.
position
[
0
]
col
=
self
.
position
[
1
]
if
direction
==
"n"
:
self
.
position
=
(
0
,
col
)
elif
direction
==
"s"
:
self
.
position
=
(
self
.
grid
.
size
-
1
,
col
)
elif
direction
==
"w"
:
self
.
position
=
(
row
,
0
)
elif
direction
==
"e"
:
self
.
position
=
(
row
,
self
.
grid
.
size
-
1
)
self
.
report_leap
()
def
report_leap
(
self
):
print
(
"Leaping forward to the edge."
)
def
move_forward_to_wall
(
self
):
""" Move robot forward in the current direction until it hits a wall.
Returns:
tuple : (row, column) coordinate after hitting a wall
"""
self
.
leap_forward
()
self
.
report_location
()
if
__name__
==
"__main__"
:
_test_robot_init
()
\ No newline at end of file
robot_factory.py
View file @
901082fa
...
...
@@ -5,7 +5,7 @@ Contains the RobotFactory class for creating a new robot.
import
random
from
robot
import
Robot
from
robot
import
Robot
,
LeapingRobot
class
RobotFactory
:
def
__init__
(
self
,
grid
,
candidate_names
=
[],
prev_id
=-
1
):
...
...
@@ -53,13 +53,21 @@ class RobotFactory:
identifier
=
self
.
_generate_id
()
initial_position
=
self
.
_generate_position
()
initial_direction
=
self
.
_generate_direction
()
return
Robot
(
identifier
=
identifier
,
name
=
name
,
position
=
initial_position
,
direction
=
initial_direction
,
grid
=
self
.
grid
)
choice
=
random
.
randint
(
0
,
1
)
if
choice
==
0
:
return
Robot
(
identifier
=
identifier
,
name
=
name
,
position
=
initial_position
,
direction
=
initial_direction
,
grid
=
self
.
grid
)
else
:
return
LeapingRobot
(
identifier
=
identifier
,
name
=
name
,
position
=
initial_position
,
direction
=
initial_direction
,
grid
=
self
.
grid
)
def
_generate_name
(
self
):
""" Select a robot's name at random from a given list
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment