-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_3.py
109 lines (74 loc) · 3.12 KB
/
assignment_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Name: Alfredo Pena
Class: CS 241
Prof.: Bro. Macbeth
This program models driving a robot around in an environment.
"""
##############################################################
class CrappyRobot():
def __init__(self): ###Initializes the crappy robot.
self.xCoordinate = 10
self.yCoordinate = 10
self.fuel_amount = 100
def useFuel(self): ###Lowers the robot's amount of fuel left.
self.fuel_amount -= 5
def moveLeft(self): ###Moves the robot a unit to the left.
if self.fuel_amount < 5:
print("Insufficient fuel to perform action")
else:
self.xCoordinate -= 1
self.useFuel()
def moveRight(self): ####Moves the robot a unit to the right.
if self.fuel_amount < 5:
print("Insufficient fuel to perform action")
else:
self.xCoordinate += 1
self.useFuel()
def moveUp(self): ###Moves the robot a unit up.
if self.fuel_amount < 5:
print("Insufficient fuel to perform action")
else:
self.yCoordinate -= 1
self.useFuel()
def moveDown(self): ###Moves the robot a unit down.
if self.fuel_amount < 5:
print("Insufficient fuel to perform action")
else:
self.yCoordinate += 1
self.useFuel()
def shoot(self): ###Shoots two bullets with the robot's shotgun.
if self.fuel_amount < 15:
print("Insufficient fuel to perform action")
else:
for times in range(3):
self.useFuel()
print("Bam! Bam!")
def displayStatus(self): ###Displays the robot's status.
print("({}, {}) - Fuel: {}".format(self.xCoordinate, self.yCoordinate, self.fuel_amount))
################################################################
def askforCommand(): ###Prompts the user for a command.
command = input("Enter command: ")
return command
def main(): ###While there's fuel, allows the user to control the robot.
robot = CrappyRobot()
command = None
while command != 'quit':
command = askforCommand()
if command == 'left':
robot.moveLeft()
elif command == 'right':
robot.moveRight()
elif command == 'up':
robot.moveUp()
elif command == 'down':
robot.moveDown()
elif command == 'fire':
robot.shoot()
elif command == 'status':
robot.displayStatus()
else:
pass
print('Goodbye.')
##############################################################
if __name__ == "__main__":
main()