Bug fix for alises using unicode.
Lots of fixes for blackjack
Showing
3 changed files
with
68 additions
and
19 deletions
... | @@ -46,21 +46,22 @@ class Deck: | ... | @@ -46,21 +46,22 @@ class Deck: |
46 | self.shuffle(number_of_decks) | 46 | self.shuffle(number_of_decks) |
47 | 47 | ||
48 | def shuffle(self, number_of_decks): | 48 | def shuffle(self, number_of_decks): |
49 | self.cards = [] | 49 | self.draw_pile = [] |
50 | for d in range(number_of_decks): | 50 | for d in range(number_of_decks): |
51 | for suit in SUITS: | 51 | for suit in SUITS: |
52 | for card in CARDS: | 52 | for card in CARDS: |
53 | self.cards.append(card + suit) | 53 | self.draw_pile.append(card + suit) |
54 | 54 | ||
55 | def draw(self,number_of_cards=1): | 55 | def draw(self, number_of_cards=1): |
56 | drawn_cards = [] | 56 | drawn_cards = [] |
57 | for n in range(number_of_cards): | 57 | for n in range(number_of_cards): |
58 | choice = randrange(0,len(self.cards)) | 58 | print("CARDS LEFT: {}".format(self.cards_left())) |
59 | drawn_cards.append(self.cards.pop(choice)) | 59 | choice = randrange(0, len(self.draw_pile)) |
60 | drawn_cards.append(self.draw_pile.pop(choice)) | ||
60 | return drawn_cards | 61 | return drawn_cards |
61 | 62 | ||
62 | def cards_left(self): | 63 | def cards_left(self): |
63 | return len(self.cards) | 64 | return len(self.draw_pile) |
64 | 65 | ||
65 | class Blackjack: | 66 | class Blackjack: |
66 | def __init__(self, bet): | 67 | def __init__(self, bet): |
... | @@ -78,6 +79,13 @@ class Blackjack: | ... | @@ -78,6 +79,13 @@ class Blackjack: |
78 | self.dealer_points = self.dealer_hand.get_points() | 79 | self.dealer_points = self.dealer_hand.get_points() |
79 | #print("Dealer: {}".format(dealer_points,)) | 80 | #print("Dealer: {}".format(dealer_points,)) |
80 | 81 | ||
82 | def draw(self): | ||
83 | self.player_hand.add_cards(self.deck.draw(1)) | ||
84 | |||
85 | def dealer_draw(self): | ||
86 | while self.player_hand.get_points() <= 21 and (self.dealer_hand.get_points() < 17 or self.dealer_hand.get_points() < self.player_hand.get_points()): | ||
87 | self.dealer_hand.add_cards(self.deck.draw(1)) | ||
88 | |||
81 | def is_busted(self): | 89 | def is_busted(self): |
82 | if self.player_points > 21: | 90 | if self.player_points > 21: |
83 | return 1 | 91 | return 1 |
... | @@ -87,9 +95,17 @@ class Blackjack: | ... | @@ -87,9 +95,17 @@ class Blackjack: |
87 | return 0 | 95 | return 0 |
88 | 96 | ||
89 | def is_win(self): | 97 | def is_win(self): |
98 | self.dealer_draw() | ||
99 | self.dealer_points = self.dealer_hand.get_points() | ||
100 | self.player_points = self.player_hand.get_points() | ||
101 | |||
90 | # TODO: Check for blackjack Ace + 10 pt | 102 | # TODO: Check for blackjack Ace + 10 pt |
91 | if self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack(): | 103 | if self.player_points > 21: |
104 | return (-self.bet, 'You Bust: {}') | ||
105 | elif self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack(): | ||
92 | return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}') | 106 | return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}') |
107 | elif self.dealer_points > 21: | ||
108 | return (self.bet * 2, 'Dealer Busts: {}') | ||
93 | elif self.dealer_hand.is_blackjack(): | 109 | elif self.dealer_hand.is_blackjack(): |
94 | return (-self.bet, 'You lose: {}') | 110 | return (-self.bet, 'You lose: {}') |
95 | elif self.player_points > self.dealer_points: | 111 | elif self.player_points > self.dealer_points: |
... | @@ -99,20 +115,53 @@ class Blackjack: | ... | @@ -99,20 +115,53 @@ class Blackjack: |
99 | else: | 115 | else: |
100 | return (-self.bet, 'You lose: {}') | 116 | return (-self.bet, 'You lose: {}') |
101 | 117 | ||
102 | def print_hand(self): | 118 | def print_hand(self, show_dealer=False): |
119 | if show_dealer: | ||
120 | dealers = ' '.join(self.dealer_hand.get_cards()) | ||
121 | print("Dealer's Hand: {} for {} points".format(dealers, self.dealer_hand.get_points())) | ||
122 | else: | ||
103 | dealers = ' '.join(self.dealer_hand.get_cards()[1:]) | 123 | dealers = ' '.join(self.dealer_hand.get_cards()[1:]) |
104 | print("Dealer's Hand: {}".format(dealers)) | 124 | print("Dealer's Hand: {}".format(dealers)) |
105 | 125 | ||
126 | self.player_points = self.player_hand.get_points() | ||
127 | print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_hand.get_points())) | ||
128 | |||
129 | def get_actions(self): | ||
130 | points = self.player_hand.get_points() | ||
131 | cards = self.player_hand.get_cards() | ||
132 | actions = [] | ||
133 | if self.player_hand.is_blackjack(): | ||
134 | pass | ||
135 | elif points < 21 and not self.is_busted() == 2: | ||
136 | actions.append('hit') | ||
137 | actions.append('stand') | ||
138 | actions.append('double') | ||
139 | elif len(cards) == 2: | ||
140 | if cards[0][0] == cards[1][0]: | ||
141 | actions.append('split') | ||
142 | return actions | ||
106 | 143 | ||
107 | print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_points)) | ||
108 | bet = 10 | 144 | bet = 10 |
109 | bj = Blackjack(bet) | 145 | bj = Blackjack(bet) |
110 | bj.print_hand() | 146 | bj.print_hand() |
111 | busted = bj.is_busted() | 147 | actions = bj.get_actions() |
112 | if busted == 1: | 148 | done = False |
113 | print("YOU BUST") | 149 | while len(actions) > 0: |
114 | elif busted == 2: | 150 | action = raw_input('Please choose an option [{}]'.format(', '.join(actions))) |
115 | print("Dealer Busts, YOU WIN!") | 151 | if action == 'stand': |
116 | else: | 152 | break |
117 | win, response = bj.is_win() | 153 | elif action == 'hit': |
118 | print(response.format(win,)) | 154 | bj.draw() |
155 | bj.print_hand() | ||
156 | |||
157 | actions = bj.get_actions() | ||
158 | |||
159 | win, response = bj.is_win() | ||
160 | print('\n') | ||
161 | bj.print_hand(show_dealer=True) | ||
162 | print('\n') | ||
163 | print(response.format(win,)) | ||
164 | |||
165 | |||
166 | |||
167 | ... | ... |
No preview for this file type
... | @@ -514,7 +514,7 @@ Stuff: | ... | @@ -514,7 +514,7 @@ Stuff: |
514 | if member: | 514 | if member: |
515 | aliases = db_get_aliases(member['member_id']) | 515 | aliases = db_get_aliases(member['member_id']) |
516 | if aliases: | 516 | if aliases: |
517 | client.send_message(message.channel, '{} has the following aliases: {}'.format(username, ', '.join(aliases))) | 517 | client.send_message(message.channel, '{} has the following aliases: {}'.format(username, byteify(', '.join(aliases)))) |
518 | else: | 518 | else: |
519 | client.send_message(message.channel, 'No known alises for {} yet {}'.format(username, message.author.mention())) | 519 | client.send_message(message.channel, 'No known alises for {} yet {}'.format(username, message.author.mention())) |
520 | else: | 520 | else: |
... | @@ -527,7 +527,7 @@ Stuff: | ... | @@ -527,7 +527,7 @@ Stuff: |
527 | member = db_get_member(username=username) | 527 | member = db_get_member(username=username) |
528 | if member: | 528 | if member: |
529 | db_add_aliases(member['member_id'], alias) | 529 | db_add_aliases(member['member_id'], alias) |
530 | client.send_message(message.channel, '{} has been added to your aliases'.format(alias)) | 530 | client.send_message(message.channel, '{} has been added to your aliases'.format(byteify(alias))) |
531 | else: | 531 | else: |
532 | client.send_message(message.channel, 'Something horrible happened and it is all your fault, try logging out / on again or play a game. (or fuck off i dunno i\'m just an error message. Who am I to tell you how to run your life...)') | 532 | client.send_message(message.channel, 'Something horrible happened and it is all your fault, try logging out / on again or play a game. (or fuck off i dunno i\'m just an error message. Who am I to tell you how to run your life...)') |
533 | return | 533 | return | ... | ... |
-
Please register or sign in to post a comment