reworked Board.called to be a table of booleans for faster usage

This commit is contained in:
Josh Holtrop 2016-04-12 21:40:33 -04:00
parent 0b01e6f281
commit d2d644ffd3

View File

@ -3,12 +3,13 @@ import random
class Board: class Board:
def __init__(self): def __init__(self):
self.cols = [] self.cols = []
self.called = []
for col in range(5): for col in range(5):
n = 4 if col == 2 else 5 n = 4 if col == 2 else 5
self.cols.append(random.sample(range(15 * col + 1, 15 * col + 15), n)) self.cols.append(random.sample(range(15 * col + 1, 15 * col + 15), n))
self.called.append([False] * 5)
self.cols[2][2:2] = [0] self.cols[2][2:2] = [0]
self.border = "+----" * 5 + "+" self.border = "+----" * 5 + "+"
self.called = {0}
def print(self): def print(self):
for row in range(5): for row in range(5):
@ -18,10 +19,13 @@ class Board:
caption = self.cols[col][row] caption = self.cols[col][row]
if caption == 0: if caption == 0:
caption = "F" caption = "F"
called = "*" if self.cols[col][row] in self.called else " " called = "*" if self.called[col][row] else " "
print("|%s%2s%s" % (called, caption, called), end = "") print("|%s%2s%s" % (called, caption, called), end = "")
print("|") print("|")
print(self.border) print(self.border)
def call(self, n): def call(self, n):
self.called.add(n) for col in range(5):
for row in range(5):
if self.cols[col][row] == n:
self.called[col][row] = True