我正在尝试编写一个简单的两人井字游戏,但是每当我运行它时,它总是说平局。我似乎找不到它的问题,所以我想知道是否有人会帮助我弄清楚为什么它总是这样做。我已经附上了下面的示例代码。
import random
import os
def display_board(board):
print(" | |")
print(" " +board[7] + "|" + board[8] + " |" +board[9])
print(" | |")
print(".........")
print(" | |")
print(" " +board[4] + "|" + board[5] + " |" +board[6])
print(" | |")
print(".........")
print(" | |")
print(" " +board[1] + "|" + board[2] + " |" + board[3] )
print(" | |")
def choose_player():
letter = ""
name1= input("Enter your name Player1: ")
name2= input("Enter your name Player2: ")
while(letter !="X" or letter!="O"):
letter = input(name1 +"" +"What letter do you want to be?: X or O: ").upper()
if letter == "X":
pl={"X":name1,"O":name2}
print("Player1 is X player2 is O")
return pl
elif letter == "O":
pl={"O":name1,"X":name2}
print("player1 is O player2 is X")
return pl
else:
errmsg = ["Choose a valid letter", "really?", "You cant be serious"]
val = random.randint(0,2)
print(errmsg[val])
def position_board(position,board,letter):
'''places input value to a position in list .here name of list is board'''
board[position] = letter
def winner_check(board,letter):
'''checks for winner using the conditions of tic tac toe'''
return (board[1] == board[2] == board[3] == letter or
board[4] == board[5] == board[6] == letter or
board[7] == board[8] == board[9] == letter or
board[1] == board[4] == board[7] == letter or
board[2] == board[5] == board[8] == letter or
board[3] == board[6] == board[9] == letter or
board[1] == board[5] == board[9] == letter or
board[7] == board[5] == board[3] == letter)
def who_starts():
if random.randint(0,1) ==1:
print("player one starts")
return "X"
else:
print("player two starts")
return "O"
def position_check(board,position):
if board[position] == "":
return True
else:
return False
def board_full(board,position_check):
for num in range(1,10):
if position_check(board,num):
return False
else:
return True
def player_input(board,letter):
choice = ""
while(choice not in "1,2,3,4,5,6,7,8,9".split() or not position_check(board,int(choice))):
choice = input("{} Choose a number between 1 & 9: ".format(letter))
return int(choice)
while True:
board = [" "]*10
game_on = True
name = choose_player()
turn= who_starts()
while game_on:
if turn == "X":
display_board(board)
board_status=board_full(board,position_check)
if winner_check(board, "O"):
print("congrats {} is the winner".format(name["0"]))
break
if board_status:
print("you tied")
break
position = player_input(board,name["X"])
position_status = position_check(board,position)
if position_status == True:
position_board(position,board,"X")
turn="O"
else:
display_board(board)
board_status=board_full(board,position_check)
if winner_check(board, "X"):
print("congrats {} is the winner".format(name["X"]))
break
if board_status:
print("you tied")
break
position = player_input(board,name["O"])
position_status = position_check(board,position)
if position_status == True:
position_board(board,position,"O")
turn="X"
该代码应随机选择启动游戏的用户,而不是一如既往地选择X。目前,由于始终显示平局,因此无法进入玩家输入。我希望以后能增加分数,以便玩家跟踪谁赢得了最多的比赛
答案 0 :(得分:0)
该错误可能出在函数position_check
上,您在其中检查板子位置是否等于""
,而您可能想检查它是否仍为空格" "
:
def position_check(board,position):
if board[position] == "":
return True
else:
return False
更改:
if board[position] == "":
return True
收件人:
if board[position] == " ":
return True
告诉我这是否可行?