11import os
22import random
33from functools import namedtuple
4-
54'''
65Target: BlackJack 21 simulate
76 - Role
1413 - Player: 1
1514 - Bet: (Drop chip before gambling start)
1615 - Hit: (Take other card from the dealer)
17- - Stand: (Take no more card dealer may take card when rank under 17)
16+ - Stand: (No more card dealer may take card when rank under 17)
1817 - Double down: (When you got over 10 in first hand)
1918 (Get one card)
2019 - Surrender: (only available as first decision of a hand)
2120 - Dealer return 50% chips
2221'''
2322
2423__author__ = 'Alopex Cheung'
24+ __version__ = '0.2'
2525
2626BLACK_JACK = 21
2727BASE_VALUE = 17
@@ -87,10 +87,6 @@ def built(self):
8787 card = Card (suit , rank )
8888 self .cards .append (card )
8989
90- def testing_built (self , cards ):
91- self .cards .clear ()
92- self .cards .extend (cards )
93-
9490 def shuffle (self ):
9591 for _ in range (self .num ):
9692 for index in range (len (self .cards )):
@@ -118,14 +114,13 @@ def __init__(self, amount):
118114 self .is_double = False
119115
120116 def __bool__ (self ):
121- if self ._amount > 0 :
122- return True
123- return self ._amount > 0
117+ return self .amount > 0
124118
125119 @staticmethod
126120 def get_tips (content ):
127121 fmt_tips = '{color}** TIPS: {content}! **{end}'
128- return fmt_tips .format (color = COLOR .get ('YELLOW' ), content = content ,
122+ return fmt_tips .format (color = COLOR .get ('YELLOW' ),
123+ content = content ,
129124 end = COLOR .get ('END' ))
130125
131126 @property
@@ -192,8 +187,6 @@ def reset_chip(self):
192187 self .is_insurance = False
193188
194189 def can_double (self ):
195- if self .current_amount () - self .bet_amount >= 0 :
196- return True
197190 return self .current_amount () - self .bet_amount >= 0
198191
199192
@@ -254,14 +247,14 @@ def _sum_up(ranks):
254247
255248 def is_point (self , opt , point ):
256249 self .calculate_point ()
257- compare_fmt = '{user_point} {opt} {point}' .format (user_point = self . point ,
258- opt = opt , point = point )
250+ compare_fmt = '{user_point} {opt} {point}' .format (
251+ user_point = self . point , opt = opt , point = point )
259252 return eval (compare_fmt )
260253
261254 def speak (self , content = '' , end_char = '\n ' ):
262255 print ('' )
263- print (COLOR .get (self .color ) + self .prompt +
264- COLOR . get ( 'END' ) + content , end = end_char )
256+ print (COLOR .get (self .color ) + self .prompt + COLOR . get ( 'END' ) + content ,
257+ end = end_char )
265258
266259 def showing (self ):
267260 self .speak ()
@@ -280,9 +273,9 @@ def __init__(self, name):
280273 self .trigger = 0
281274
282275 def ask_insurance (self ):
283- buy_insurance = '( Insurance pay 2 to 1)\n ' \
284- ' \t My Face card is an Ace.\n ' \
285- ' \t Would your like buy a insurance ?'
276+ buy_insurance = ( "( Insurance pay 2 to 1)\n "
277+ " \t My Face card is an Ace.\n "
278+ " \t Would your like buy a insurance ?" )
286279 self .speak (content = buy_insurance )
287280
288281 def strategy_trigger (self , deck ):
@@ -296,7 +289,9 @@ def strategy_trigger(self, deck):
296289
297290class Player (User ):
298291 def __init__ (self , name , amount ):
299- super ().__init__ (name = name , chips_amount = amount , role = 'Player' ,
292+ super ().__init__ (name = name ,
293+ chips_amount = amount ,
294+ role = 'Player' ,
300295 color = 'CYAN' )
301296 self .refresh_prompt ()
302297
@@ -307,11 +302,13 @@ def refresh_prompt(self):
307302 def select_choice (self , pattern ):
308303 my_turn = 'My turn now.'
309304 self .speak (content = my_turn )
310- operation = dict (I = 'Insurance' ,
311- H = 'Hit' ,
312- S = 'Stand' ,
313- D = 'Double-down' ,
314- U = 'Surrender' )
305+ operation = {
306+ 'I' : 'Insurance' ,
307+ 'H' : 'Hit' ,
308+ 'S' : 'Stand' ,
309+ 'D' : 'Double-down' ,
310+ 'U' : 'Surrender'
311+ }
315312 enu_choice = enumerate ((operation .get (p ) for p in pattern ), 1 )
316313 dict_choice = dict (enu_choice )
317314 for index , operator in dict_choice .items ():
@@ -321,7 +318,6 @@ def select_choice(self, pattern):
321318
322319
323320class Recorder :
324-
325321 def __init__ (self ):
326322 self .data = []
327323 self .winner = None
@@ -345,34 +341,40 @@ def update(self, winner, chips, player_point, dealer_point):
345341
346342 def record (self , winner , chips , player_point , dealer_point ):
347343 self .update (winner , chips , player_point , dealer_point )
348- Row = namedtuple ('Row' , ['rounds' ,
349- 'player_point' ,
350- 'dealer_point' , 'winner' ,
351- 'remain_chips' ])
352- row = Row (self .rounds , self .player_point ,
353- self .dealer_point , self .winner ,
354- self .remain_chips )
344+ Row = namedtuple ('Row' , [
345+ 'rounds' , 'player_point' , 'dealer_point' , 'winner' , 'remain_chips'
346+ ])
347+ row = Row (self .rounds , self .player_point , self .dealer_point ,
348+ self .winner , self .remain_chips )
355349 self .data .append (row )
356350
357351 def draw_diagram (self ):
358- bars = '--' * 13
359- content = 'Record Display'
360- content_bar = bars + 'Record display' + bars
352+ content = 'Record display'
353+ bars = '--' * 14
354+ content_bar = bars + content + bars
361355 base_bar = bars + '-' * len (content ) + bars
362- title = 'Round\t Player-Point\t Dealer-Point\t Winner-is\t Remain-Chips'
363- row_fmt = '{}\t {}\t \t {}\t \t {}\t \t {}'
364356
357+ os .system ('clear' )
358+ print (base_bar )
359+ print (content_bar )
360+ print (base_bar )
361+ self .digram ()
365362 print (base_bar )
366363 print (content_bar )
367364 print (base_bar )
368365
366+ def digram (self ):
367+ title = 'Round\t Player-Point\t Dealer-Point\t Winner-is\t Remain-Chips'
368+ row_fmt = '{}\t {}\t \t {}\t \t {}\t \t {}'
369+
369370 print (title )
370371 for row in self .data :
371- print (row_fmt .format (row .rounds , row .player_point , row .dealer_point ,
372- row .winner , row .remain_chips ))
372+ print (
373+ row_fmt .format (row .rounds , row .player_point , row .dealer_point ,
374+ row .winner , row .remain_chips ))
373375
374- print (base_bar )
375- win_rate_fmt = 'Player win rate: {}%\n Dealer win rate: {}%'
376+ print ('' )
377+ win_rate_fmt = '>> Player win rate: {}%\n >> Dealer win rate: {}%'
376378 try :
377379 player_rate = round (self .player_win_count / self .rounds * 100 , 2 )
378380 dealer_rate = round (self .dealer_win_count / self .rounds * 100 , 2 )
@@ -381,10 +383,6 @@ def draw_diagram(self):
381383 dealer_rate = 0
382384 print (win_rate_fmt .format (player_rate , dealer_rate ))
383385
384- print (base_bar )
385- print (content_bar )
386- print (base_bar )
387-
388386
389387class BlackJack :
390388 def __init__ (self , username ):
@@ -481,11 +479,12 @@ def menu(self):
481479 pattern = 'HS'
482480 if self .first_hand :
483481 pattern += 'U'
484- if ( self .dealer .hand [1 ]. rank == 1 ) and \
485- self .player .chips .current_amount ():
482+ if self .dealer .hand [
483+ 1 ]. rank == 1 and self .player .chips .current_amount ():
486484 pattern += 'I'
487485 self .dealer .ask_insurance ()
488- if self .player .is_point ('>' , 10 ) and self .player .chips .can_double ():
486+ if self .player .is_point ('>' ,
487+ 10 ) and self .player .chips .can_double ():
489488 pattern += 'D'
490489 self .first_hand = False
491490 choices = self .player .select_choice (pattern )
@@ -512,7 +511,6 @@ def get_select(select_max, prompt='>> ', general_err=''):
512511
513512 def chips_manage (self ):
514513 if self .choice == 'Insurance' :
515- err = 'The amount should under ' + \
516514 err = ('The amount should under ' +
517515 str (self .player .chips .current_amount ()))
518516 pay_ins = self .get_select (self .player .chips .current_amount (),
@@ -555,10 +553,8 @@ def is_surrender(self):
555553
556554 def get_winner (self ):
557555 if self .bust :
558- if self .player .is_point ('>' , BLACK_JACK ):
559- return 'Dealer'
560- else :
561- return 'Dealer' if self .player .is_point ('>' , BLACK_JACK ) else 'Player'
556+ return 'Dealer' if self .player .is_point ('>' ,
557+ BLACK_JACK ) else 'Player'
562558
563559 if self .choice == 'Surrender' :
564560 return 'Dealer'
@@ -596,10 +592,7 @@ def calculate_chips(self):
596592 def result_exhibit (self ):
597593 def get_color ():
598594 if 'BUST' in content :
599- if 'Player' in content :
600- return COLOR .get ('RED' )
601- else :
602- return COLOR .get ('RED' if 'Player' in content else 'GREEN' )
595+ return COLOR .get ('RED' if 'Player' in content else 'GREEN' )
603596 if self .winner == 'Player' :
604597 return COLOR .get ('GREEN' )
605598 elif self .winner == 'Dealer' :
0 commit comments