Python Forum
Please help with Create Custom class Robot():
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with Create Custom class Robot():
#1
Hello,
The below is the homework assignment we have been assigned and due at midnight. If anyone is able to assist with a solution that would be greatly and most certainly appreciated!

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’.

[Image: D8wtnFd.png]

What the test must pass
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!")
Reply
#2
Please use python tags when posting code. I put them in for you this time. See the BBCode link in my signature below for instructions. Also please do not use screen shots.

Also, please show us what you have tried, and explain exactly how it is not working. Contrary to popular belief, we are not paid to write code for you on short notice. As it turns out, we are not paid at all. But we are still happy to help you fix code that isn't working.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yes of course much apologizes!

So far i have tried creating a base skeleton file. Its a start that i am failing to get a good start on.

class robot():

    input(int(x), in(y), str(U)):

    def turnLeft(self):

    def turnRight(self):

    def advance(self):

    def getPosition(self):

        return (x, y)

    def getFacing(self):

        return str(U)

    def runProgram(self, str):


    def constructor(self):
    
    def __init__(self, initialValue=(x,y)):
        self.starting = initialValue
        pass


    pass
    
Reply
#4
The input statement on line 3 is a best in the wrong place. Get rid of it for now.

Your __init__ method is your constructor. Note that it does not meet the first requirement in the homework assignment.

Next you should look at turnLeft and turnRight. Given your current direction (having been set in your fixed __init__ method), how do you change it to effectively turn in those directions.

Then you can work on advance. How do you change x and y given the direction you are facing?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
So i changed it to the below so far.

How would i call my current direction? and then change it?

class robot():

    def turnLeft(self):
        

    def turnRight(self):

    def advance(self):

    def getPosition(self):

        return (x, y)

    def getFacing(self):

        return str(U)

    def runProgram(self, str):


    def constructor(self):

    def __init__(self, x, y, startD):
        self.starting = (x, y)
        self.position = startD
        pass


    pass
    
Reply
#6
Your current direction is in self.position (maybe it should be called self.direction?). So figure out what self.position/self.direction is, figure out what it would be if you turned left, and set it to that. Do the same for turnRight.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using the robot class - implement robot battles - Python OOP krnkrnkrn 1 1,824 May-17-2021, 09:19 PM
Last Post: deanhystad
  How can create class Person :( Azilkhan 1 1,923 Nov-21-2019, 08:12 AM
Last Post: DeaD_EyE
  How to create custom error bars in matplotlib.pyplot? wlsa 1 4,216 Nov-04-2018, 09:49 PM
Last Post: wlsa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020