repl.it
@bdmessenger/

ab_game

Python

No description

fork
loading
Files
  • main.py
main.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import random
import time

round_num = 1

actions = ["Ally","Betray"]

def sleep(num):
  time.sleep(num)

names = [
'Allison',
'Arthur',
'Ana',
'Alex',
'Arlene',
'Alberto',
'Barry',
'Bertha',
'Bill',
'Bonnie',
'Bret',
'Beryl',
'Chantal',
'Cristobal',
'Claudette',
'Charley',
'Cindy',
'Chris',
'Dean',
'Dolly',
'Danny',
'Danielle',
'Dennis',
'Debby',
'Erin',
'Edouard',
'Erika',
'Earl',
'Emily',
'Ernesto',
'Felix',
'Fay',
'Fabian',
'Frances',
'Franklin',
'Florence',
'Gabielle',
'Gustav',
'Grace',
'Gaston',
'Gert',
'Gordon',
'Humberto',
'Hanna',
'Henri',
'Hermine',
'Harvey',
'Helene',
'Iris',
'Isidore',
'Isabel',
'Ivan',
'Irene',
'Isaac',
'Jerry',
'Josephine',
'Juan',
'Jeanne',
'Jose',
'Joyce',
'Karen',
'Kyle',
'Kate',
'Karl',
'Katrina',
'Kirk',
'Lorenzo',
'Lili',
'Larry',
'Lisa',
'Lee',
'Leslie',
'Michelle',
'Marco',
'Mindy',
'Maria',
'Michael',
'Noel',
'Nana',
'Nicholas',
'Nicole',
'Nate',
'Nadine',
'Olga',
'Omar',
'Odette',
'Otto',
'Ophelia',
'Oscar',
'Pablo',
'Paloma',
'Peter',
'Paula',
'Philippe',
'Patty',
'Rebekah',
'Rene',
'Rose',
'Richard',
'Rita',
'Rafael',
'Sebastien',
'Sally',
'Sam',
'Shary',
'Stan',
'Sandy',
'Tanya',
'Teddy',
'Teresa',
'Tomas',
'Tammy',
'Tony',
'Van',
'Vicky',
'Victor',
'Virginie',
'Vince',
'Valerie',
'Wendy',
'Wilfred',
'Wanda',
'Walter',
'Wilma',
'William',
'Kumiko',
'Aki',
'Miharu',
'Chiaki',
'Michiyo',
'Itoe',
'Nanaho',
'Reina',
'Emi',
'Yumi',
'Ayumi',
'Kaori',
'Sayuri',
'Rie',
'Miyuki',
'Hitomi',
'Naoko',
'Miwa',
'Etsuko',
'Akane',
'Kazuko',
'Miyako',
'Youko',
'Sachiko',
'Mieko',
'Toshie',
'Junko']


players = []

print("              ----- Prisoner's Dilemma ----- ")
print("Goal: Get 9 points to escape by either picking ally or betray each round against a random oppenent. However, make sure not to lose points constantly enough to reach zero or negative, otherwise you will be disqualified to continue the game. If you and your opponent picked ally, both players will gain 2 points. But if you choose betray and the opponent choose ally, you would gain 3 points while your opponent lose 2 points, and vice versa. And last, if both players choose betray, no one gain or lose any points. All players will start with 3 points. \n\nWhat will you do, Ally or Betray? I wish the best of luck to you...\n")
print("What is your name?")
name = input()
players.append([name,3])

prev = None
num = None
temp_array = []
for i in range(5):
  while(prev == num):
    num = random.randint(0,151)
    if prev != num:
      while(names[num] in temp_array):
        num = random.randint(0,151)
      temp_array.append(names[num])
  prev = num

for i in temp_array:
  players.append([i,3])

def randomize_opponents():
  temp = [player for player in players]
  teams = []
  while(len(temp) != 0):
    randInt = random.randint(0,len(temp)-1)
    while(not temp[randInt] in temp):
      randInt = random.randint(0,len(temp)-1)
    first_person = temp[randInt]
    temp.remove(first_person)
    randInt = random.randint(0,len(temp)-1)
    while(not temp[randInt] in temp):
      randInt = random.randint(0,len(temp)-1)
    second_person = temp[randInt]
    temp.remove(second_person)
    teams.append([first_person,second_person])
  return teams




def ally(player):
  return [player[0],0]

def betray(player):
  return [player[0],1]


