Create Custom class Robot(): in python 3

User Generated

nopnopnop1993

Programming

Description

A robot is located on the (x,y) plane and faces one of four directions (up, down, left, or
right). You must fill in all the missing methods described below.

1. The constructor takes 3 arguments. The first two are integers and correspond to the
initial x and y coordinates of the robot. The third argument is a single character string
containing one of the characters U, u, D, d, R, r, L, or l. These characters correspond to
the initial direction that the robot is facing (up, down, right, or left).

2. The robot class has a turnLeft method that takes no arguments. The result of calling this
method on a robot is that it rotates ¼ turn in the clockwise direction. For example, if the
robot is currently facing left, then after calling turnLeft it will be facing down. The
method does not return anything.

3. The robot class has a turnRight method that takes no arguments. The result of calling this
method on a robot is that it rotates ¼ turn in the counter clockwise direction. For
example, if the robot is currently facing left, then after a call to turnRight it will be facing
up. The method does not return anything.

4. The robot class has an advance method that takes no arguments. The result of calling this
method on a robot is that it moves one unit in the direction it is facing. For example, if
the robot is currently at (10, 20) and is facing down, then after calling advance the robot
will be at (10, 19). The method does not return anything.

5. The robot class has a getPosition method that takes no arguments. It returns a tuple
containing two integers, the x and y position of the robot.

6. The robot class has a getFacing method that takes no arguments and returns one of 4
strings: ‘U’, ‘D’, ‘L’, or ‘R’, indicating which direction the robot is facing. Note: while
the constructor accepts lower case letters, getFacing always returns upper case letters.

7. The robot class has a runProgram method that takes a string consisting of the characters
A, R, or L. These letters correspond to issuing the commands to advance (advance
method), to turn to the right (turnRight method) or to turnto the left (turnLeft method).
The result of calling the runProgram method with such a string is the same as if the
corresponding methods were called on the robot in the order they appear in the string. So
a robot given the program ‘ARAL’ should make the following sequence of moves:
advance(), turnRight(), advance(), and turnLeft()

8. The str() constructor when called on a robot should result in a string with the format ‘(x,
y):d’where x is the current x position, y is the current y position, and d is the current
direction. For example, if the robot r’s position is (-4, 3) and it is currently facing left,
then str® should return the string ‘(-4, 3):L’.


Testing should be executed by below script


from HW7 import robot

def test1():

'''Test getPosition'''

r1 = robot(0,0,'U')

r2 = robot(1,11,'r')

r3 = robot(2,22,'d')

r4 = robot(3,33,'L')

if r1.getPosition() != (0, 0):

print("failed test1A")

return False

if r2.getPosition() != (1, 11):

print("failed test1B")

return False

if r3.getPosition() != (2, 22):

print("failed test1C")

return False

if r4.getPosition() != (3, 33):

print("failed test1D")

return False

return True

def test2():

'''Test getFacing'''

r1 = robot(0,0,'U')

r2 = robot(1,11,'r')

r3 = robot(2,22,'d')

r4 = robot(3,33,'L')

if r1.getFacing() != 'U':

print("failed test2A")

return False

if r2.getFacing() != 'R':

print("failed test2B")

return False

if r3.getFacing() != 'D':

print("failed test2C")

return False

if r4.getFacing() != 'L':

print("failed test2D")

return False

return True

def test3():

'''Test move'''

r1 = robot(4,5,'U')

r2 = robot(-4,2, 'R')

r3 = robot(9,-5, 'D')

r4 = robot(-6, -2, 'L')

r1.advance()

r2.advance()

r3.advance()

r4.advance()

if r1.getPosition() != (4,6):

print("failed test3A")

return False

if r2.getPosition() != (-3,2):

print("failed test3B")

return False

if r3.getPosition() != (9,-6):

print("failed test3C")

return False

if r4.getPosition() != (-7,-2):

print("failed test3D")

return False

return True

def test4():

'''Test turnLeft'''

r1 = robot(4,5,'U')

r2 = robot(-4,2, 'r')

r3 = robot(9,-5, 'd')

r4 = robot(-6, -2, 'L')

r1.turnLeft()

r2.turnLeft()

r3.turnLeft()

r4.turnLeft()

if r1.getFacing() != 'L':

print("failed test4A")

return False

if r2.getFacing() != 'U':

print("failed test4B")

return False

if r3.getFacing() != 'R':

print("failed test4C")

return False

if r4.getFacing() != 'D':

print("failed test4D")

return False

return True

def test5():

'''Test turnRight'''

r1 = robot(4,5,'U')

r2 = robot(-4,2, 'r')

