THIS QUESTION IS NOT FOR CHEGG USER SRUJANA. She did not look at the question cl
ID: 3859713 • Letter: T
Question
THIS QUESTION IS NOT FOR CHEGG USER SRUJANA. She did not look at the question closely and she used jython instead of python, and she did not follow the template and satisfy the requirements in the rubric. I had to repeat my question again on Chegg, but she came back and copied and pasted her wrong answer again. So, please, someone else answer this question. Please take this seriously and read the whole question.
(Python 3)
Please follow the template and the rubric within this question! Everything within the rubric at the bottom must be satisfied. No shortcuts or tricks like importing java.io*. This is a python 3 program, not a jython program.
Your task is to implement Python classes to represent and manipulate robots in a given square maze.
A robot has a name, a color and a position in the maze. It also has a battery that can be recharged.
The position of the robot is represented by a row and a column identifying one of the discrete squares in the maze.
For the robot depicted below, the row is 4 and the column is 2 (we start counting at 0).
We'll use a predefined 10 x 10 grid for our maze, but your methods must handle any size square maze with arbitrary obstacles. The maze is represented as a two dimensional list and is defined as a class variable. A True value at a given position indicates an open square whereas a False indicates the presence of an obstacle. For the underwater robots, we'll assume that the obstacles are the same at any depth.
Your task is to implement two classes: a Robot class, and an UnderwaterRobot class.
Robot class:
Your task is to implement the following methods for the Robot class:
__init__(self, name, color, row=0, column=0) method: this method will take the robot's name, color and initial position (row and column) as arguments. If the row or column are omitted, they will default to 0. The robot is created fully charged so the method will also initialize the robot's battery to the self.full class variable (specified in the template as 20 units). Note that the robot's battery is an instance variable with no corresponding argument in __init__.
__str__(self) method: this method will define the string representation of the robot object. We would like the representation to be of the form: name is a color robot lost in the maze. See the video for some examples.
__gt__(self, other) method: this method will compare two robot objects based on their battery charge. We would like robot1 > robot2 to return True if and only if robot1.battery > robot2.battery. Otherwise robot1 > robot2 should return False.
recharge(self) method: this method will recharge the robot's battery to the self.full class variable (specified in the template as 20 units). It will return the updated robot object.
one_step_forward(self), one_step_back(self), one_step_right(self): and one_step_left(self) methods. These methods move the robot one step in the corresponding direction if possible, and decrement the battery value by 1. A move may not be possible if the robot is out of battery, an obstacle is encountered or the boundary of the maze is reached.
backward(self, steps), forward(self, steps), right(self, steps) and left(self, steps) method. These methods take a number of steps as an argument and move the robot that many steps in the specified direction, as far as possible. These methods should call the corresponding one_step method repeatedly.
UnderwaterRobot class:
An underwater robot is a special kind of robot. An underwater robot has all the attributes of a robot (name, color, row, column and battery). However an underwater robot has one additional depth attribute. We'll assume that the depth is a positive integer. The __init__ method corresponding to the UnderwaterRobot class will have the following signature:
__init__(self, name, color, depth, row=0, column=0)
You'll implement the Underwater class in the module robot.py in a way that re-uses the methods in the Robot class wherever possible. Remember our DRY principle?
An underwater robot will have the same behavior as a regular robot. However the string representation of an underwater robot will be different than that of a regular robot. We would like the representation to be of the form: name is a color robot diving under water.
You will also implement a dive method in the Underwater class only:
dive(self, distance) - the dive method adds a given distance to the underwater robot's depth. The row and column attributes are not affected. We'll assume that the robot does not use any battery charge in this case.
Optional challenge:
Modify the __init__ method so that if the position specified is taken by an obstacle, the robot is placed in the closest available position.
Note that one way to do that is to create a class method (not an instance method), closest, that returns a (row, column) tuple representing the closest available position to the ideal row and column. Closeness is defined in terms of the Euclidian (straight line) distance.
closest(cls, ideal_row, ideal_column)
How do I get started?
FOLLOW THE TEMPLATE.
Use the template provided (see pictures).
Make sure you take out the pass statements and replace them with your code.
The method show has been provided to help you see where your robot is in the maze when you are testing. Zoom in on these pictures or open them in a new tab. In order, the one of the top left is page 1, top right is page 2, bottom left is page 3, bottom right is page 4.
Testing:
Make sure you test your class definitions. The screencasts below show how you can do that. I recommend that you fully implement the Robot class and test it first, then move on to the UnderwaterRobot class.
You can also run the following program to test your class definitions:
description.)
6 points: Documentation - method docstrings describe parameter & return value: Include a docstring for each non magic method, describing its use, its parameter(s) and return value, if any. Make sure you include the expected type for the parameter(s) and return value.
6 points: Robot class initialization: The instance variables name, color, row, column and battery are initialized correctly. The class variable self.full is used correctly to initialize the battery - not 20.
6 points: Robot class string representation: The __str__method defines the string representation of the robot object correctly. The representation is of the form: is a robot lost in the maze.
6 points: Comparison: robot1 > robot2 returns True if and only if robot1.battery > robot2.battery. Otherwise robot1 > robot2 returns False.
8 points: recharge method: The method updates the robot's battery using the self.full class variable. The method returns the updated robot object.
10 points: one_step_forward: The battery is checked correctly. The maze boundaries are checked correctly. The obstacles are checked correctly. The robot's battery is updated correctly. The robot's position is updated correctly.
10 points: one_step_back: The battery is checked correctly. The maze boundaries are checked correctly. The obstacles are checked correctly. The robot's battery is updated correctly. The robot's position is updated correctly.
10 points: one_step_right: The battery is checked correctly. The maze boundaries are checked correctly. The obstacles are checked correctly. The robot's battery is updated correctly. The robot's position is updated correctly.
10 points: one_step_left: The battery is checked correctly. The maze boundaries are checked correctly. The obstacles are checked correctly. The robot's battery is updated correctly. The robot's position is updated correctly.
6 points: forward: The method updates the robot object correctly. The method calls the corresponding one-step method repeatedly.
6 points: backward: The method updates the robot object correctly. The method calls the corresponding one-step method repeatedly.
6 points: right: The method updates the robot object correctly. The method calls the corresponding one-step method repeatedly.
6 points: left: The method updates the robot object correctly. The method calls the corresponding one-step method repeatedly.
10 points: The methods can handle any size maze.
6 points: UnderwaterRobot class initialization: The instance variables name, color, row, column, depth and battery are initialized correctly. The class variable self.full is used correctly to initialize the battery - not 20.
6 points: UnderwaterRobot class string representation: The __str__method defines the string representation of the robot object correctly. The representation is of the form: is a robot diving under water.
12 points: DRY implementation: The UnderwaterRobot class implementation re-uses the methods in the Robot class wherever possible.
5 points: dive: The dive method adds a given distance to the underwater robot's depth. The row, column and battery are not affected.
Wall-E in the Maze backward left right forwardExplanation / Answer
main.py
import tkinter
class Robot(object):
unit_size = 60 #define size of a block
# False represents an obstacle, True represents open space
maze = [[True, True, False, True, True, True, False, True, True, True],
[True, True, False, True, True, True, False, True, True, True],
[True, False, True, True, True, True, False, True, True, True],
[True, True, True, True, True, True, True, True, False, True],
[True, True, True, True, True, True, True, True, False, True],
[True, True, True, True, True, True, True, True, True, True],
[True, False, False, False, False, True, True, True, True, True],
[True, True, False, True, True, True, True, True, False, True],
[True, False, True, True, True, True, True, True, False, True],
[True, True, True, True, True, True, True, True, False, True]]
maze_size = len(maze)
# A robot with a fully charged battery can take up to 20 steps
full = 20
def __init__(self, name, color, row=0, column=0):
self.name = name
self.color = color
self.row = row
self.column = column
self.battery = self.full
def __str__(self):
return self.name + ' is a ' + self.color + ' robot lost in the maze'
def __gt__(self, other):
return self.battery > other.battery
def recharge(self):
self.battery = self.full
return self
def helper(self):
row = self.row
column = self.column
if (0 <= row < self.maze_size) and (0 <= column < self.maze_size) and self.maze[row][column]:
return True
else:
return False
def one_step_forward(self):
self.row += 1
if self.helper() and (self.battery != 0):
self.battery -= 1
return self
else:
self.row -= 1
return self
def one_step_back(self):
self.row -= 1
if self.helper() and (self.battery != 0):
self.battery -= 1
return self
else:
self.row += 1
return self
def one_step_right(self):
self.column += 1
if self.helper() and (self.battery != 0):
self.battery -= 1
return self
else:
self.column -= 1
return self
def one_step_left(self):
self.column -= 1
if self.helper() and (self.battery != 0):
self.battery -= 1
return self
else:
self.column += 1
return self
def forward(self, steps):
i = 1
while i <= steps:
self.one_step_forward()
i += 1
def backward(self, steps):
i = 1
while i <= steps:
self.one_step_back()
i += 1
def right(self, steps):
i = 1
while i <= steps:
self.one_step_right()
i += 1
def left(self, steps):
i = 1
while i <= steps:
self.one_step_left()
i += 1
def show(self):
"""
Draw a graphical representation of the robot in the maze.
Imported from Tkinter
"""
root = tkinter.Tk()
root.title (self.name + ' in the Maze')
canvas= tkinter.Canvas(root, background = 'light green',
width = self.unit_size * self.maze_size,
height = self.unit_size * self.maze_size)
canvas.grid() #draw the grid
#draw a representation of the robot in the maze
if self.battery: #appearance of robot with battery
upper_x = self.column * self.unit_size + self.unit_size / 4
upper_y = self.row * self.unit_size
lower_x = upper_x + self.unit_size / 2
lower_y = upper_y + self.unit_size
eye_x = lower_x - 3 * self.unit_size / 20
eye_y = upper_y + self.unit_size / 10
else: #appearance of robot without battery
upper_x = self.column * self.unit_size
upper_y = self.row * self.unit_size + self.unit_size / 2
lower_x = upper_x + self.unit_size
lower_y = upper_y + self.unit_size / 2
eye_x = lower_x - 9 * self.unit_size / 10
eye_y = lower_y - 3 * self.unit_size / 20
rectangle = canvas.create_rectangle(upper_x,
upper_y,
lower_x,
lower_y,
fill = self.color)
#draw the robot's eyes
canvas.create_oval(upper_x + self.unit_size / 10,
upper_y + self.unit_size / 10,
upper_x + 3 * self.unit_size / 20,
upper_y + 3 * self.unit_size / 20,
fill = 'black')
canvas.create_oval(eye_x,
eye_y,
eye_x + self.unit_size / 20,
eye_y + self.unit_size / 20,
fill = 'black')
# draw the obstacles in the maze
for row in range(self.maze_size):
for col in range(self.maze_size):
if not self.maze[row][col]:
canvas.create_rectangle(col * self.unit_size,
row * self.unit_size,
(col + 1) * self.unit_size,
(row + 1) * self.unit_size,
fill='red')
# draw lines for the grid
for row in range(self.maze_size):
canvas.create_line(0,
row * self.unit_size,
self.maze_size * self.unit_size,
row * self.unit_size)
for col in range(self.maze_size):
canvas.create_line(col * self.unit_size,
0,
col * self.unit_size,
self.maze_size * self.unit_size )
root.mainloop()
#2nd class for Underwater robot for depth
class UnderwaterRobot(Robot):
depth = 1
def __init__(self, name, color, depth, row=0, column=0):
self.depth = depth
super().__init__(name, color, row=0, column=0) #give underwater robot the features of regular robot
def __str__(self):
return self.name + ' is a ' + self.color + ' robot diving under water.'
def dive(self, distance):
"""
Allows the robot to dive deeper
"""
self.depth += distance
return self
======================================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.