-
Notifications
You must be signed in to change notification settings - Fork 0
/
worstPlayer.py
146 lines (126 loc) · 5.31 KB
/
worstPlayer.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
import math
import random
values = [2,3,4,5,6,7,8,9,10,11,12,13,14]
class WorstPlayer(object):
# this player shuffles their dict before doing anything, making them play random (but playable) cards
def __init__(self, name):
self.name = name
self.startingHand = []
self.valHand = []
self.cardDict = {
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
10: 0,
11: 0,
12: 0,
13: 0,
14: 0
}
self.totalCards = 0
def resetPlayer(self):
self.startingHand = []
self.valHand = []
self.cardDict = {2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0}
self.totalCards = 0
def sortHand(self):
for card in self.startingHand:
self.valHand.append(card.value)
self.valHand.sort()
# makes dictionary for how many of each card they have
for card in self.valHand:
self.totalCards += 1
for typeOfCard in self.cardDict:
if card == typeOfCard:
self.cardDict[typeOfCard] += 1
# put dict in reverse order
keys = list(self.cardDict.keys())
shuffledDict = {}
for key in reversed(keys):
shuffledDict.update({key: self.cardDict[key]})
self.cardDict = shuffledDict
# print(self.cardDict)
# Draw n number of cards from a given deck
def draw(self, deck, num=1):
for i in range(num):
self.startingHand.append(deck.draw())
def showValHand(self):
return self.valHand
def showHand(self):
return self.startingHand
def play(self, cardsOnTop):
# checks if they are out
self.totalCards = 0
for card in self.cardDict:
self.totalCards += self.cardDict[card]
if self.totalCards == 0:
return ['out']
# loops through cardDict (goes forward so plays highest possible first)
for card in self.cardDict:
# checks if its the same type of trick and if they have enough cards to play on the trick (at least one)
if self.cardDict[card] + self.cardDict[3] >= cardsOnTop[1] and self.cardDict[card] > 0:
# checks if the card is playable and not a two or three
# (shouldn't be possible to be a two or three cuz it should be a four or higher)
if card >= cardsOnTop[0] and not card == 2 and not card == 3:
# checks to see if you have enough of the card to play without threes (won't break up larger sets)
if self.cardDict[card] - cardsOnTop[1] == 0:
self.cardDict[card] -= cardsOnTop[1]
return [card, cardsOnTop[1]]
# this says it is ok to break up larger pairs if it is for matching
elif self.cardDict[card] - cardsOnTop[1] > 0 and card == cardsOnTop[0]:
self.cardDict[card] -= cardsOnTop[1]
return [card, cardsOnTop[1]]
# if you don't have enough without threes, plays what you do have as well as threes, but not if its matching
elif card != cardsOnTop[0] and cardsOnTop[1] > 1 and self.cardDict[card] < cardsOnTop[1]:
self.cardDict[3] -= cardsOnTop[1] - self.cardDict[card]
self.cardDict[card] = 0
return [card, cardsOnTop[1]]
# if it gets to here then it has nothing to play other than 2's and 3's
# checks if it has enough 3's to play as aces
if cardsOnTop[0] < 14 and self.cardDict[3] >= cardsOnTop[1]:
self.cardDict[3] -= cardsOnTop[1]
return [14, cardsOnTop[1]]
# checks if it has 2's to play and plays it
elif self.cardDict[2] > 0:
self.cardDict[2] -= 1
return [2, 1]
# if it has not returned by now then it needs to pass
else:
return ['pass']
def start(self):
# checks if they are out
self.totalCards = 0
for card in self.cardDict:
self.totalCards += self.cardDict[card]
if self.totalCards == 0:
return ['out']
# loops through card forward
for card in self.cardDict:
# checks the first card and plays one of it
if self.cardDict[card] > 0:
amountOfCard = self.cardDict[card]
self.cardDict[card] -= self.cardDict[card]
return [card, amountOfCard]
print(self.name + "ERROR start() didn't return?")
def giveLowestCard(self):
for card in self.cardDict:
if card != 2 and card != 3 and self.cardDict[card] > 0:
self.cardDict[card] -= 1
return card
def giveHighestCard(self):
if self.cardDict[2] > 0:
self.cardDict[2] -= 1
return 2
if self.cardDict[3] > 0:
self.cardDict[3] -= 1
return 3
# cant loop through dict reversed so i made a list of the cards at the top of this
for card in reversed(values):
if self.cardDict[card] > 0:
self.cardDict[card] -= 1
return card