def outcome(player1,player2):
  for player in players:
    if player1[1] == 0 and player2[1] == 1:
        if player[0] == player1[0]:
          player[1] -= 2
        elif player[0] == player2[0]:
          player[1] += 3
    elif player1[1] == 1 and player2[1] == 0:
        if player[0] == player1[0]:
          player[1] += 3
        elif player[0] == player2[0]:
          player[1] -= 2
    elif player1[1] == 0 and player2[1] == 0:
        if player[0] == player1[0] or player[0] == player2[0]:
          if player[1] >= 1:
            player[1] += 2

  array = [player1[1],player2[1]]
  array2 = []
  for num in array:
    string = ""
    if num == 0:
      string = "Ally"
      array2.append(string)
    else:
      string = "Betray"
      array2.append(string)
  return [[player1[0],array2[0]],[player2[0],array2[1]]]

def randomize_choice():
  randInt = random.randint(0,1)
  return randInt

def second_chance(choice):
  if choice != 1:
    return random.randint(0,1)
  return choice

def display_choices():
  print("\n[Ally] or [Betray]")
  print("\nType 0 for Ally or 1 for Betray, choose wisely:")
  value = input()
  number = value
  while(int(number) >= 2):
    value = input()
    number = value
  return number



print("")
for player in players:
  print(player)

def currentRound(teams):
  current_opponent = ""
  print("\nAB Round " + str(round_num) + " begins...")
  for team in teams:
    print(str(team[0]) + " vs " + str(team[1]))
    if team[0][0] == name:
      current_opponent = team[1][0]
    elif team[1][0] == name:
      current_opponent = team[0][0]
  print("\nYour current opponent: " + current_opponent)

def abGame(teams):
  results = []
  for team in teams:
    array = []
    zero_player = None
    ninth_man = None
    for player in team:
      if zero_player == None and player[1] < 1:
        zero_player = player
      if ninth_man == None and player[1] > 5:
        ninth_man = player
      if player[0] == name:
        if player[1] >= 1:
          value = display_choices()
          _input_ = int(value)
        else:
          _input_ = 0
        if _input_ == 0:
          array.append(ally(player))
        elif _input_ == 1:
          array.append(betray(player))
      else:
        value = randomize_choice()
        if player[1] == 2 or player[1] == 1:
          value = second_chance(value)
        elif player[1] < 1:
          value = 0

        if ninth_man != None and ninth_man != player:
          value = second_chance(value)

        if zero_player != None and zero_player != player:
          value = 1

        if value == 0:
          array.append(ally(player))
        elif value == 1:
          array.append(betray(player))
    results.append(outcome(array[0],array[1]))
  return results

def display_results(results):
  print("")
  for team in results:
    name1 = team[0][0]
    name2 = team[1][0]
    choice1 = team[0][1]
    choice2 = team[1][1]
    

    print(name1 + ": " + choice1)
    sleep(2)
    print(name2 + ": " + choice2)
    sleep(1)
    print("")

def start_round():
  teams = randomize_opponents()
  currentRound(teams)
  sleep(2)
  display_results(abGame(teams))

def check_for_winner():
  winners = []
  for player in players:
    if player[1] >= 9:
      winners.append(player)
  if len(winners) != 0:
    return winners


sleep(2)
start_round()
round_num += 1
for player in players:
  print(player)
print("")

while(check_for_winner() == None):
  print("Beginning next round in a minute...")
  sleep(10)
  start_round()
  round_num += 1
  for player in players:
    print(player)
  print("")

winners = check_for_winner()

if len(winners) == 1:
  winner = winners[0]
  if str(winner[0]) == name:
    print("You (" + str(winner[0]) + ") have escaped prisonment with "+str(winner[1])+" points. Congratulation. The rest of the prisoners are trapped in prison for eternity. Thanks for playing.")
  else:
    print(str(winner[0]) + " has exited the prison with " + str(winner[1]) + " points. You and the rest of the prisoners are trapped in prison forever. Thanks for playing.")
elif len(winners) == len(players):
  print("Everyone, congratulations, you all have escaped prisonment. Thanks to each one's cooperation, no one will be left behind. Thanks for playing.")
else:
  winners_strings = ""
  for i in range(len(winners)):
    if i != (len(winners)-1):
      winners_strings = winners_strings + str(winners[i][0]) +", "
    else:
      winners_strings = winners_strings +"and "+ str(winners[i][0])
  print(winners_strings +", congratulations, you escaped prison. Everyone else will remained eternity in prisonment. Thanks for playing.")