[Tutor] Improving My Simple Game Code for Speed, Memory and Learning
WolfRage
wolfrage8765 at gmail.com
Sat Jan 3 23:24:07 CET 2015
On 01/03/2015 04:42 PM, Dave Angel wrote:
> On 01/03/2015 04:22 PM, WolfRage wrote:
>> On 01/03/2015 06:58 AM, Dave Angel wrote:
>>> To transpose a grid, you want to use the zip() function.
>>> self.transposed_grid = zip(*self.grid)
>> I see this gives me a list that is the column. Thus it solves the column
>> iteration problem, because now I can feed it to my checking and
>> elimination functions that take a slice.
>> Thanks!
>> Implementing now.
>
> I suspect you want instead:
>
> self.transposed_grid = list( zip(*self.grid) )
>
> in Python 3.4. zip gives an iterable for python 3.x, while it gave a
> list in python 2.x
>
> This is what I meant by "untested."
>
OK, I will try that implementation next. But I think this currently
works well. Here is the latest iteration of my code.
import random
class GameTile():
def __init__(self, col, row, values=None, value=None, **kwargs):
# values is not required because the value can be directly set.
# This is to support a future feature that will allow me to build a
# board off of a list.
# id is grid (X,Y) which is equal to grid (col,row)
self.id = str(col) + ',' + str(row)
self.col = col
self.row = row
if value is None:
value = random.choice(values)
self.value = value
self.eliminated = False
def __str__(self):
return "%2d" % self.value
class GameGrid():
def __init__(self, cols=8, rows=7, **kwargs):
if cols < 3 or rows < 3:
raise ValueError("Minimum board size is 3x3! %sx%s is too
small."
% (cols, rows))
self.cols = cols
self.rows = rows
self.values = [5, 6, 11, 19, 20]
self.make_grid()
def make_grid(self):
# grid is 2d array as x, y ie [x][y].
self.grid = []
for row_num in range(self.rows):
# Do you still think this needs to be broken into a smaller
method?
row = [GameTile(row_num, col_num, self.values)
for col_num in range(self.cols)]
self.grid.append(row)
self.transposed_grid = zip(*self.grid)
def draw(self):
for col in self.grid:
print(end='| ')
for node in col:
print(node, end=' | ')
print()
def find_eliminations(self):
#First Down the columns.
i = 0
for col_list in self.transposed_grid:
while True:
try:
if self.check_total(col_list[i: i + 3]):
self.eliminate(col_list[i: i + 3])
i += 1
except ValueError:
i = 0
break
# Now across the rows.
for row_list in self.grid:
while True:
try:
if self.check_total(row_list[i: i + 3]):
self.eliminate(row_list[i: i + 3])
i += 1
except ValueError:
i = 0
break
# Set all eliminated nodes to a value of 0.
for col in self.grid:
for node in col:
if node.eliminated is True:
node.eliminated = False
node.value = 0
def check_total(self, slices):
first, second, third = slices
if first.value == second.value or second.value == third.value:
total = first.value + second.value + third.value
return total in (17, 21, 28, 29, 31, 42, 45, 46, 49, 58)
def eliminate(self, slices):
first, second, third = slices
first.eliminated = True
second.eliminated = True
third.eliminated = True
def drop_floating_nodes(self):
pass
# Now we can used the transposed_grid!
grid = GameGrid(4, 8)
grid.draw()
grid.find_eliminations()
print('After Eliminations')
grid.draw()
More information about the Tutor
mailing list