-
Notifications
You must be signed in to change notification settings - Fork 0
/
clans.py
286 lines (219 loc) · 9.07 KB
/
clans.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Clan vs Clan - A Deadly Serious Game!
# Copyright 2013 Tom Hanrahan
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License
# Based on the missionaries and cannibals game. Two clans are on one side of a
# void and must cross safely to the other side. There is only one vehicle and
# it can carry only two beings. One clan is VERY hungry and if at anytime they
# out number the other clan on either side of the void, they'll eat them. The
# goal of the game is to make sure that both clans cross the the other side of
# the void in an uneaten state.
__version__ = 1.0
import time
import sys
TRUE = 1
FALSE = 0
"""
The traditional game of Missionaries and Cannibal
CALLING = ("missionary", "cannibal")
CLANS = ("missionaries", "cannibals")
CLAN_NAMES_0 = ("Peter", "Paul", "Biff")
CLAN_NAMES_1 = ("Zulu", "Kinta", "Tombalku")
LOCATION = ("Upper Bank", "Lower Bank")
LAUNCH_SITE = "bank"
VEHICLE = "boat"
VEHICLE_FIGURE = "[BOAT]> "
FIRST_BOARDER = "first_boarder"
SECOND_BOARDER = "second_boarder"
"""
"""
An alien variation of the game
"""
CALLING = ("Human", "Alien")
CLANS = ("humans", "aliens")
CLAN_NAMES_0 = ("Tom", "Larry", "Moe")
CLAN_NAMES_1 = ("Zork", "Betel", "Oggs")
LOCATION = ("Planet Zwet", "Planet Earth")
LAUNCH_SITE = "planet"
VEHICLE = "space craft"
VEHICLE_FIGURE = "[=====> "
FIRST_BOARDER = "first"
SECOND_BOARDER = "second"
class Being(object):
"""The shell of a man, er ... being"""
upper_level_clan_0 = 0
upper_level_clan_1 = 0
lower_level_clan_0 = 0
lower_level_clan_1 = 0
@staticmethod
def census():
print("There are now \n", \
Being.upper_level_clan_0, CLANS[0], "and", Being.upper_level_clan_1, CLANS[1], "on the", LOCATION[0], "\n",\
Being.lower_level_clan_0, CLANS[0], "and", Being.lower_level_clan_1, CLANS[1], "on the", LOCATION[1])
def __init__(self, calling, name, location=LOCATION[0]):
self.calling = calling
self.name = name
self.location = location
if self.calling == CALLING[0]:
Being.upper_level_clan_0 += 1
else:
Being.upper_level_clan_1 += 1
print(self.calling, self.name, "materilaizes on the", LOCATION[0])
def increment_upper_level_clan_0():
Being.upper_level_clan_0 += 1
def decrement_upper_level_clan_0():
Being.upper_level_clan_0 -= 1
def increment_lower_level_clan_0():
Being.lower_level_clan_0 += 1
def decrement_lower_level_clan_0():
Being.lower_level_clan_0 -= 1
def increment_upper_level_clan_1():
Being.upper_level_clan_1 += 1
def decrement_upper_level_clan_1():
Being.upper_level_clan_1 -= 1
def increment_lower_level_clan_1():
Being.lower_level_clan_1 += 1
def decrement_lower_level_clan_1():
Being.lower_level_clan_1 -= 1
def print_population_map(clan_0_list, clan_1_list):
"""
Show the location of all the players
"""
print("\n")
print(LOCATION[0], ":", end=" ")
for i in range(3):
if clan_0[i].location == LOCATION[0]:
print(clan_0[i].name, end=" ")
if clan_1[i].location == LOCATION[0]:
print(clan_1[i].name, end=" ")
print("\n-----------------------------------------------------")
print(LOCATION[1], ":", end=" ")
for i in range(3):
if clan_0[i].location == LOCATION[1]:
print(clan_0[i].name, end=" ")
if clan_1[i].location == LOCATION[1]:
print(clan_1[i].name, end=" ")
print("\n")
Being.census()
print("======================================================\n")
def get_passenger(boarding_order):
"""
Get a passenger (or no one). The passenger must exist in the game and be on the same
side of the void as the vehicle
"""
need_passenger = TRUE
while(need_passenger):
need_passenger = FALSE
name = None
# Make sure that only a current named being is selected
while ((name not in CLAN_NAMES_0) and (name not in CLAN_NAMES_1) and name != ""):
if boarding_order == FIRST_BOARDER:
name = input("Who should go on board first? ")
elif boarding_order == SECOND_BOARDER:
name = input("Who should go on board second (if no one press enter)? ")
else:
print("Something is terribly wrong in get_passenger\n")
name = name.title()
if boarding_order == FIRST_BOARDER and name == "":
name = None
# Make sure the selected being is on the same side of the void as the vehicle
if name != "":
i = 0
while ((name != clan_0[i].name) and (name != clan_1[i].name) and i < 3):
i = i + 1
if name == clan_0[i].name:
boarder_location = clan_0[i].location
else:
boarder_location = clan_1[i].location
if ((direction == "down" and boarder_location == LOCATION[1]) or \
(direction == "up") and (boarder_location == LOCATION[0])):
print(name, "is on the opposite", LAUNCH_SITE, ". \nPick someone on the", \
LAUNCH_SITE, "with the", VEHICLE ,".\n")
name = None
need_passenger = TRUE
if name != None:
return name
def cross_the_void(direction, name_1, name_2):
"""
Get your passengers across the void
"""
print(VEHICLE_FIGURE, end=" ")
for i in range(3):
if name_1 == clan_0[i].name or name_2 == clan_0[i].name:
print(clan_0[i].name, end=" ")
if direction == "down":
clan_0[i].location = LOCATION[1]
Being.decrement_upper_level_clan_0()
Being.increment_lower_level_clan_0()
else:
clan_0[i].location = LOCATION[0]
Being.increment_upper_level_clan_0()
Being.decrement_lower_level_clan_0()
for i in range(3):
if name_1 == clan_1[i].name or name_2 == clan_1[i].name:
print(clan_1[i].name, end=" ")
if direction == "down":
clan_1[i].location = LOCATION[1]
Being.decrement_upper_level_clan_1()
Being.increment_lower_level_clan_1()
else:
clan_1[i].location = LOCATION[0]
Being.increment_upper_level_clan_1()
Being.decrement_lower_level_clan_1()
for j in range(10):
sys.stdout.flush()
time.sleep(0.2)
print(end="-")
print("\n-----------------------------------------------------")
# Main
# Instructions
print("\n\nYOUR ASSIGNMENT")
print("Move every one from", LOCATION[1], "to", LOCATION[0])
print("Use your space craft, which can hold no more than two beings")
print("But remember that", CLANS[1], "like to eat", CLANS[0])
print("Don't let the", CLANS[1], "outnumber the", CLANS[0])
print("on any planet at any time.\n")
# Create the players
clan_0 = [None, None, None]
clan_1 = [None, None, None]
for i in range(3):
clan_0[i] = Being(CALLING[0], CLAN_NAMES_0[i])
for i in range(3):
clan_1[i] = Being(CALLING[1], CLAN_NAMES_1[i])
print_population_map(clan_0, clan_1)
# Play the game
direction = None
still_alive = TRUE
while (Being.upper_level_clan_0 + Being.upper_level_clan_1) > 0 and still_alive:
# Position the vehicle for the next move
if direction == "down":
direction = "up"
print("\nThe", VEHICLE, "is on the", LOCATION[1], ".")
else:
direction = "down"
print("\nThe", VEHICLE, "is on the", LOCATION[0], ".")
# Load the vehicle and cross the void
name_1 = get_passenger(FIRST_BOARDER)
name_2 = get_passenger(SECOND_BOARDER)
cross_the_void(direction, name_1, name_2)
# Sort out where everyone is and whether anyone has been eaten yet
print_population_map(clan_0, clan_1)
if ((Being.upper_level_clan_0 != 0) and Being.upper_level_clan_0 < Being.upper_level_clan_1) or \
((Being.lower_level_clan_0 != 0) and (Being.lower_level_clan_0 < Being.lower_level_clan_1)):
still_alive = FALSE
# Game over
if still_alive:
print("Congratulations! You've completed your assignment.\n")
else:
print("Oh My God! The", CLANS[1], "are eating the", CLANS[0], "! You've lost!\n")