r3 = robot(9,-5, 'd')

r4 = robot(-6, -2, 'L')

r1.turnRight()

r2.turnRight()

r3.turnRight()

r4.turnRight()

if r1.getFacing() != 'R':

print("failed test5A")

return False

if r2.getFacing() != 'D':

print("failed test5B")

return False

if r3.getFacing() != 'L':

print("failed test5C")

return False

if r4.getFacing() != 'U':

print("failed test5D")

return False

return True

def test6():

'''Test multiple moves'''

r1 = robot(4,5,'U')

r2 = robot(-4,2, 'R')

r3 = robot(9,-5, 'D')

r4 = robot(-6, -2, 'L')

r1.advance()

r1.advance()

r2.advance()

r1.advance()

r2.advance()

r3.advance()

r1.advance()

r2.advance()

r3.advance()

r4.advance()

if r1.getPosition() != (4,9):

print("failed test6A")

return False

if r2.getPosition() != (-1,2):

print("failed test3B")

return False

if r3.getPosition() != (9,-7):

print("failed test6C")

return False

if r4.getPosition() != (-7,-2):

print("failed test6D")

return False

return True

def test7():

'''Test complex sequence of moves and turns'''

r1 = robot(8, 2, 'R')

r2 = robot(-1, -4, 'U')

r1.turnRight()

r2.advance()

r2.advance()

r2.turnRight()

r1.advance()

r1.advance()

r2.advance()

r1.turnLeft()

r1.advance()

r1.turnRight()

r2.turnRight()

r2.turnRight()

r2.advance()

r1.advance()

r1.turnRight()

r1.advance()

r1.advance()

r2.advance()

r2.turnRight()

if r1.getPosition() != (7,-1):

print("failed test 7A")

return False

if r1.getFacing() != 'L':

print("failed test 7B")

return False

if r2.getPosition() != (-2,-2):

print("failed test 7C")

return False

if r2.getFacing() != 'U':

print("failed test 7D")

return False

return True

def test8():

'''testing program'''

r1 = robot(8, 2, 'D')

r2 = robot(-4, -1, 'R')

r1.runProgram('AARAR')

if r1.getPosition() != (7,0):

print("failed test8A")

return False

if r1.getFacing() != 'U':

print("failed test8B")

return False

r2.runProgram('LARALA')

if r2.getPosition() != (-3, 1):

print("failed test8C")

return False

if r2.getFacing() != 'U':

print("failed test8D")

return False

r1.runProgram('AL')

if r1.getPosition() != (7,1):

print("failed test8E")

return False

if r1.getFacing() != 'L':

print("failed test8F")

return False

r2.runProgram('LR')

if r2.getPosition() != (-3, 1):

print("failed test8F")

return False

if r2.getFacing() != 'U':

print("failed test8G")

return False

return True

def test9():

'''testing __str__'''

r1 = robot(1, 4, 'U')

r2 = robot(-4, 0, 'R')

r3 = robot(-7, -18, 'L')

r4 = robot(81, -984, 'D')

if str(r1) != '(1, 4):U':

print("failed test9A")

return False

if str(r2) != '(-4, 0):R':

print("failed test9B")

return False

if str(r3) != '(-7, -18):L':

print("failed test9C")

return False

if str(r4) != '(81, -984):D':

print("failed test9D")

return False

return True

score = 0

if (test1()):

score += 10

if (test2()):

score += 10

if (test3()):

score += 10

if (test4()):

score += 10

if (test5()):

score += 10

if (test6()):

score += 10

if (test7()):

score += 10

if (test8()):

score += 10

if (test9()):

score += 10

print('Total score: {}/90'.format(score))

print("Don't forget to submit a screenshot of this execution!")


Unformatted Attachment Preview

Х Python 3.7.0 Shell File Edit Shell Debug Options Window Help >>> rl = robot (0, 0, 'U') >>> r2 = robot(-1, 3, 'd') >>> ri.getPosition() (0, 0) >>> r2.getPosition() (-1, 3) >>> ri.getFacing () 'U' >>> r2.getFacing () 'D' >>> r2.advance () >>> r2.get Position() (-1, 2) >>> r2.turnleft() >>> r2.getFacing() 'R' >>> r2.advance() >>> r2.getPosition() (0, 2) >>> ri.run Program ('AARAR') >>> r1.getPosition() (1, 2) >>> rl.getFacing () 'D' >>> str(rl) '(1, 2):D >>> print (rl) (1, 2):D >>> Ln: 43 Col: 0
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

At...


Anonymous
I use Studypool every time I need help studying, and it never disappoints.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags