如何解决TicTacToe游戏,试图找到赢家
我正在尝试在井字游戏中寻找获胜者。
我检查列表中的索引是否相等,但似乎不起作用。这是代码。当索引更改为字母x
或o
时,我要检查它们是否相等。
对此表示抱歉,但由于代码太多,因此我无法上传问题。
board = ['_','_','_']
player1 = ''
player2 = ''
def board_display ():
global board
index_board = ('0','1','2','3','4','5','6','7','8 ' )
#Index Board
print("Position Board \n{} |{} |{} |\n{} |{} |{} |\n{} |{} |{}| \n".format(index_board[0],index_board[1],index_board[2],index_board[3],index_board[4],index_board[5],index_board[6],index_board[7],index_board[8]))
#Print The Board
print("{} |{} |{} |\n{} |{} |{} |\n{} |{} |{} |\n".format(board[0],board[1],board[2],board[3],board[4],board[5],board[6],board[7],board[8]))
def player_symbol_choice ():
global player1
global player2
#Accepatbles Signs
acceptable_values = ['X','O']
#Player Input Sign
player1 = input("Please Choose,X or O \n").upper()
player2 = ''
#Check if User Choose Between X or O
while player1 not in acceptable_values:
player1 = input("Please Choose Only X or O \n").upper()
print("Player 1 is " +player1)
if player1 == 'X':
player2= 'O'
elif player1 == 'O':
player2 = 'X'
print("Player 2 is " +player2)
def gameplay ():
print("Select position for your sign between 0 - 8\nYou can check the position board to be sure that your choice is in the place you want")
#The accepatbles index values
acceptables_positions = [0,1,2,3,4,5,6,7,8]
#Player 1 Turn
Turn1 = int(input("Player 1 \nPlease play your move: "))
#Check if the input values is in the range of 0-8
while Turn1 not in acceptables_positions:
Turn1 = int(input("Player 1 \nPlease play your move,between values 0-8: "))
#Change the index value and replace it with Player 1 sign
board[Turn1] = player1
board_display ()
Turn2 = int(input("Player 2 \nPlease play your move: "))
while Turn2 not in acceptables_positions:
Turn2 = int(input("Player 2 \nPlease play your move,between values 0-8: "))
board[Turn2] = player2
board_display ()
在此功能中,我试图找到赢家。
def find_winner():
if board[0] == board[1] == board[2]:
print("Player 1 Wins" )
elif board[3] == board[4] == board[5]:
print("Player 1 Wins" )
elif board[6] == board[7] == board[8]:
print("Player 1 Wins" )
elif board[0] == board[4] == board[8]:
print("Player 1 Wins" )
board_display()
player_symbol_choice ()
while True:
gameplay()
find_winner()
解决方法
您不需要使用标记。
但这很可能是您搜索的内容。从Udemy课程获得了内容。
@ @积分
提示和技巧:
完整演练:
def win_check(board,mark):
return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
(board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
(board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
(board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
(board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
(board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
(board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
(board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
此功能使用实际的棋盘和标记,因此您只需在每次移动后调用该功能,并检查X或0的人是否赢了。
只需调用checkWin(,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。