Python Forum

Full Version: Simple Function Problem, please help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I would do this completely differently:
import copy

num_rows = 6
num_cols = 5
board = [ ]

def make_board(num_rows, num_cols):
    newrow = [ ]
    for row in range(num_rows):
        for col in range(num_cols):
            newrow.append(0)
        board.append(newrow)
        newrow = [ ]

def drawboard():
    border = ('----' * num_cols) + '-'
    for row in range(num_rows):
        print(border)
        for col in range(num_cols):
            # print(f'row: {row}, col: {col}')
            if board[row][col] == 1:
                cell = '| x '
            else:
                cell = '|   '
            print(cell, end='')
        print('|')
    print(border)

def set_cell(row, col):
    # one based, to make zero based, use:
    # board[row][col] = 1
    board[row-1][col-1] = 1

def clear_cell(row, col):
    # one based, to make zero based, use:
    # board[row][col] = 1
    board[row-1][col-1] = 0

def columncheck(row):
    sums = 0
    for col in range(num_cols):
        sums += board[row-1][col]
    return sums == num_cols

def show_results(row):
    drawboard()
    if columncheck(row):
        print('row {} is full\n'.format(row))
    else:
        print('row {} is not full\n'.format(row))

def test_it():
    make_board(num_rows, num_cols)
    # Set first 4 cells of row 6
    row = 6
    for col in range(4):
        set_cell(row, col+1)
    # Now set last cell
    show_results(row)
    set_cell(row, 5)
    show_results(row)

if __name__ == '__main__':
    test_it()
run results:
Output:
--------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | x | x | x | x | | --------------------- row 6 is not full --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | | | | | | --------------------- | x | x | x | x | x | --------------------- row 6 is full
But what is wrong with my function?
Also, I'm not checking if a row is full, but if the lowest position in a column is full, just like gravity in Connect 4.
(Jan-01-2018, 08:55 PM)ShadowWarrior17 Wrote: [ -> ]But what is wrong with my function?
Also, I'm not checking if a row is full, but if the lowest position in a column is full, just like gravity in Connect 4.

row6="|   |   |   |   |   |   |   |"
#     012345678
 
def columncheck(c):
    x=5
    pos=False
    while pos != True:
        if dboard[x][(4*c)-1]== " ":
            pos=True
        elif dboard[x][(4*c)-1] != " ":
            x-=1
    return x
I copied your code from a previous post and added indices to see how things lined up. According to my cut-and-paste, if c == 1, you're checking dboard[5][3]. Is that were you're putting the 'X'? Probably not. Larz approach is going to be much less error-prone than encoding this into a string.

I assume you're starting your column-counting at 1? That's a bad habit to get into... we programmers like to start with zero.
When the user enters a column, the correct position is taken in the list.

The function that changes the string:
def insertChar(mystring, position, chartoinsert ):
    mystring=mystring[:position-1]+chartoinsert+mystring[position:]
    return mystring
The calling of that function:
dboard[columncheck(column)]=insertChar(dboard[columncheck(column)],(4*column)-1,counter)
Counter is literally the counter(X or O)

I have edited the function, and the 'gravity' now works. Thanks for the help works.

If an admin could delete this post, that would be appreciated! Big Grin LOL
(Jan-02-2018, 10:44 PM)ShadowWarrior17 Wrote: [ -> ]If an admin could delete this post, that would be appreciated! Big Grin LOL

We don't delete posts. The whole point of being an open forum, is so that if someone else has the same (or similar) issue, they can find the answer via a google search. Deleting threads/posts helps no one, while hurting future people.
I'm curious what the fix is. I've been poking at it and can't see wtf is wrong.
The "nth term" in the function columncheck() is 4c-2, but to replace the space in the string, it's still 4c-1.
Pages: 1 2