Originally posted by true911m
Code:
self.name=name # passed in when instantiated
self.name=name # passed in when instantiated
self.name=name # passed in when instantiated
# it is legal to access the variables directly
print inst1.cv1
import random
class BingoCard:
def __init__(self):
self.card=[]
def FillGrid(self):
for row in range(5):
self.rowlist=[]
for col in range(5):
self.rowlist.append(Square(self,"%d"%row,"%d"%col))
self.card.append(self.rowlist[:])
def ShowGrid(self):
for row in self.card:
print
for sq in row:
sq.ShowName()
class Square:
def __init__(self, parent, row,col):
self.row=row
self.col=col
self.name="square-"+str(row)+"-"+str(col)
self.parent=parent
def ShowName(self):
print self.name+" ",
card=BingoCard()
card.FillGrid()
card.ShowGrid()
print
print
card.card[2][1].ShowName()
square-0-0 square-0-1 square-0-2 square-0-3 square-0-4 square-1-0 square-1-1 square-1-2 square-1-3 square-1-4 square-2-0 square-2-1 square-2-2 square-2-3 square-2-4 square-3-0 square-3-1 square-3-2 square-3-3 square-3-4 square-4-0 square-4-1 square-4-2 square-4-3 square-4-4 square-2-1
def FillGrid(self):
for row in range(5):
self.rowlist=[]
for col in range(5):
self.rowlist.append(Square(self,"%d"%row,"%d"%col))
self.card.append(self.rowlist[:])
def FillGrid(self):
for row in range(5):
rowlist=[]
for col in range(5):
rowlist.append(Square(self,"%d"%row,"%d"%col))
self.card.append(rowlist[:]) # since you want [:] probably don't need to slice
del objcect
object = None
B I N G O 8 26 36 48 75 2 22 38 58 64 10 24 ## 51 63 3 30 44 59 69 15 25 41 53 62 30 balls drawn: 46 55 53 26 58 16 30 22 64 49 67 32 50 4 21 20 6 2 18 27 62 11 23 15 35 5 51 66 47 38 BINGO!!! B I N G O 8 ## 36 48 75 ## ## ## ## ## 10 24 ## ## 63 3 ## 44 59 69 ## 25 41 ## ##
import random
class BingoCard:
def __init__(self,prnt):
self.card=[]
self.parent=prnt
self.BingoPatterns=[\
[[0,0],[0,4],[2,2],[4,0],[4,4]],\
[[0,0],[1,1],[2,2],[3,3],[4,4]],\
[[4,0],[3,1],[2,2],[1,3],[0,4]],\
[[0,0],[0,1],[0,2],[0,3],[0,4]],\
[[1,0],[1,1],[1,2],[1,3],[1,4]],\
[[2,0],[2,1],[2,2],[2,3],[2,4]],\
[[3,0],[3,1],[3,2],[3,3],[3,4]],\
[[4,0],[4,1],[4,2],[4,3],[4,4]],\
[[0,0],[1,0],[2,0],[3,0],[4,0]],\
[[0,1],[1,1],[2,1],[3,1],[4,1]],\
[[0,2],[1,2],[2,2],[3,2],[4,2]],\
[[0,3],[1,3],[2,3],[3,3],[4,3]],\
[[0,4],[1,4],[2,4],[3,4],[4,4]]\
]
def FillCard(self):
if len(self.card):
for row in self.card:
for square in row:
square.ResetMatched()
square.ResetNumber()
else:
for row in range(5):
rowlist=[]
for col in range(5):
rowlist.append(BingoSquare(self,"%d"%row,"%d"%col))
self.card.append(rowlist)
def ShowCard(self):
print; print'B\tI\tN\tG\tO'
for row in self.card:
print
for square in row:
if square.GetMatched():
print '##\t',
else:
print '%d\t' % square.GetNumber(),
print
def NewGame(self):
NumbersLeft=[]
for i in range(5):
NumbersLeft.append(range(1+(i*15),16+(i*15)))
self.FillCard()
for row in self.card:
for i in range(5):
rnum=random.randint(0,len(NumbersLeft[i])-1)
row[i].SetNumber(NumbersLeft[i][rnum])
#print i,NumbersLeft[i][rnum]
#print square.GetNumber()
NumbersLeft[i]=NumbersLeft[i][:rnum]+NumbersLeft[i][rnum+1:]
#print NumbersLeft
self.card[2][2].SetNumber(0)
self.card[2][2].SetMatched()
#self.ShowCard()
def CheckMatches(self,num):
for row in self.card:
for square in row:
if square.GetNumber()==num:
square.SetMatched()
def CheckBingo(self):
for set in self.BingoPatterns:
bingo=True
for coords in set:
if not self.card[coords[0]][coords[1]].GetMatched():
bingo=False
break
if bingo: return True
return False
class BingoSquare:
def __init__(self, prnt, row,col):
self.row=row
self.col=col
self.name="sqr"+str(row)+str(col)
self.parent=prnt
self.number=0
self.matched=False
def ShowName(self):
pass
print self.name+" ",
def SetNumber(self,num):
self.number=num
def ResetNumber(self):
self.number=None
def GetNumber(self):
return self.number
def SetMatched(self):
self.matched=True
def ResetMatched(self):
self.matched=False
def GetMatched(self):
return self.matched
class BingoMixer:
def __init__(self,prnt):
self.balls=[]
self.parent=prnt
def FillBox(self):
if len(self.balls):
for ball in self.balls:
ball.ResetDrawn()
ball.ResetNumber()
else:
for ball in range(75):
self.balls.append(BingoBall(self,"%d"%ball))
def ShowBox(self):
for ball in self.balls:
ball.ShowName()
def ShowBalls(self):
for ball in self.balls:
print ball.GetNumber(), # ball.GetDrawn()
def ShowDrawn(self):
for i in range(len(self.balls)):
if not self.balls[i].GetDrawn():
self.drawnBalls=self.balls[:i]
print; print '%d balls drawn:' % len(self.drawnBalls)
print
for ball in self.drawnBalls:
print ball.GetNumber(),
break
print
def NewGame(self):
NumbersLeft=range(1,76)
self.FillBox()
for ball in self.balls:
rnum=random.randint(0,len(NumbersLeft)-1)
ball.SetNumber(NumbersLeft[rnum])
#print ball.GetNumber()
NumbersLeft=NumbersLeft[:rnum]+NumbersLeft[rnum+1:]
#print NumbersLeft
#self.ShowBalls()
class BingoBall:
def __init__(self, parent, ballno):
self.name="ball"+str(ballno)
self.parent=parent
self.number=0
self.drawn=False
def ShowName(self):
print self.name,
def SetNumber(self,num):
self.number=num
def ResetNumber(self):
self.number=None
def GetNumber(self):
return self.number
def SetDrawn(self):
self.drawn=True
def ResetDrawn(self):
self.drawn=False
def GetDrawn(self):
return self.drawn
class BingoGame:
import random
random.seed()
def __init__(self):
self.card=BingoCard(self)
self.mixer=BingoMixer(self)
def Play(self):
bingo=False
self.card.NewGame()
self.mixer.NewGame()
self.card.ShowCard()
for ball in self.mixer.balls:
thisNum=ball.GetNumber()
ball.SetDrawn()
#print;print thisNum
self.card.CheckMatches(thisNum)
#self.card.ShowCard()
if self.card.CheckBingo():
break
self.mixer.ShowDrawn()
print; print 'BINGO!!!'
self.card.ShowCard()
quit()
if __name__ == '__main__':
game=BingoGame()
game.Play()
Comment