-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanapool.py
More file actions
85 lines (72 loc) · 2.83 KB
/
manapool.py
File metadata and controls
85 lines (72 loc) · 2.83 KB
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
import re
import numpy as np
from errors import GameStateError
class ManaPool():
def __init__(self, mana_string = None):
self.mana = {'white':0, 'blue':0, 'black':0, 'red': 0, 'green': 0,
'colorless': 0,'generic': 0}
if mana_string:
self.init_by_string(mana_string)
def init_by_string(self, mana_string):
mana_string = str.lower(mana_string)
self.mana['white'] = mana_string.count('w')
self.mana['blue'] = mana_string.count('u')
self.mana['black'] = mana_string.count('b')
self.mana['red'] = mana_string.count('r')
self.mana['green'] = mana_string.count('g')
self.mana['colorless'] = mana_string.count('o')
self.mana['generic'] = np.sum(re.findall(r'\d+', mana_string))
def empty_pool(self):
self.mana['white'] = 0
self.mana['blue'] = 0
self.mana['black'] = 0
self.mana['red'] = 0
self.mana['green'] = 0
self.mana['colorless'] = 0
self.mana['generic'] = 0
def __repr__(self):
return '[W: ' + str(self.mana['white']) + \
' - U: ' + str(self.mana['blue']) + \
' - B: ' + str(self.mana['black']) + \
' - R: ' + str(self.mana['red']) + \
' - G: ' + str(self.mana['green']) + \
' - O: ' + str(self.mana['colorless']) + \
' - any: ' + str(self.mana['generic']) + \
']'
def reduce_by(self, cost):
if type(cost) == str:
self -= ManaPool(cost)
def add(self, value):
if type(value) == str:
self += ManaPool(value)
else:
pass
def __add__(self, other):
for key in self.mana:
if key != 'generic':
self.mana[key] += other.mana[key]
def __sub__(self, other):
for key in self.mana:
if key != 'generic':
self.mana[key] -= other.mana[key]
# process generic, can be any order
if other.mana['generic'] > 0:
generic_to_deduct = other.mana['generic']
for key in self.mana:
mana = self.mana[key]
if generic_to_deduct <= 0:
break
if mana > 0:
if mana <= generic_to_deduct:
generic_to_deduct -= mana
self.mana[key] = 0
else:
self.mana[key] -= generic_to_deduct
generic_to_deduct = 0
if generic_to_deduct > 0:
raise GameStateError('Mana left to pay, cannot cast '
'this spell')
# make sure all mana values are positive
for key in self.mana:
if self.mana[key] < 0:
raise GameStateError("Mana value below 0 found")