Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation checking
#21
I am stuck in a loop where I cannot move onto player 2 selection. (im in the early stage and I am only onto the validation checking so far):

import numpy as np
turn = 0
game_over = False

def board_create(column, row):
    board = np.zeros((row,column))
    return board

def is_valid_location(column_choice, board, bottom_row):
    if board[bottom_row][column] == 0:
        return False
    else:
        return True

row = 5   
#row = int(input("How many rows: "))
column = int(input("How many columns: "))
board = board_create(column, row)

while not game_over:
    if turn == 0:
        invalid = True
        bottom_row = 0
        while invalid == True:
            column = column - 1
            column = str(column)
            column_choice = int(input("Player 1 make your selection (0-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
            column = column + 1
    else:
        while invalid == True:
            column = column - 1
            column = str(column)
            column_choice = int(input("Player 2 make your selection (0-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
            column = column + 1
    turn = turn + 1
    turn = turn % 2
Would be thankful for any help given
Reply
#22
You are returning a tuple from is_valid_location() and comparing to a Boolean (True). Debug this by putting a print(invalid) after line 25 and correct function call and function until you get the behavior you want.

@perfringo and anyone else not familiar with the game - sold by Hasbro as Connect 4. Vertical board with 4 channels, players take turns dropping disks (checkers) down the channels. Disc automatically falls to the lowest available position. 4 in a row either across, vertical, or diagonal wins. Tic-tac-toe made non-trivial.
Reply
#23
(Aug-16-2019, 11:46 AM)jefsummers Wrote: @perfringo and anyone else not familiar with the game - sold by Hasbro as Connect 4. Vertical board with 4 channels, players take turns dropping disks (checkers) down the channels. Disc automatically falls to the lowest available position. 4 in a row either across, vertical, or diagonal wins. Tic-tac-toe made non-trivial.

Thank you jefsummers, it makes things clearer. I have played this game (but not under this name nor from Harsbo).

So far this code has been about validation and there is nothing about bread-and-butter of this assingment - how to keep track of moves and determine winner. There is loooong way to go.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#24
Don't test where invalid == True, just test where invalid:.

When you return three values using tuple syntax, you need to assign three values using tuple syntax. Take this function:

def foo():
    return True, 801, 'a duck'
If I do invalid = foo(), then invalid is equal to (True, 801, 'a duck'). What should be done is invalid, number, floats = foo(). Then invalid is True, number is 801, and floats is 'a duck'.

@ perfringo: I think by four in a row he means something like Connect Four. You drop pieces into slots and they stack on top of each other. You try to get four pieces in a row, orthogonally or diagonally.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#25
I am completely lost with this tuple business, can someone please emend this for me and repost it back as I am overwhelmed and confused:

import numpy as np
turn = 0
game_over = False

def board_create(column, row):
    board = np.zeros((row,column))
    return board

def is_valid_location(column_choice, board, bottom_row):
    if board[bottom_row][column] == 0:
        return False
    else:
        return True

row = 5   
#row = int(input("How many rows: "))
column = int(input("How many columns: "))
board = board_create(column, row)

while not game_over:
    if turn == 0:
        invalid = True
        bottom_row = 0
        while invalid:
            print(invalid)
            column = column - 1
            column = str(column)
            column_choice = int(input("Player 1 make your selection (0-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
            column = column + 1
    else:
        while invalid == True:
            print(invalid)
            column = column - 1
            column = str(column)
            column_choice = int(input("Player 2 make your selection (0-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, board, bottom_row)
            column = column + 1
    turn = turn + 1
    turn = turn % 2
Thankyou!
Reply
#26
Simplify. What are the criteria for a valid move? Basically you need to make sure the column_choice is within the range of columns and that the column is not full. board is already visible to the function (see line 18) and I know there is discussion on this, but if you create board in this manner you probably do not want to then pass it as an argument into the function, as it may not do what you think. See:
>>> def change_25_to_6(twenty_five) :
	twenty_five = 6
	print(twenty_five)
	return twenty_five

>>> twenty_five = 25
>>> print(twenty_five)
25
>>> print (change_25_to_6(twenty_five))
6
6
>>> print (twenty_five)
25
>>> 
In this case, we changed twenty_five to have the value 6 and printed it both inside and outside the function (it returns twenty-five, value 6, which is then printed from the function call. Yet, when you print twenty-five after that, it has the original unchanged value. The value inside the function is different from the value outside the function though they have the same name. Avoid that - it's crazy making! At your level of understanding, better to not pass board into the functions since you are really wanting the line 18 board to be acted upon.
Also, your choice of variable names makes it really really likely that you will make a mistake - for example, column is the number of columns. But, in your while loop from 24-31 you decrement the number of loops then increment it again to make up for that mistake.

To see whether a move is valid, the only thing you need to pass to the function is the column_choice. Use the board to figure out how many pieces are already in the column, and use number_of_columns as the variable for the number of columns (and don't change it) to check the limits.
Reply


Forum Jump:

User Panel Messages

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