Hello.

Here I am again with my Connect 4 project and another board problem.

This is my code:

fielda = []
fieldb = []
rows = int(raw_input('Height: '))
columns = int(raw_input('Width: '))

if rows < 4:
        rows = 4
if columns < 4:
        columns = 4

def print_board():
        for i in range(columns):
                fieldb.append('.')
        for i in range(rows):
                fielda.append(fieldb)
                print ''.join(fieldb)

print_board()
print_board()
print_board()

The first print_board( ) gives out the following (rows = 4, columns = 4):
....
....
....
....

So far, so good.

The second print_board() prints following:
........
........
........
........

The third print_board():
............
............
............
............

Question: Why is the board getting bigger even though nothing has been changed between the first and the last?
Can anybody explain?
Does anybody know how to solve this?(I don't want the board to get bigger)

I don't have a clue what happens there...

Thanks in advance!

Member Avatar for masterofpuppets

well you almost have if working. The problem is that when you're appending items to the list, the list gets bigger every time and unless you reset it to [] it stays the same. so for the second call of print_board() fielda already has "...." and it adds another "...." to it.

here's a simple solution:

fielda = []
fieldb = []
rows = int( raw_input( 'Height: ' ) )
columns = int( raw_input( 'Width: ' ) )
 
if rows < 4:
        rows = 4
if columns < 4:
        columns = 4
 
def print_board():
    global fielda, fieldb

    for i in range( columns ):
        fieldb.append( '.' )
    for i in range(rows):
        fielda.append( fieldb )
        print ''.join( fieldb )
    fielda, fieldb = [], []


print_board()
print_board()
print_board()

>>> 
Height: 5
Width: 5
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
>>>

hope this helps :)

Thanks :)
But umm ... If I always clear the list with every print (because of fielda, fieldb = [], []) I won't be able to insert coins / chips later on. Everytime the board gets printed out (after every turn) the whole board will be cleared - no coin will stay in the board.

I think this project is way too complicated for me ;)

Use the print function I gave you in the previous thread. For filling the board with blank (-) spaces, you need a seperate function (i.e. the one you have above called print board) to initialize the board.

GameBoard = [] #this will be the main game board

def initialize_board(argR, argC): #this function takes rows and columns and spits out a fresh game board
        aRow = []
        theBoard = []
        for i in range(argC):
                aRow.append('.')
        for i in range(argR):
                theBoard.append(aRow)
        return theBoard
 
def print_board(): #this function just prints the main game board
        global GameBoard
        for row in GameBoard:
                for spot in row:
                        print spot,
                print
 
rows = int(raw_input('Height: '))
columns = int(raw_input('Width: '))
 
if rows < 4:
        rows = 4
if columns < 4:
        columns = 4
 
GameBoard = initialize_board(rows, columns)
 
print_board()
print_board()
print_board()

I'm sorry if I changed the variable names around too much. Just ask if anything needs clarification.

Hi,

it's clear for now, thank you :-)

You really should consider using a class for this. Otherwise it gets real convoluted real fast. If you want some assistance, post back.

Hello wooee,

I don't have any experience in classes / OOP at all, thats why I'm avoiding it for now. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.