-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.py
67 lines (56 loc) · 1.71 KB
/
cell.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
import random
from enums import Flags
from views import Cell as View
class Cell:
def __init__(self, value=Flags.EMPTY):
self.value = value
def __str__(self):
return View.render(self)
@classmethod
def random_color(cls):
return Flags(Flags.RED * random.randint(1,3))
def infect(self, color=None):
if color is None:
color = self.random_color()
assert color in (Flags.RED, Flags.YELLOW, Flags.BLUE)
self.value = Flags(Flags.VIRUS | color)
def treat(self, color=None, bound=Flags.BIND_NONE):
if color is None:
color = self.random_color()
assert color in (Flags.RED, Flags.YELLOW, Flags.BLUE)
self.value = Flags(Flags.PILL | color | bound)
def bind(self, value):
mask = Flags.BIND_LEFT | Flags.BIND_RIGHT
self.value &= ~mask
self.value |= value & mask
self.value = Flags(self.value)
def bind_left(self):
self.bind(Flags.BIND_LEFT)
def bind_right(self):
self.bind(Flags.BIND_RIGHT)
def bind_below(self):
self.value |= Flags.BIND_BELOW
def zap(self):
self.value |= Flags.ZAP
def unbind(self):
self.value &= ~(Flags.BIND_LEFT | Flags.BIND_RIGHT | Flags.BIND_BELOW)
def empty(self):
self.value = Flags.EMPTY
def is_empty(self):
return self.value == Flags.EMPTY
def color(self):
return self.value & (Flags.RED | Flags.YELLOW | Flags.BLUE)
def is_pill(self):
return self.value & Flags.PILL
def is_virus(self):
return self.value & Flags.VIRUS
def is_zapped(self):
return self.value & Flags.ZAP == Flags.ZAP
def is_bound(self):
return self.value & (Flags.BIND_LEFT | Flags.BIND_RIGHT)
def is_bound_left(self):
return self.value & Flags.BIND_LEFT
def is_bound_right(self):
return self.value & Flags.BIND_RIGHT
def is_bound_below(self):
return self.value & Flags.BIND_BELOW