Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

python - How to Check Diagonal Winner in Connect Four

I'm a relatively new beginner to Python and just started to make a Connect Four game! I've basically completed everything except the diagonal check. I've completed the row and column check, but I have no idea how the diagonal check will work. I don't want to list out all the possibilities of diagonal wins, and I believe that there's a similar way to how I solved for rows and columns. Please see my code below, you'll want to look at the winner(board) function at the top:

import random


def winner(board):
    """This function accepts the Connect Four board as a parameter.
    If there is no winner, the function will return the empty string "".
    If the user has won, it will return 'X', and if the computer has
    won it will return 'O'."""
    for row in range(7):
        count = 0
        last = ''
        for col in range(7):
            row_win = board[row][col]
            if row_win == " ":
                count = 0
                continue
            if row_win == last:
                count += 1
            else:
                count = 1
            if count >= 4:
                return row_win
            last = row_win

    for col in range(7):
        count = 0
        last = ''
        for row in range(7):
            col_win = board[row][col]
            if col_win == " ":
                count = 0
                continue
            if col_win == last:
                count += 1
            else:
                count = 1
            if count >= 4:
                return col_win
            last = col_win

    for diag in range(7):
        i = 0
        last = ""
        for diag in range(7):
            this = board

    # No winner: return the empty string
    return ""


def display_board(board):
    """This function accepts the Connect Four board as a parameter.
    It will print the Connect Four board grid (using ASCII characters)
    and show the positions of any X's and O's.  It also displays
    the column numbers on top of the board to help
    the user figure out the coordinates of their next move.
    This function does not return anything."""

    print("   0   1   2   3   4   5   6")
    print("   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][
        4] + " | " + board[0][5] + " | " + board[0][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][
        4] + " | " + board[1][5] + " | " + board[1][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board[2][
        4] + " | " + board[2][5] + " | " + board[2][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board[3][
        4] + " | " + board[3][5] + " | " + board[3][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board[4][
        4] + " | " + board[4][5] + " | " + board[4][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board[5][
        4] + " | " + board[5][5] + " | " + board[5][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[6][0] + " | " + board[6][1] + " | " + board[6][2] + " | " + board[6][3] + " | " + board[6][
        4] + " | " + board[6][5] + " | " + board[6][6])
    print()


def make_user_move(board):
    """This function accepts the Connect Four board as a parameter.
    It will ask the user for a row and column.  If the row and
    column are each within the range of 0 and 6, and that square
    is not already occupied, then it will place an 'X' in that square."""

    valid_move = False
    while not valid_move:
        try:
            col = int(input("What col would you like to move to (0-6):"))
            if board[0][col] != ' ':
                print("Sorry, that column is full. Please try again!
")
            else:
                for row in range(6, -1, -1):
                    if board[row][col] == ' ' and not valid_move:
                        board[row][col] = 'X'
                        valid_move = True
        except:
            ValueError

    return board


def make_computer_move(board):
    """This function accepts the Connect Four board as a parameter.
    It will randomly pick row and column values between 0 and 6.
    If that square is not already occupied it will place an 'O'
    in that square.  Otherwise, another random row and column
    will be generated."""
    computer_valid_move = False
    while not computer_valid_move:
        col = random.randint(0, 6)
        if board[0][col] != ' ':
            print("Sorry, that column is full. Please try again!
")
        else:
            for row in range(6, -1, -1):
                if board[row][col] == ' ' and not computer_valid_move:
                    board[row][col] = 'O'
                    computer_valid_move = True
    return board


def main():
    """The Main Game Loop:"""

    free_cells = 42
    users_turn = True
    ttt_board = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
                 [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
                 [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
                 [" ", " ", " ", " ", " ", " ", " "]]

    while not winner(ttt_board) and (free_cells > 0):
        display_board(ttt_board)
        if users_turn:
            ttt_board = make_user_move(ttt_board)
            users_turn = not users_turn
        else:
            ttt_board = make_computer_move(ttt_board)
            users_turn = not users_turn
        free_cells -= 1

    display_board(ttt_board)
    if (winner(ttt_board) == 'X'):
        print("Y O U   W O N !")
    elif (winner(ttt_board) == 'O'):
        print("I   W O N !")
    else:
        print("S T A L E M A T E !")
    print("
*** GAME OVER ***
")


# Start the game!
main()

Thank you so much!

question from:https://stackoverflow.com/questions/65861697/how-to-check-diagonal-winner-in-connect-four

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...