On 2011-12-02 00:30, Charles Karl Becker wrote:
So the main thing I'm looking for are pointers on how I could
optimize/refactor this, and any resources on this and how to 'think'
more in the right way for this type of thing. Also, please let me
know what's good about it :P
def build_line(part):
''' dynamically builds and returns the static lines for use in the board
'''
line = [part for x in range(board_size)]
line = ''.join(line)
line = line[:-1]
return line
# defines the board size
board_size = 5
# these pieces are used in creating the two static lines
part1 = ' |'
part2 = '---|'
# this creates a list of the line #s which will be dynamic (if 0 is the first)
dynamic_list = [x for x in range(board_size)[1:board_size*board_size:4]]
I don't understand what you want to do with "dynamic_list". You are not
using it and if you need a range from 1 to board_size with step 4 just
use the range-function:
dynamic_list = range(1, board_size, 4)
But I've still no clue why you need this list.
# generates the array used for controlling the board spaces/taken locations
master = [[str(x+1), 0] for x in range(board_size**2)]
I guess "master" is later used to save the content of each board cell,
right? But why do you store the cell-number as a string? And what is the
second value (0)?
Wouldn't it be easier to have a structure like
master = ["xxo",
" xo",
"xoo"]
because then you can easily index every cell with
master[row][col]
?
# this section builds the two static lines
line1 = build_line(part1)
line2 = build_line(part2)
As Alan has already mentioned you can build the two lines without using
an extra function. For example "line1" becomes
line1 = " |" * (board_size - 1)
# these are used for loop/flow control
b = 0 # this is used to tell which line needs to be printed
c = 0 # this controls the slicing to tell the board where to start 'temp_row'
# this generates the board on the fly
for row in range(board_size*4-1):
if(b == 0 or b == 2):
print(line1)
elif(b == 3):
print(line2)
elif(b == 1):
# since these rows are dynamic they are called 'temp_row'
# and are reset each time a new one is made
temp_row = ''
for cell in master[c:c+board_size]:
if(int(cell[0])>= 100):
temp_row += '{0}|'.format(cell[0])
if(int(cell[0])>= 10):
temp_row += '{0} |'.format(cell[0])
else:
temp_row += ' {0} |'.format(cell[0])
c += board_size # this is how this variable determines where
to start next time
temp_row = temp_row[:-1] # need to get rid of extra '|' at end of line
print(temp_row)
# this is just some loop/flow control
b += 1
if(b == 4):
b = 0
You can easily simplify your main drawing part:
for row in board_size:
content_line = master[row] # You have to build the line here
print(line1)
print(content_line)
print(line1)
if row < (board_size - 1): print(line2)
So no need for "b" and "c" and and all the if-checks.
Bye, Andreas
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor