# Игра 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] == board[row] == board[row]) and
(board[row] != " "):
return board[row]

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

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

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

# 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 + " | " + board + " | " + board)
print("  ---+---+---")
print("1: " + board + " | " + board + " | " + board)
print("  ---+---+---")
print("2: " + board + " | " + board + " | " + board)
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, " | ", board, " | ", board, " | ", board, " | ", board, " | ", board[
5], " | ", board)
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. Я еще не дошел

Спасибо!