Connect4 in Python - Pieces doesn't drop into the board

138 Views Asked by At

I'm writing Connect4 in Python. My problem is the function of player_one and player_two don't seem to be working, so the no piece is drop onto the board after player was asked to give input. I also wonder if my code to return the board after player has dropped is correct; I suspect that my present code doesn't return the board updated with a player's piece but being new, I'm not sure what to do.

Please take a look!

def field(field):
    for w in range(14):
        if w % 2 == 0:
            usable_w = int(w/2)
            for h in range(15):
                if h % 2 == 0:
                    usable_h = int(h/2)
                    if h != 14:
                        print(field[usable_h][usable_w], end="")
                    else:
                        print(" ")
                else:
                    print("|", end="")
        else:
            print("_"*13)


PlayField = [[" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "],
             [" ", " ", " ", " ", " ", " ", " "]]
field(PlayField)


def player_one(field):
    MoveColumn = int(input("Enter the column 1 - 7\n"))
    MoveRow = 6
    for row in PlayField:
        if MoveColumn >= 1 and MoveColumn <= 7:
            if PlayField[MoveColumn-1][MoveRow] == " ":
                    PlayField[MoveColumn-1][MoveRow] = "X"
                    break
            MoveRow -= 1
            return field(PlayField)
        else:
            print("Column outside range, please enter valid move")

def player_two(field):
    MoveColumn = int(input("Enter the column 1 - 7\n"))
    MoveRow = 6
    for row in PlayField:
        if MoveColumn >= 1 and MoveColumn <= 7:
            if PlayField[MoveColumn-1][MoveRow] == " ":
                    PlayField[MoveColumn-1][MoveRow] = "O"
                    break
            MoveRow -= 1
            return field(PlayField)
        else:
            print("Column outside range, please enter valid move")

def launch_play():
    while True:
        Player = 1
        print("Player's turn", Player)
        player_one(field)
        player_two(field)

launch_play()

2

There are 2 best solutions below

6
Armali On

Well, your player_... functions contain suitable statements, but in an unsuitable order; and since they operate on the global PlayField, returning it is pointless. Besides that, it's ugly having two nearly identical functions. A rearranged variant where the only difference between player one and two is passed as an argument (instead of the useless field) works as you expect:

def player(xo):
    while (MoveColumn := int(input("Enter the column 1 - 7\n"))) < 1 or \
                                                      MoveColumn > 7:
        print("Column outside range, please enter valid move")
    MoveRow = 6
    for row in PlayField:
        if PlayField[MoveColumn-1][MoveRow] == " ":
            PlayField[MoveColumn-1][MoveRow] = xo
            field(PlayField)
            break
        MoveRow -= 1

In your launch_play loop you can now call

        player('X')
        player('O')

Now it's up to you to complete the program by checking when the game is over.

1
Kentta On

I came up with two solutions (before you modified the codes) to prevent players' turn from changing, but which couldn't work:

def player(xo):
    while MoveColumn := int(input("Enter the column 1 - 7\n")):
    MoveRow = 6
    for row in PlayField:
        if PlayField[MoveColumn-1][MoveRow] == " ":
            PlayField[MoveColumn-1][MoveRow] = xo
            field(PlayField)
            break
            Return True
        MoveRow -= 1
    else:
       print("Column outside range, please enter valid move")
       Return False

def launch_play():
    while True:
        Player = 'X'
        print("Player's turn", Player)
        player('X')
        Player = '0'
        print("Player's turn", Player)
        player('0')

launch_play()

The other solution is to introduce player variables in the player functions (also didn't work) :

 def player(xo):
    while MoveColumn := int(input("Enter the column 1 - 7\n")):
    MoveRow = 6
    Player = 'X'
    for row in PlayField:
        if PlayField[MoveColumn-1][MoveRow] == " ":
            PlayField[MoveColumn-1][MoveRow] = xo
            field(PlayField)
            break
        MoveRow -= 1
        Player = '0'
    else:
       print("Column outside range, please enter valid move")