-
Notifications
You must be signed in to change notification settings - Fork 0
/
kakegurui.py
executable file
·252 lines (225 loc) · 11.2 KB
/
kakegurui.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
import string,time
import random,sys,pygame
import os
import termcolor
dialog_path = os.path.join("assets","yumeko.ogg")
class Kakegurui:
def __init__(self,player):
self.player_name = player
self.chips1 = 20
self.chips2 = 20
self.total_chips_won = 0
self.player1_card,self.computer_card = [],[]
def display_rules(self):
termcolor.cprint('''
Try your luck
Idea taken from: KAKEGURUI Season 1 episode 4
INDIAN POKER (deck of 40 cards with joker and face card removed)
game rules, read this before playing
[here player 2 is computer]
rule 0: Player will play the game with computer; the game is 40 deck card Indian poker
rule 1: 20 chips will be provided initially, game starts with 1 chip as fee
rule 1.1: 2 cards each will be provided to both player and computer
rule 2: minimum of 1 and maximum of 5 chips can be bet at a time
rule 3: 0 to show the cards
rule 4: game will continue till the players give up(i.e., show=0) or chip is 0 and player lose the bet
rule 5: type1:PAIR denotes same number
rule 6: type2:SUIT denotes same house
rule 7: type3:PIG denotes different house and different number
rule 8: if both the player and computer have same type, higher sum is the winner
Note: type1>type2>type3
\n ''',"green")
# heart(H), spade(S), club(C), diamond(D)
def deck_cards(self):
num=list(range(2,11)) #it is a 40 card deck game with face card removed
suits = ['S','H','C','D']
deck = []
for ace_card in suits: #this for loop appends ace cards in the deck
deck.append(ace_card+'A')
for suits_card in num:
for s in suits:
card = s+str(suits_card)
deck.append(card)
random.shuffle(deck) #shuffles the 40 deck
return deck
def get_cards(self,deck40):
#lets consider A as 11 while addition for higher cards
#self.player1_card - player 1 random cards
#computer_card - computer random cards
for i in range(2):
self.player1_card.append(random.choice(deck40))
self.computer_card.append(random.choice(deck40))
def display1card_toPlayer(self): #show 1 card out of 2 to the player
print("Displaying 1 card out of 2 to the Player")
print("\n\n Your Card:{} \n\n".format(random.choice(self.player1_card)))
def bet(self):
print("Current Available Chips of {}:{}.".format(self.player_name,self.chips1))
print("Current Available Chips of Computer:{}.\n".format(self.chips2))
print('Collecting match fee of 1 chip each....')
self.chips1 -= 1
self.chips2 -= 1
time.sleep(1)
while True:
try:
player1_bet = int(input("{}; Enter your bet in range(1-5) or 0 to show: ".format(self.player_name)))
if player1_bet==0:
confirm = input("Are you filling to show?(Y/n)").lower()
if confirm.startswith("y"):
termcolor.cprint("{} decides to show".format(self.player_name),"red")
self.winner()
break
else:
continue
elif player1_bet in range(1,6):
comp_bet = random.randint(0,5)
if self.chips1>1 and self.chips2>1 and comp_bet<self.chips2 and self.chips1>player1_bet:
self.chips1 = self.chips1-player1_bet
self.total_chips_won+=player1_bet
if comp_bet==0:
termcolor.cprint("Computer Decided to Show","red")
self.winner()
break
print("Computer Placed His Bet:{}".format(comp_bet))
self.chips2 = self.chips2-comp_bet
self.total_chips_won += comp_bet
else:
print("\nInsuffient Chip, forced to show")
self.winner()
break
else:
print("Invalid Bet Range(1-5) or 0 to show")
continue
termcolor.cprint("Your Available Chips:{}".format(self.chips1),"blue")
termcolor.cprint("Computer Available Chips:{}".format(self.chips2),"blue")
except ValueError:
print("Illegal entry")
def cards_showdown(self):
print("Player 1 cards:{}".format(self.player1_card))
print("Computer cards:{}".format(self.computer_card))
def player_wins(self,how):
self.chips1+=self.total_chips_won
termcolor.cprint("\n{} won, {}".format(self.player_name.capitalize(),how),"red")
termcolor.cprint("Total Chips Won By {} is: {}.\n".format(self.player_name,self.total_chips_won),"blue")
def computer_wins(self,how):
self.chips2+=self.total_chips_won
termcolor.cprint("\nComputer won, {}".format(how),"red")
termcolor.cprint("Total Chips Won By Computer is: {}.\n".format(self.total_chips_won),"blue")
def conclude(self):
won_by = self.player_name if self.chips1>self.chips2 else "Computer"
termcolor.cprint(f"{won_by.capitalize()} Won","green")
if self.play_again():
main()
def winner(self):
#addition of higher card is important when both player gets same card or pig
# if both player has pair, higher card wins
sum_for_same_pair_p1=sum_for_same_pair_p2=0 #sum of both player p1 and p2
#if both player has suit, same house
sum_for_same_suit_p1=sum_for_same_suit_p2=0
#if both has a pig
sum_for_pig_p1=sum_for_pig_p2=0
#checking for the pair
if self.player1_card[0][1:] == self.player1_card[1][1:] and self.computer_card[0][1:] != self.computer_card[1][1:]:
self.player_wins(how="Pair Of Number")
self.cards_showdown()
elif self.player1_card[0][1:] != self.player1_card[1][1:] and self.computer_card[0][1:] == self.computer_card[1][1:]:
self.computer_wins(how="Pair Of Number")
self.cards_showdown()
elif self.player1_card[0][1:] == self.player1_card[1][1:] and self.computer_card[0][1:] == self.computer_card[1][1:]:
for player1 in range(len(self.player1_card)):
if 'A' in self.player1_card[player1][1:]:
self.player1_card[player1] = self.player1_card[player1].replace(self.player1_card[player1][1:], '11')
sum_for_same_pair_p1 = sum_for_same_pair_p1 + int(self.player1_card[player1][1:])
for player2 in range(len(self.computer_card)):
if 'A' in self.computer_card[player2][1:]:
self.computer_card[player2] = self.computer_card[player2].replace(self.computer_card[player2][1:], '11')
sum_for_same_pair_p2 = sum_for_same_pair_p2 + int(self.computer_card[player2][1:])
if sum_for_same_pair_p1 > sum_for_same_pair_p2:
self.player_wins(how="by a Higher Pair card")
self.cards_showdown()
else:
self.computer_wins(how="by a Higher Pair card")
self.cards_showdown()
elif self.player1_card[0][:1] == self.player1_card[1][:1] and self.computer_card[0][:1] != self.computer_card[1][:1]:
self.player_wins(how="Higher Suits")
self.cards_showdown()
elif self.player1_card[0][:1] != self.player1_card[1][:1] and self.computer_card[0][:1] == self.computer_card[1][:1]:
self.computer_wins(how="Higher Suits")
self.cards_showdown()
elif self.player1_card[0][:1] == self.player1_card[1][:1] and self.computer_card[0][:1] == self.computer_card[1][:1]:
for player1 in range(len(self.player1_card)):
if 'A' in self.player1_card[player1][1:]:
self.player1_card[player1] = self.player1_card[player1].replace(self.player1_card[player1][1:], '11')
sum_for_same_suit_p1 = sum_for_same_suit_p1 + int(self.player1_card[player1][1:])
for player2 in range(len(self.computer_card)):
if 'A' in self.computer_card[player2][1:]:
self.computer_card[player2] = self.computer_card[player2].replace(self.computer_card[player2][1:], '11')
sum_for_same_suit_p2 = sum_for_same_suit_p2 + int(self.computer_card[player2][1:])
if sum_for_same_suit_p1 > sum_for_same_suit_p2:
self.player_wins(how="by a Higher Suit Card")
self.cards_showdown()
else:
self.computer_wins(how="by a Higher Suit Card")
self.cards_showdown()
else:
for player1 in range(len(self.player1_card)):
if 'A' in self.player1_card[player1][1:]:
self.player1_card[player1] = self.player1_card[player1].replace(self.player1_card[player1][1:], '11')
sum_for_pig_p1 = sum_for_pig_p1 + int(self.player1_card[player1][1:])
for player2 in range(len(self.computer_card)):
if 'A' in self.computer_card[player2][1:]:
self.computer_card[player2] = self.computer_card[player2].replace(self.computer_card[player2][1:], '11')
sum_for_pig_p2 = sum_for_pig_p2 + int(self.computer_card[player2][1:])
if sum_for_pig_p1 > sum_for_pig_p2:
self.player_wins(how="by a Higher Pig Card")
self.cards_showdown()
else:
self.computer_wins(how="by a Higher Pig Card")
self.cards_showdown()
self.player1_card.clear()
self.computer_card.clear()
self.total_chips_won = 0
def play_again(self):
play = input('Do you wanna play again?(Y/n)').lower()
if play.startswith('y'):
round = 1
self.chips1 = 100
self.chips2 = 100
self.total_chips_won = 0
self.player1_card,self.computer_card = [],[]
return True
else:
print("Thank You For Playing")
sys.exit()
def main():
player=input('Enter player name:')
kakeguruiGame = Kakegurui(player)
pygame.init()
game_rules=input('Do you want to see the game rules(y/n):').lower()
if 'y' in game_rules:
kakeguruiGame.display_rules()
time.sleep(5)
pygame.mixer.music.load(dialog_path)
pygame.mixer.music.play(loops=1)
deck40=kakeguruiGame.deck_cards()
round=1
termcolor.cprint('Lets get the Kakegurui freak on',"red")
while True:
#choose cards
kakeguruiGame.get_cards(deck40)
time.sleep(1)
kakeguruiGame.display1card_toPlayer()
kakeguruiGame.bet()
round +=1
if round > 10:
print("Rounds Over, Genrating Results.....")
kakeguruiGame.conclude()
break
if kakeguruiGame.chips1<2 or kakeguruiGame.chips2<2:
print("Game ends due to insufficient fund")
kakeguruiGame.conclude()
break
print("\n\n\tRound {} begins \n\n".format(round))
time.sleep(1.5)
continue
if __name__ == '__main__':
main()