Игра Connect Four, созданная на основе игры Tic Tac Toe [closed]

Я новичок в Python и почти не имею опыта программирования. В любом случае, как следует из названия, можно ли вывести мой код крестики-нолики на доску 7 x 6 Connect Four? Ниже мой код для крестиков-ноликов:

"""
    Author: Victor Xu

    Date: Jan 12, 2021

    Description: An implementation of the game Tic-Tac-Toe in Python,
    using a nested list, and everything else we've learned this quadmester!
"""

import random


def winner(board):
    """This function accepts the Tic-Tac-Toe 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'."""

    # Check rows for winner
    for row in range(3):
        if (board[row][0] == board[row][1] == board[row][2]) and 
                (board[row][0] != " "):
            return board[row][0]

    # COMPLETE THE REST OF THE FUNCTION CODE BELOW
    for col in range(3):
        if (board[0][col] == board[1][col] == board[2][col]) and 
                (board[0][col] != " "):
            return board[0][col]

    # Check diagonal (top-left to bottom-right) for winner
    if (board[0][0] == board[1][1] == board[2][2]) and 
            (board[0][0] != " "):
        return board[0][0]

    # Check diagonal (bottom-left to top-right) for winner
    if (board[0][2] == board[1][1] == board[2][0]) and 
            (board[0][2] != " "):
        return board[0][2]

    # No winner: return the empty string
    return ""


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

    print("   0   1   2")
    print("0: " + board[0][0] + " | " + board[0][1] + " | " + board[0][2])
    print("  ---+---+---")
    print("1: " + board[1][0] + " | " + board[1][1] + " | " + board[1][2])
    print("  ---+---+---")
    print("2: " + board[2][0] + " | " + board[2][1] + " | " + board[2][2])
    print()


def make_user_move(board):
    """This function accepts the Tic-Tac-Toe 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 2, and that square
    is not already occupied, then it will place an 'X' in that square."""

    valid_move = False
    while not valid_move:
        row = int(input("What row would you like to move to (0-2):"))
        col = int(input("What col would you like to move to (0-2):"))
        if (0 <= row <= 2) and (0 <= col <= 2) and (board[row][col] == " "):
            board[row][col] = 'X'
            valid_move = True
        else:
            print("Sorry, invalid square. Please try again!n")


def make_computer_move(board):
    """This function accepts the Tic-Tac-Toe board as a parameter.
    It will randomly pick row and column values between 0 and 2.
    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:
        row = random.randint(0, 2)
        col = random.randint(0, 2)
        if (0 <= row <= 2) and (0 <= col <= 2) and (board[row][col] == " "):
            board[row][col] = 'O'
            computer_valid_move = True


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

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

    while not winner(ttt_board) and (free_cells > 0):
        display_board(ttt_board)
        if users_turn:
            make_user_move(ttt_board)
            users_turn = not users_turn
        else:
            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("n*** GAME OVER ***n")


# Start the game!
main()

Если возможно, как бы вы изменили размер доски и механику игры? Я много раз пытался модифицировать доску, но безуспешно. Одна из моих попыток:

    Author: Victor Xu

    Date: Jan 20, 2021

    Description: Connect Four, the classic game where a player needs to match 4 horizontally, vertically, or diagonally
    in order to win.
"""

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'."""

    # Check rows for winner

    # 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], " | ", board[1], " | ", board[2], " | ", board[3], " | ", board[4], " | ", board[
        5], " | ", board[6])
    print("---+---+---+---+---+---+---")
    print(" ", " | ", " | ", " | ", " | ", " | ", " | ")
    print("---+---+---+---+---+---+---")
    print(" ", " | ", " | ", " | ", " | ", " | ", " | ")
    print("---+---+---+---+---+---+---")
    print(" ", " | ", " | ", " | ", " | ", " | ", " | ")
    print("---+---+---+---+---+---+---")
    print(" ", " | ", " | ", " | ", " | ", " | ", " | ")
    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 2, and that square
    is not already occupied, then it will place an 'X' in that square."""

    valid_move = False
    while not valid_move:
        row = int(input("What row would you like to move to (0-2):"))
        col = int(input("What col would you like to move to (0-2):"))
        if (0 <= row <= 6) and (0 <= col <= 5) and (board[row][col] == " "):
            board[row][col] = 'X'
            valid_move = True
        else:
            print("Sorry, invalid square. Please try again!n")


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 2.
    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:
        row = random.randint(0, 5)
        col = random.randint(0, 6)
        if (0 <= row <= 6) and (0 <= col <= 5) and (board[row][col] == " "):
            board[row][col] = 'O'
            computer_valid_move = True


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

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

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

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


# Start the game!
main()

Однако я столкнулся с ошибкой, когда они печатают cf_board … Пожалуйста, не обращайте внимания на остальной код, просто посмотрите на плату ASCII. Я еще не дошел

Спасибо!

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *