daanneek
daanneek

Reputation: 53

Winning condition in Connect four game (Python)?

I've been working on a connect four game for a few weeks now (beginning programmer) and im almost done. But the only problem I have is that I can't seem to get the win conditions to work. I honestly have no idea how to fix it so im hoping someone on here can see whats wrong. The upper part until the last comment (#) works fine, it prints the board, gives error msg's at wrong input and drops the pieces where you want. the only thing that still doesn't work is the win conditions. I can't finish a game, it will let me get to 7 in a row and not give a win condition. My code can be found below!

ROW_COUNT = 6                               #CREEEREN VAN HET BORD
COLUMN_COUNT = 7

Board = []
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece):  #PLAATSEN VAN EEN RONDJE
    Board[row][Column] = piece

def is_valid_location(Board, Column):       #CHECKEN OF DE PLEK OP HET BORD LEEG IS
    return Board[-1][Column] == 0

def get_next_open_row(Board, Column):       #CHECKEN OF DE ROW LEEG IS
    for r in range(ROW_COUNT):
        if Board[r][Column]==0:
            return r

gameOver = False
turn = True

while not gameOver:
    if turn: player = 1
    else: player = 2
    UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
    if UserInput.isdigit():
        Column = int(UserInput)
    else:
        print("Your input has to be between 0 and 6!")
        UserInput = input("Player " + str(player) + ", Make your turn(0-6):")

    if Column != 0 and Column != 1 and Column != 2 and Column != 3 and Column != 4 and Column != 5 and Column != 6:
       print("Your input has to be between 0 and 6!")
       UserInput = input("Player " + str(player) + ", Make your turn(0-6):")
    else:
        if is_valid_location(Board, Column):
            row = get_next_open_row(Board, Column)
            drop_piece(Board, row, Column, player)
            turn = not turn
        else: print("Invalid selection")

        for row in reversed(Board):
            print(row)                  # EVERYTHING UNTIL HERE WORKS FINE!

#HORIZONTAAL
for C in range(COLUMN_COUNT - 3):
    for R in range(ROW_COUNT):
        if Board[R][C] == 1 and Board[R][C + 1] == 1 and Board[R][C + 2] == 1 and Board[R][C + 3] == 1:
            print("You've won")
#VERTICAAL
for C in range(COLUMN_COUNT):
    for R in range(ROW_COUNT - 3):
        if Board[R][C] == 1 and Board[R + 1][C] == 1 and Board[R + 2][C] == 1 and Board[R + 3][C] == 1:
            print("You've won")
#DIAGONAAL
for C in range(COLUMN_COUNT - 3):
    for R in range(ROW_COUNT - 3):
        if Board[R][C] == 1 and Board[R + 1][C + 1] == 1 and Board[R + 2][C + 2] == 1 and Board[R + 3][C + 3] == 1:
            print ("You've won")

Upvotes: 0

Views: 2787

Answers (2)

user11964042
user11964042

Reputation:

Just reminding you: You will need one more win condition, for the backwards diagonal.

Heres my solution from a long time ago, see what you can make of it.

 for x in range(len(Board)):
        for y in range(len(Board[x])):
            for turn in ['r', 'b']:
                try:
                    # Sideways line checking
                    if Board[x][y] == turn and Board[x][y+1] == turn and Board[x][y+2] == turn and Board[x][y+3]: pass
                except:# In case of a RangeError, meaning it checks for Board off the edge of the board
                    pass
                else:
                    if Board[x][y] == turn and Board[x][y+1] == turn and Board[x][y+2] == turn and Board[x][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
                try: # Diagonal-right check
                    if Board[x][y] == turn and Board[x+1][y+1] == turn and Board[x+2][y+2] == turn and Board[x+3][y+3]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x+1][y+1] == turn and Board[x+2][y+2] == turn and Board[x+3][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
                try: #Vertical Check
                    if Board[x][y] == turn and Board[x+1][y] == turn and Board[x+2][y] == turn and Board[x+3][y]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x+1][y] == turn and Board[x+2][y] == turn and Board[x+3][y] == turn:
                        winner = turn
                        gamestate = "Win"
                try: # Diagonal-left check
                    if Board[x][y] == turn and Board[x-1][y+1] == turn and Board[x-2][y+2] == turn and Board[x-3][y+3]: pass
                except:
                    pass
                else:
                    if Board[x][y] == turn and Board[x-1][y+1] == turn and Board[x-2][y+2] == turn and Board[x-3][y+3] == turn:
                        winner = turn
                        gamestate = "Win"
    if 'e' not in Board[0] and gamestate != "Win": # Checks for Tie (if there is any emty spaces in the top row after checking for a win)

Upvotes: 1

ahota
ahota

Reputation: 459

Based on your indentation, those checks aren't even being looked at until the while loop ends. Indent all three of the checks so they're inside the while loop. Additionally, your checks need to set gameOver to True so that the game actually ends when you win.

Upvotes: 1

Related Questions