-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.py
127 lines (108 loc) · 4.16 KB
/
Deck.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 6 23:53:49 2021
@author: ekohrt
Class that represents a magic deck
"""
class Deck:
"""
Constructor: takes a properly formatted deck (string) as input
exampleDeck = ("4 Chain Lightning\n"
"3 Curse of the Pierced Heart\n"
"4 Fireblast\n"
"4 Ghitu Lavarunner\n"
"4 Lightning Bolt\n"
"17 Mountain\n"
"\n"
"//Sideboard\n"
"2 Electrickery\n"
"3 Flame Rift\n")
"""
def __init__(self, deckString):
#original input string
self.deckString = deckString
allCards = Deck.parseDeckString(deckString)
#main deck dictionary {str_cardName: int_amnt}
self.main = allCards[0]
#sideboard dictionary {str_cardName: int_amnt}
self.sideboard = allCards[1]
#count # of cards in main / side
self.mainAmnt = sum(self.main.values())
self.sideboardAmnt = sum(self.sideboard.values())
#make list of strings for displaying main & side
#self.mainList = [self.currentDeck.main[cardName] + "x " + cardName for cardName in self.currentDeck.main.keys()]
#self.sideList = [self.currentDeck.sideboard[cardName] + "x " + cardName for cardName in self.currentDeck.sideboard.keys()]
"""
Converts this deck into a string
"""
def toString(self):
s = ""
for cardName in self.main.keys():
s += str(self.main[cardName]) + " " + str(cardName) + "\n"
s += "//Sideboard\n"
for cardName in self.sideboard.keys():
s += str(self.sideboard[cardName]) + " " + str(cardName) + "\n"
return s
"""
Add 1 given card to deck
"""
def addCard(self, str_cardToAdd):
#add 1 of this card to deck
if str_cardToAdd in self.main.keys():
self.main[str_cardToAdd] += 1
else:
self.main[str_cardToAdd] = 1
"""
Converts an input decklist string into a list of two dicts {str_cardName: int_amnt}
The first dict in the list is the main deck, second is sideboard.
Decklist must be formatted like "4 cardName\n17 cardName\n 3 cardName\n..."
"""
def parseDeckString(deckString):
#each card on a separate line. Strip whitespace from front/back of tokens. Delete empty items.
tokens = deckString.split("\n")
tokens = map(str.strip, tokens)
tokens = [value for value in tokens if value != ""]
#check if a token contains "sideboard" and get its index
sideboardIdx = -1
for t in tokens:
if "sideboard" in t.lower():
sideboardIdx = tokens.index(t)
#split into two lists of tokens: main and sideboard
if sideboardIdx != -1:
main_raw = tokens[:sideboardIdx]
side_raw = tokens[sideboardIdx+1:]
else:
main_raw = tokens
side_raw = []
#convert tokens into dicts
main = {}
side = {}
for token in main_raw:
#parse main deck
data = token.split(' ')
if len(data) < 2: continue
num = int(data[0])
cardName = " ".join(data[1:])
main[cardName] = num
for token in side_raw:
#parse sideboard
data = token.split(' ')
if len(data) < 2: continue
num = int(data[0])
cardName = " ".join(data[1:])
side[cardName] = num
return [main,side]
def main():
testDeck = ("4 Chain Lightning\n"
"3 Curse of the Pierced Heart\n"
"4 Fireblast\n"
"4 Ghitu Lavarunner\n"
"4 Lightning Bolt\n"
"17 Mountain\n"
"\n"
"//Sideboard\n"
"2 Electrickery\n"
"3 Flame Rift\n")
print(Deck.parseDeckString(testDeck))
if __name__ == "__main__":
main()