【发布时间】:2015-10-21 15:11:18
【问题描述】:
python2中的tac-toe游戏,但程序遇到3-Xs或3-Os时无法停止游戏 这是我的代码:
a=[]
for i in range(0,10):
a.append(" ")
def table():
print " " + a[1]+ " | " +a[2]+ " | " +a[3] +" "
print "-----------------"
print " " + a[4]+ " | " +a[5]+ " | " +a[6] +" "
print "-----------------"
print " " + a[7]+ " | " +a[8]+ " | " +a[9] +" "
table()
#x = None
def play():
global x
for i in range(1,10):
x= None
if (i%2==1):
while ((x>9 or x<0) ):
x=int(raw_input("Player1, it's your turn : "))
while ((a[x]=="O" or a[x]=="X")):
x=int(raw_input("Player1, it's your turn : "))
a[x]='X'
table()
if (i%2==0):
while ((x>9 or x<0) ):
x=int(raw_input("Player2, it's your turn : "))
while ((a[x]=="O" or a[x]=="X")):
x=int(raw_input("Player2, it's your turn : "))
a[x]='O'
table()
if win() or win2():
return
def win():
if (a[1]=="X" and a[2]=="X" and a[3]=="X") or \
(a[4]=="X" and a[5]=="X" and a[6]=="X") or \
(a[7]=="X" and a[8]=="X" and a[9]=="X") or \
(a[1]=="X" and a[4]=="X" and a[7]=="X") or \
(a[2]=="X" and a[5]=="X" and a[8]=="X") or \
(a[3]=="X" and a[6]=="X" and a[9]=="X") or \
(a[1]=="X" and a[5]=="X" and a[9]=="X") or \
(a[3]=="X" and a[5]=="X" and a[7]=="X") : print "PLAYER1 WON, Congratulations !!!"
#######
def win2():
if (a[1]=="O" and a[2]=="O" and a[3]=="O") or \
(a[4]=="O" and a[5]=="O" and a[6]=="O") or \
(a[7]=="O" and a[8]=="O" and a[9]=="O") or \
(a[1]=="O" and a[4]=="O" and a[7]=="O") or \
(a[2]=="O" and a[5]=="O" and a[8]=="O") or \
(a[3]=="O" and a[6]=="O" and a[9]=="O") or \
(a[1]=="O" and a[5]=="O" and a[9]=="O") or \
(a[3]=="O" and a[5]=="O" and a[7]=="O"): print "PLAYER2 WON, Congratulations !!!"
#else: print "Egalitate !!!"
def egal():
if (not win() and not win2() ):
print "Egalitate !"
play()
#win()
#egal()
如你所见,我已经编码:
if win() or win2():
return
不幸的是它在满足任何一个 TRUE 时都不会退出程序。
【问题讨论】:
-
你不会从这些函数中返回任何东西,所以它总是返回
None。 -
是的,但是我需要退出函数 play(),我确实返回是为了退出函数...
-
但是你没有从
win()或win2()返回任何东西。
标签: python algorithm function tic-tac-toe