Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(PYTHON 3) PLEASE FOLLOW DIRECTIONS AND THE TEMPLATE PROVIDED Please follow the template and the rubric within this question! Everything within the rubric at the

(PYTHON 3) PLEASE FOLLOW DIRECTIONS AND THE TEMPLATE PROVIDED 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).

image text in transcribed

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.

image text in transcribed

image text in transcribed

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:

image text in transcribed

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 forward Wall-E in the Maze backward left right forward

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions