Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning

2015-01-11 Thread WolfRage

On 01/05/2015 06:21 PM, Danny Yoo wrote:
SNIP

if total in (17, 21, 28, ...):

Implemented, thanks.
SNIP


The other comment I'd make is to start thinking about how you'd _test_
your program automatically.

SNIP
You are right, I should. But I am fighting with myself on this topic. I 
have been fighting myself for some time. As it seems the test slow me 
down. I am sure latter they may speed me up, but then again I am still a 
relatively new programmer and so my code is very fluid to the situation 
at hand, so tests for immature code seems like a bad idea. Still you are 
right.



Now I am having problem implementing a way to drop the zero values to 
the bottom of the grid. I need to look at more of the column at once 
possibly all of it, or remember where I was dropping. This is my latest 
code any help would be appreciated.



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 = list(zip(*self.grid))

def draw(self):
for col in self.grid:
print(end='| ')
for node in col:
print(node, end=' | ')
print()

def draw_by_id(self):
for col in self.grid:
print(end='| ')
for node in col:
print(node.id, 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_nodes0(self):
i = self.rows
# first_zero_row serves as memory for how far to drop non-zero 
values

first_zero_row = None
for col_list in self.transposed_grid:
while True:
low, high = col_list[i - 2: i]  # Goes Up the Rows
if high.value == 0 and low.value != 0:
if first_zero_row is None:
high.value = low.value
low.value = 0
first_zero_row = low
else:
first_zero_row.value = low.value
low.value = 0
high.value = 0
i -= 1
if i == 1:
i = self.rows
first_zero_row = None
break

def drop_floating_nodes1(self):
i = 0
for col_list in self.transposed_grid:
while True:
try:
low, 

Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning

2015-01-11 Thread WolfRage
I had an issue in my logic and again my named variables provided for 
confusion, so I had to add some comments to clarify.


I got much closer by editing my code like this:

def drop_floating_nodes0(self):
i = self.rows
# first_zero_row serves as memory for how far to drop non-zero 
values

first_zero_row = None
for col_list in self.transposed_grid:
while True:
# Low is on Top, High is on Bottom
low, high = col_list[i - 2: i]  # Goes Up the Rows
if high.value == 0:
if low.value != 0:
if first_zero_row is None:
high.value = low.value
low.value = 0
first_zero_row = low
else:
first_zero_row.value = low.value
low.value = 0
high.value = 0
first_zero_row = low
else:
if first_zero_row is None:
first_zero_row = high
i -= 1
if i == 1:
i = self.rows
first_zero_row = None
break

But it still fails as you can see here from the output:

| 20 | 19 | 11 | 20 |
| 11 |  5 |  5 | 11 |
| 19 |  5 | 11 | 11 |
| 19 | 20 | 20 |  5 |
| 11 | 19 | 19 | 11 |
| 20 |  6 |  6 | 11 |
| 19 | 11 |  5 | 20 |
| 11 | 20 | 11 | 20 |
After Eliminations
| 20 |  0 | 11 |  0 |
|  0 |  0 |  0 |  0 |
|  0 |  0 | 11 |  0 |
|  0 |  0 |  0 |  0 |
|  0 |  0 |  0 |  0 |
| 20 |  6 |  6 |  0 |
| 19 | 11 |  5 |  0 |
| 11 | 20 | 11 | 20 |
After Drops
|  0 |  0 |  0 |  0 |
|  0 |  0 |  0 |  0 |
|  0 |  0 | 11 |  0 |
|  0 |  0 |  0 |  0 |
| 20 |  0 | 11 |  0 |
| 20 |  6 |  6 |  0 |
| 19 | 11 |  5 |  0 |
| 11 | 20 | 11 | 20 |
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning

2015-01-11 Thread WolfRage
Ok, now the code works as expected to drop the non zero values. But I 
think there exist an error in the naming and display of the col and row 
variables at least from with in the GameTile() class, looking into that 
now. All Suggestions Welcome! Thank You All.


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 = list(zip(*self.grid))

def draw(self):
for col in self.grid:
print(end='| ')
for node in col:
print(node, end=' | ')
print()

def draw_by_id(self):
for col in self.grid:
print(end='| ')
for node in col:
print(node.id, 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_nodes0(self):
i = self.rows
# first_zero_row serves as memory for how far to drop non-zero 
values

first_zero_row = None
for col_list in self.transposed_grid:
while True:
# Low is on Top, High is on Bottom
low, high = col_list[i - 2: i]  # Goes Up the Rows
if high.value == 0:
if low.value != 0:
if first_zero_row is None:
high.value = low.value
low.value = 0
first_zero_row = low
else:
first_zero_row.value = low.value
low.value = 0
try:
row = first_zero_row.row
col = first_zero_row.col -1
first_zero_row = self.grid[col][row]
except:
i = self.rows
first_zero_row = None
break
else:
if first_zero_row is None:
first_zero_row = high
i -= 1
if i == 1:
i = self.rows
first_zero_row = None
break

def drop_floating_nodes1(self):
i = 0
for col_list in self.transposed_grid:
while True:
try:
low, h