下面是python中的tic tak toe遊戲代碼,可以有人向我展示如何使用重置選項在GUI窗體中顯示它,並顯示誰在最後獲勝。像X贏或O贏?在python編程中的GUI
board =「1 | 2 | 3 \ n ----------- \ n 4 | 5 | 6 \ n ----------- \ n 7 | 8 | 9「
。 checkboard = [1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5, 7]
空間=範圍(1,10)
DEF moveHandler(板,空格,棋盤格,播放器,N):
if player==1:
check="X"
else:
check="O"
while spaces.count(n)==0:
print "\nInvalid Space"
n=playerinput(player)
spaces=spaces.remove(n)
board=board.replace(str(n),check)
for c in range(len(checkboard)):
if checkboard[c]==n:
checkboard[c]=check
status = checkwinner(checkboard,check)
return board,status
DEF checkwinner(棋盤格,檢查): 一個, b,C = 0,1,2
while a<=21:
combo = [checkboard[a],checkboard[b],checkboard[c]]
if combo.count(check) == 3:
status =1
break
else:
status =0
a+=3
b+=3
c+=3
return status
DEF playerinput(播放器): 嘗試: 鍵= INT(的raw_input( '\ n \ nPlayer' + STR(播放器)+ ':請選擇一個空間'))
except ValueError:
print "Invalid Space"
key = playerinput(player)
return key
而真:
player = len(spaces)%2 +1
if player == 1:
player = 2
else:
player =1
print "\n\n" + board
key = playerinput(player)
board,status =moveHandler(board,spaces,checkboard,player,key)
if status == 1:
print '\n\nPlayer ' + str(player) + ' is the winner!!!'
print board
break
elif len(spaces)==0:
print "No more spaces left. Game ends in a TIE!!!"
print board
break
else:
continue