感谢您的评论。
我现在解决了原来的问题,如果一名玩家获胜并且选择不继续游戏,游戏就会正确结束。
但是,我现在已经尝试添加游戏是否是平局,这似乎不起作用。
我已经定义了一个函数来检查是否有任何有效的移动,如果没有,它应该返回相应的值来停止游戏并打印到屏幕上有一个平局。
它认识到没有人赢过但没有触发抽签回应。
def start():
choices = [" "," "," "," "," "," "," "," "," "]
initialboard=["1","2","3","4","5","6","7","8","9"]
board(initialboard)
getchoice(choices)
def board(choices):
print("+---+---+---+")
print("+ "+choices[0]+" + "+choices[1]+" + "+choices[2]+" +")
print("+---+---+---+")
print("+ "+choices[3]+" + "+choices[4]+" + "+choices[5]+" +")
print("+---+---+---+")
print("+ "+choices[6]+" + "+choices[7]+" + "+choices[8]+" +")
print("+---+---+---+")
def getchoice(choices):
while checkwin(choices)==0:
userchoice = int(input ("Where would you like to put your X?"))
index = userchoice-1
while choices[index]!=" ":
userchoice = int(input ("That space is taken. Where would you like to put your X?"))
index = userchoice-1
choices[index]="X"
board(choices)
if checkwin(choices)!=1:
userchoice = int(input ("Where would you like to put your O?"))
index = userchoice-1
while choices[index]!=" ":
userchoice = int(input ("That space is taken. Where would you like to put your O?"))
index = userchoice-1
choices[index]="O"
board(choices)
if checkwin(choices)==1:
print("Player 1 wins")
elif checkwin(choices)==2:
print("Player 2 wins")
else:
if checkwin(choices)==3:
print("It's a draw")
again = input("Would you like to play again? Y/N")
again = again.upper()
if again == "Y":
start()
else:
print("Thank you for playing")
def draw(c):
for i in range(0,8):
if c[i] ==" ":
return True
return False
def checkwin (c):
if ((c[0]=="X" and c[1]=="X" and c[2]=="X") or
(c[3]=="X"and c[4]=="X" and c[5] =="X") or
(c[6]=="X"and c[7]=="X" and c[8] =="X") or
(c[0]=="X"and c[3]=="X" and c[6] =="X" )or
(c[1]=="X"and c[4]=="X" and c[7] =="X") or
(c[2]=="X"and c[5]=="X" and c[8] =="X") or
(c[0]=="X"and c[4]=="X" and c[8] =="X") or
(c[6]=="X"and c[4]=="X" and c[2] =="X"))==True:
return 1
elif ((c[0]=="O" and c[1]=="O" and c[2]=="O") or
(c[3]=="O"and c[4]=="O" and c[5] =="O") or
(c[6]=="O"and c[7]=="O" and c[8] =="O") or
(c[0]=="O"and c[3]=="O" and c[6] =="O" )or
(c[1]=="O"and c[4]=="O" and c[7] =="O") or
(c[2]=="O"and c[5]=="O" and c[8] =="O") or
(c[0]=="O"and c[4]=="O" and c[8] =="O") or
(c[6]=="O"and c[4]=="O" and c[2] =="O")) ==True:
return 2
elif draw(c)==False:
print("DRAW")
return 3
else:
return 0
start()