如何确定四人游戏的赢家



我是一个相对较新的python初学者,他只是想学习尽可能多的东西,只是摆弄这门语言。我已经开始制作自己的《Connect Four》游戏,除了4个棋子排成一行时的赢家识别问题(游戏邦注:无论是成行、成列还是对角线)外,一切都很正常。在我的赢家(棋盘)功能中,我首先测试了一行的获胜。请参阅下面我的赢家(板)函数:

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(4):
if (board[row][0] == board[row][1] == board[row][2]) == board[row][3] and 
(board[row][0] != " "):
return board[row][0]
for col in range(4):
if (board[0][col] == board[1][col] == board[2][col]) == board[3][col] and 
(board[row][0] != " "):
return board[0][col]
# 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!n")
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!n")
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("n*** GAME OVER ***n")

# Start the game!
main()

然而,每当我运行程序并连续获得4时,它不会说我赢了或其他什么。它只是继续这个程序。希望有人能帮帮我:)

还有一件事。我如何检查行和列的赢家,而不必列出所有可能存在赢家的行和列?有有效的方法吗?

非常感谢!

你的赢家函数很难找到一个赢家,因为它只看左上角的4 × 4正方形。您需要找到一种方法来查看一行中所有的行和列是否等于4。

这个代码部分可以检查行。

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

您可以通过仅检查最近插入的块的列和行来提高性能。

最新更新