-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLogic.py
245 lines (196 loc) · 9.33 KB
/
GameLogic.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class GameLogic(object):
def __init__(self):
# Initialize various variables to be used across the class
self.prompt_choices = {
"m": "missionary",
"c": "cannibal",
"n": "no",
"q": "quit"
}
self.response = ""
self.response2 = ""
def display_locations(self, east_bank, west_bank, canoe):
""" Handles the logic for displaying the locations """
output = """
West Bank Contains East Bank contains
Missionaries: %s Missionaries: %s
Cannibals: %s Cannibals: %s
""" % (west_bank['missionaries'], east_bank['missionaries'], west_bank['cannibals'], east_bank['cannibals'])
print(output)
if canoe['location'] == "west":
print(" Canoe is on the west bank")
else:
print(" Canoe is on the east bank")
print('\n')
return
def update_locations(self, user_choice, east_bank, west_bank, canoe):
""" Handles the logic for updating each location """
# DEBUG STATEMENTS
#print("Variables before manipulation$$$$$$")
#print(user_choice)
#print("EAST", east_bank)
#print("WEST", west_bank)
#print(canoe)
# Loop through the users choices and update each dictionary accordingly.
for i in user_choice:
if i is not None and i is not "":
if canoe['location'] == "east":
if i == "m":
east_bank['missionaries'] = east_bank['missionaries'] - 1
west_bank['missionaries'] = west_bank['missionaries'] + 1
else:
east_bank['cannibals'] = east_bank['cannibals'] - 1
west_bank['cannibals'] = west_bank['cannibals'] + 1
# DEBUG STATEMENTS
#print("Variables east manipulation$$$$$$")
#print(user_choice)
#print("EAST", east_bank)
#print("WEST", west_bank)
#print(canoe)
elif canoe['location'] == "west":
if i != "n":
if i == "m":
east_bank['missionaries'] = east_bank['missionaries'] + 1
west_bank['missionaries'] = west_bank['missionaries'] - 1
else:
east_bank['cannibals'] = east_bank['cannibals'] + 1
west_bank['cannibals'] = west_bank['cannibals'] - 1
# DEBUG STATEMENTS
#print("Variables west manipulation$$$$$$")
#print(user_choice)
#print("EAST", east_bank)
#print("WEST", west_bank)
#print(canoe)
else:
print("Nothing")
# Update which direction the canoe is heading
if canoe['location'] == "east":
canoe['location'] = "west"
else:
canoe['location'] = "east"
return (east_bank, west_bank, canoe)
def check_for_win(self, east_bank, west_bank):
""" Handles the logic needed for checking to see if there has been a win """
# DEBUG STATEMENTS
#print("West bank MSN = {}".format(west_bank['missionaries']))
#print("West bank CNB = {}".format(west_bank['cannibals']))
#print("East bank MSN = {}".format(west_bank['missionaries']))
#print("East bank CNB = {}".format(west_bank['cannibals']))
if west_bank['missionaries'] == 3 and west_bank['cannibals'] == 3:
return 'Winner'
elif west_bank['cannibals'] > west_bank['missionaries'] and west_bank['missionaries'] > 0:
return 'Loser'
elif east_bank['cannibals'] > east_bank['missionaries'] and east_bank['missionaries'] > 0:
return 'Loser'
else:
return 'continue to play'
def user_prompt(self, east_bank, west_bank, canoe):
""" Handles the logic needed for asking a user for information """
# Set our variables real quick
global response
global response2
response = ""
response2 = ""
# Ask for user input for who to send to the west bank
if canoe['location'] == "east":
response = input("Who would you like to send to the west bank? Type 'm' for missionary or 'c' for cannibal: ")
response = str(response)
if response == "q":
return (response)
response2 = input("Who would you like to send next to the west bank? Type 'm' for missionary or 'c' for cannibal or 'q' to quit: ")
response2 = str(response2)
if response2 == "q":
return (response2)
# Ask for user input for who to send to the east bank
if canoe['location'] == "west":
response = input("Who would you like to take the boat back to the east bank? Type 'm' for missionary or 'c' for cannibal: ")
response = str(response)
if response == "q":
return (response)
response2 = input("Who would you like to send another person back to the east bank? Type 'm' for missionary, 'c' for cannibal, 'n' for no, or 'q' to quit: ")
response2 = str(response2)
if response2 == "q":
return (response2)
# Here we are performing error checking to ensure a selection that was chosen is a correct choice
try:
value = self.prompt_choices[response]
value2 = self.prompt_choices[response2]
except Exception as error:
print("That is not a valid choice. Please try again.")
self.user_prompt(east_bank, west_bank, canoe)
# If user made a valid choice, we make sure the move they want to perform is valid and allowed
if value != "no":
if canoe['location'] == "east" and value2 == "no":
print("Your selection is invalid - move will not work.")
self.user_prompt(east_bank, west_bank, canoe)
else:
# Checks move to ensure it can be done and doesn't violate anything
validate_move = self.ValidateMove(response, east_bank, west_bank, canoe, response2)
if validate_move is True:
if value2 == "no":
print("\nSending a {}... good luck!".format(value))
else:
print("\nSending a {} and a {}... good luck!".format(value, value2))
return (response, response2)
else:
print("Your selection is invalid - move will not work.")
self.user_prompt(east_bank, west_bank, canoe)
elif value == "no":
print("Your selection is invalid - move will not work.")
self.user_prompt(east_bank, west_bank, canoe)
else:
response, response2 = "q", "q"
return (response, response2)
def _ValidateMove(self, check_dict, user_choice, user_choice2):
""" Handles repetitive use of logic for validating the move """
# If the user chooses both missionaries to send...
if user_choice == 'm' and user_choice2 == "m":
if check_dict['missionaries'] > 1:
return True
else:
return False
# If the user chooses both cannibals to send...
if user_choice == 'c' and user_choice2 == "c":
if check_dict['cannibals'] > 1:
return True
else:
return False
# If the user chooses one missionary and one cannibal...
if user_choice == 'm' and user_choice2 == "c":
if check_dict['cannibals'] > 0 and check_dict['missionaries'] > 0:
return True
else:
return False
# Same as above, just with reversed logic choice
if user_choice == 'c' and user_choice2 == "m":
if check_dict['cannibals'] > 0 and check_dict['missionaries'] > 0:
return True
else:
return False
# If the user chooses one missionary or one cannibal and no...
if user_choice == 'm' and user_choice2 == "n":
if check_dict['missionaries'] > 0:
return True
else:
return False
# Same as above, just with reversed logic choice
if user_choice == 'c' and user_choice2 == "n":
if check_dict['cannibals'] > 0:
return True
else:
return False
return False
def ValidateMove(self, user_choice, east_bank, west_bank, canoe, user_choice2=None):
""" Handles the logic for validating a move by calling upon it's helper method """
if canoe['location'] == 'west':
value = self._ValidateMove(west_bank, user_choice, user_choice2)
if value == True:
return True
else:
return False
if canoe['location'] == "east":
value = self._ValidateMove(east_bank, user_choice, user_choice2)
if value == True:
return True
else:
return False