Python Forum
Simple Function Problem, please help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Function Problem, please help
#11
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
Reply
#12
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.
Reply
#13
(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.
Reply
#14
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
Reply
#15
(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.
Reply
#16
I'm curious what the fix is. I've been poking at it and can't see wtf is wrong.
Reply
#17
The "nth term" in the function columncheck() is 4c-2, but to replace the space in the string, it's still 4c-1.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 714 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,219 Jun-14-2022, 08:35 PM
Last Post: thesquid
Big Grin question about simple algorithm to my problem jamie_01 1 1,664 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  simple login problem MMOToaster 2 2,398 Feb-25-2020, 09:28 AM
Last Post: MMOToaster
  got SyntaxError while building simple function zarize 2 2,107 Feb-14-2020, 10:51 AM
Last Post: zarize
  Problem with simple 3D Vektor calculation Pythocodras 0 1,703 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Simple statistics with range function Pythonlearner2019 2 2,105 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Simple problem. looking for an efficient way silverchicken24 3 2,324 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Cannot get simple i/o to function. jerryi 10 6,690 Jul-27-2019, 06:22 PM
Last Post: jerryi
  simple string & input problem kungshamji 5 3,641 Jun-23-2019, 03:54 PM
Last Post: kungshamji

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020