-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingoCard.py
104 lines (85 loc) · 2.86 KB
/
BingoCard.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
#!/usr/bin/env python
class GridBox():
def __init__(self, data=None, loc=0):
self.mark = False
self.data = data
self.loc = loc
def __str__(self):
return str(self.data)
from copy import deepcopy
class BingoCard():
"""
A single bingo card
"""
def __init__(self, size=5, data=range(1,50)):
self.size = size
self.card = [GridBox(item,loc)
for (loc,item)
in zip(xrange(size*size),data)]
def match(self, item):
return [x for x in self.card if x.data == item]
def mark(self, item):
def m(item):
m.mark = True
[m(x) for x in self.match(item)]
def multiplicativeMark(self, item):
""" Makes new card[s] """
result = list()
for loc in [x.loc for x in self.match(item) if not x.mark]:
result.append(deepcopy(self))
result[-1].card[loc].data = "*"
result[-1].card[loc].mark = True
return result
def bingo(self):
for i in xrange(self.size):
start = i * self.size
end = i * self.size + self.size
horiz = self.card[start:end]
vert = self.card[i::self.size]
if all([x.mark for x in horiz]):
return True
if all([x.mark for x in vert]):
return True
return False
def __str__(self):
result = ""
for j in xrange(self.size):
for i in xrange(self.size):
result += str(self.card[i + j*self.size])
result += "\n"
return result
class NonDeterministicFiniteStateBingoPlayer():
"""
Given a bingo card with repeats, makes all possible choices
simultaneously. Accomplishes this task by multiplying his card.
"""
def __init__(self, card):
self.cards = [card]
def bingo(self):
return any([card.bingo() for card in self.cards])
def bingos(self):
return [x for x in self.cards if x.bingo()]
def mark(self, item):
newCards = list()
for card in self.cards:
newCards += card.multiplicativeMark(item)
self.cards = newCards
NDFSBingoPlayer = NonDeterministicFiniteStateBingoPlayer
from random import Random
class BingoCardFactory():
"""
Generates reproduceable random bingo cards
"""
def __init__(self, seed=0, size=5, data=range(1,49)):
self.size = size
self.data = deepcopy(data)
self.rng = Random()
self.rng.seed(seed)
def createBingoCard(self):
data = list()
while len(data) < (self.size*self.size):
data += self.data
self.rng.shuffle(data)
return BingoCard(self.size, data)
def createBingoCards(self, num=10):
return [self.createBingoCard() for i in xrange(num)]