-
Notifications
You must be signed in to change notification settings - Fork 7
/
solitaire.py
175 lines (149 loc) · 7.8 KB
/
solitaire.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
from card_elements import Card, Deck, Pile
class Game:
values = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
suits = { #keys are unicode symbols for suits
u'\u2660': "black",
u'\u2665': "red",
u'\u2663': "black",
u'\u2666': "red",
}
numPlayPiles = 7
def __init__(self):
self.deck = Deck(self.values,self.suits)
self.playPiles = []
for i in range(self.numPlayPiles):
thisPile = Pile()
[thisPile.addCard(self.deck.takeFirstCard(flip=False)) for j in range(i+1)]
thisPile.flipFirstCard()
self.playPiles.append(thisPile)
self.blockPiles = {suit: Pile() for suit in self.suits}
self.deck.cards[0].flip()
def getGameElements(self):
returnObject = {
"deck": str(self.deck),
"playPiles": [str(pile) for pile in self.playPiles],
"blockPiles": {suit: str(pile) for suit, pile in self.blockPiles.items()}
}
return returnObject
def checkCardOrder(self,higherCard,lowerCard):
suitsDifferent = self.suits[higherCard.suit] != self.suits[lowerCard.suit]
valueConsecutive = self.values[self.values.index(higherCard.value)-1] == lowerCard.value
return suitsDifferent and valueConsecutive
def checkIfCompleted(self):
deckEmpty = len(self.deck.cards)==0
pilesEmpty = all(len(pile.cards)==0 for pile in self.playPiles)
blocksFull = all(len(pile.cards)==13 for suit,pile in self.blockPiles.items())
return deckEmpty and pilesEmpty and blocksFull
def addToBlock(self, card):
if card is None:
return False
elif len(self.blockPiles[card.suit].cards)>0:
highest_value = self.blockPiles[card.suit].cards[0].value
if self.values[self.values.index(highest_value)+1] == card.value:
self.blockPiles[card.suit].cards.insert(0,card)
return True
else:
return False
else:
if card.value=="A":
self.blockPiles[card.suit].cards.insert(0,card)
return True
else:
return False
def takeTurn(self, verbose=False):
#Pre: flip up unflipped pile end cards -> do this automatically
[pile.cards[0].flip() for pile in self.playPiles if len(pile.cards)>0 and not pile.cards[0].flipped]
#1: check if there are any play pile cards you can play to block piles
for pile in self.playPiles:
if len(pile.cards) > 0 and self.addToBlock(pile.cards[0]):
card_added = pile.cards.pop(0)
if verbose:
print("Adding play pile card to block: {0}".format(str(card_added)))
return True
#2: check if cards in deck can be added
if self.addToBlock(self.deck.getFirstCard()):
card_added = self.deck.takeFirstCard()
if verbose:
print("Adding card from deck to block: {0}".format(str(card_added)))
return True
#3: move kings to open piles
for pile in self.playPiles:
if len(pile.cards)==0: #pile has no cards
for pile2 in self.playPiles:
if len(pile2.cards)>1 and pile2.cards[0].value == "K":
card_added = pile2.cards.pop(0)
pile.addCard(card_added)
if verbose:
print("Moving {0} from Pile to Empty Pile".format(str(card_added)))
return True
if self.deck.getFirstCard() is not None and self.deck.getFirstCard().value == "K":
card_added = self.deck.takeFirstCard()
pile.addCard(card_added)
if verbose:
print("Moving {0} from Deck to Empty Pile".format(str(card_added)))
return True
#4: add drawn card to playPiles
for pile in self.playPiles:
if len(pile.cards)>0 and self.deck.getFirstCard() is not None:
if self.checkCardOrder(pile.cards[0],self.deck.getFirstCard()):
card_added = self.deck.takeFirstCard()
pile.addCard(card_added)
if verbose:
print("Moving {0} from Deck to Pile".format(str(card_added)))
return True
#5: move around cards in playPiles
for pile1 in self.playPiles:
pile1_flipped_cards = pile1.getFlippedCards()
if len(pile1_flipped_cards)>0:
for pile2 in self.playPiles:
pile2_flipped_cards = pile2.getFlippedCards()
if pile2 is not pile1 and len(pile2_flipped_cards)>0:
for transfer_cards_size in range(1,len(pile1_flipped_cards)+1):
cards_to_transfer = pile1_flipped_cards[:transfer_cards_size]
if self.checkCardOrder(pile2.cards[0],cards_to_transfer[-1]):
pile1_downcard_count = len(pile1.cards) - len(pile1_flipped_cards)
pile2_downcard_count = len(pile2.cards) - len(pile2_flipped_cards)
if pile2_downcard_count < pile1_downcard_count:
[pile2.cards.insert(0,card) for card in reversed(cards_to_transfer)]
pile1.cards = pile1.cards[transfer_cards_size:]
if verbose:
print("Moved {0} cards between piles: {1}".format(
transfer_cards_size,
", ".join([str(card) for card in cards_to_transfer])
))
return True
elif pile1_downcard_count==0 and len(cards_to_transfer) == len(pile1.cards):
[pile2.cards.insert(0,card) for card in reversed(cards_to_transfer)]
pile1.cards = []
if verbose:
print("Moved {0} cards between piles: {1}".format(
transfer_cards_size,
", ".join([str(card) for card in cards_to_transfer])
))
return True
return False
def simulate(self, draw = False, verbose=False):
# clear cache if last turn was not card draw
if not draw:
self.deck.cache = []
turnResult = self.takeTurn(verbose=verbose)
if turnResult:
self.simulate(verbose=verbose)
else:
#End: draw from deck
if len(self.deck.cards)>0:
currentCard = self.deck.cards[0]
if currentCard in self.deck.cache:
if verbose:
print("No more moves left!")
return
else:
self.deck.drawCard()
#if verbose:
#print("Drawing new card: {0}".format(str(currentCard)))
self.deck.cache.append(currentCard)
return self.simulate(draw=True, verbose=verbose)
else:
if verbose:
print("No more moves left!")
return