-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.py
170 lines (147 loc) · 5.8 KB
/
player.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
"""
Project Name: blackjack-bot
File Name: player.py
Author: Connor York (cxy1054@rit.edu)
Updated: 7/20/16
Discord is a voice and chat app for gamers created by Hammer & Chisel, a startup based in Burlingame, CA.
More information on Discord and Hammer & Chisel can be found through the following links:
https://discordapp.com/
https://discordapp.com/company
blackjack-bot is developed using the unofficial API for Discord. It is made and run by developers not affiliated with
the company. The library used in this project can be found in the link below:
https://github.com/Rapptz/discord.py
Description: blackjack-bot is a Discord 'bot' for emulating the card game Blackjack in the chat channels of servers.
A 'bot' is essentially a user that is run by some sort of AI instead of a person. They perform actions based on
messages in chat that are interpreted as commands. blackjack-bot uses commands in chat to emulate Blackjack.
(These are probably not the correct terms in Blackjack, but they are consistently used within their definition in this project)
TERMS:
ROUND = A decision, where each player decides what to do with their hand ONCE.
GAME = All of the rounds, from the initial betting till each player cannot play anymore and either wins or loses.
SESSION = All of the games. 'in session' means that there are currently players playing.
The MIT License (MIT)
Copyright (c) 2016 Connor York
"""
import card
class Player:
"""
Represents a player in Blackjack, which can be a dealer or a human player.
Parameters:
member | :class: 'Member'
The member to be connected to the in-game player.
Attributes:
member | :class: 'Member'
Unique id for a specific user
hand | list
Amount of money the player currently possesses
is_playing | bool
States if the player is currently playing in the game (Not holding or bust)
This will be False if the player is not eligible to play more rounds in the game (busted or held hand)
has_played | bool
States if the player has currently played in the round (Has not hit or held)
This will always be True if the player is not eligible to play more rounds (busted or held hand)
"""
def __init__(self, member):
self.member = member
self.hand = list()
self.is_playing = True
self.has_played = False
self.is_busted = False
def deal(self):
"""
Assigns an initial hand of two random cards to the player.
"""
self.is_playing = True
for _ in range(2):
self.hand.append(card.Card.draw_card())
def hand_str(self, num_cards=None):
"""
Creates a string from the player's hand
:param num_cards: the number of cards in hand to print. If None, prints entire hand.
:return: str of cards in hand
"""
hand_string = ""
if not num_cards:
num_cards = len(self.hand)
for i in range(num_cards):
hand_string += " " + self.hand[i].__str__() + "\n"
return hand_string
def reset_hand(self):
"""
Resets the players hand to an empty hand of no cards
"""
self.hand.clear()
def reset(self):
"""
Resets the player to initial state
"""
self.reset_hand()
self.is_playing = True
self.has_played = False
self.is_busted = False
def hit(self):
"""
Adds a card to the players hand
:return: True if successful hit, False if not
"""
if self.is_playing and not self.has_played and not self.is_bust():
self.hand.append(card.Card.draw_card())
self.has_played = True
return True
return False
def hold(self):
"""
Holds the players current hand, preventing them from hitting more cards
:return: True if a successful hold, False if not
"""
if self.is_playing and not self.has_played:
self.is_playing = False
self.has_played = True
return True
return False
def get_hand_values(self):
"""
Determines the possible values of the current players hand
This is useful when a player has an Ace in their hand, as it can be of value 1 or 11.
:return: list of possible integer values of the players current hand
"""
values = list()
has_ace = False
hand_value = 0
for c in self.hand:
if isinstance(c, card.Card):
if c.value is 1:
has_ace = True
else:
hand_value += c.value
if has_ace: # append the current hand value with the two values of an Ace
values.append(hand_value + 1)
values.append(hand_value + 11)
else:
values.append(hand_value)
return values
def is_bust(self):
"""
Determines if the current player is busted (has a hand value of over 21).
If the hand has multiple values, both must be a value over 21 in order to be considered busted.
:return: Boolean stating if busted
"""
for value in self.get_hand_values():
if value <= 21:
return False
return True
def has_blackjack(self):
"""
Determines if the player has blackjack, a hand of value 21.
:return: Boolean stating if the player has 21
"""
for value in self.get_hand_values():
if value is 21:
return True
return False
def bust(self):
"""
Sets the player's state to that of a player that cannot play anymore.
"""
self.is_playing = False
self.has_played = True
self.is_busted = True