#!/usr/bin/python3.4
import tkinter as tk
import time
import os
root = tk.Tk()
label = tk.Label(root)
root.configure(background='#0C142E')
ButtonTable=[]
Turn = 0 # 0(x) 1(O)
XO=["X","O"]
wins = [
'111 000 000', '000 111 000', '000 000 111', # Across
'100 100 100', '010 010 010', '001 001 001', # Down
'100 010 001', '001 010 100', # Diagonal
]
GridArray=["","","","","","","","","",] # Gets replaced with x,o to record progress
def CheckWin():
#something goes here?
def CreateButtonmid(Text,side, Return):
return CreateButton
def ChooseButton(xx,b):
global Turn
b.configure(state = "disabled")
print(xx)
b.config(text=XO[Turn])
GridArray[int(xx)]=XO[Turn]
CheckWon()
if Turn == 0:
Turn = 1
else:
Turn=0
label.configure(text="Turn: "+XO[Turn])
print(GridArray)
def CreateButton(Text,side,Return):
b = tk.Button(side,text=Text)
b.pack(side="left")
b.config(command=lambda: ChooseButton(Return,b))
ButtonTable.append(b)
b.configure(background='#323642',bd=0,fg="white",height=5,width=10)
return b
label.pack(side = "top")
label.config(bg="#0C142E",fg="#fff",text="(STARTING:X)")
topframe = tk.Frame(root)
topframe.pack( side = "top" )
CreateButton("[]",topframe,("0"))
CreateButton("[]",topframe,("1"))
CreateButton("[]",topframe,("2"))
midframe = tk.Frame(root)
midframe.pack( side = "top" )
CreateButton("[]",midframe,("3"))
CreateButton("[]",midframe,("4"))
CreateButton("[]",midframe,("5"))
bottomframe = tk.Frame(root)
bottomframe.pack( side = "top" )
CreateButton("[]",bottomframe,("6"))
CreateButton("[]",bottomframe,("7"))
CreateButton("[]",bottomframe,("8"))
root.mainloop()
基本上这是一款需要两个人玩的多人游戏。什么是最好的方法来检查是否有人赢了。我不知道,我是编程新手,欢迎任何资源来帮助我或我的编码
答案 0 :(得分:2)
>>> get_winner(['X', 'O', 'X',
... 'O', 'O', 'X',
... 'X', 'O', '' ])
'O'
>>> get_winner(['X', 'O', 'X',
... 'O', 'O', 'X',
... 'X', '' , 'O' ])
>>> get_winner(['X', 'O', 'X',
... 'O', 'O', 'X',
... 'X', '' , 'X' ])
'X'
行动中:
{{1}}
答案 1 :(得分:1)
您的数据表示使其比实际需要更难。目前,您将网格存储为9个方块的列表,从左上角开始,按行,跨越然后向下进行。例如,只需访问(第1行,第2列)中的方块,就必须进行算术运算。
更好的表示法可让您直接通过行索引和列索引来处理方块。在Python中,计数从0开始,所以假设行是0,1,2和列。需要考虑两种选择:
一个3元素的行列表,每行为相应列中正方形值的3元素列表:
grid = [["", "", ""], ["", "", ""], ["", "", ""]]
使用此grid
,行i
中的方块,j
列仅为grid[i][j]
。
A dict
:
dgrid = {(i,j): "" for i in range(3) for j in range(3)}
使用此表示形式,行i
,列j
中的正方形仅为dgrid[i,j]
。我将在后面的代码中使用它。
现在,检查网格获胜状态要简单得多。这是一个检查行i
是否赢得player
(等于'X'或'O',让我们说)的函数:
def winning_row(i, player):
'''Return True if all entries in row i are equal to player.
player is either 'X' or 'O'.
'''
return all(dgrid[i,j] == player for j in range(3))
您可以类似地定义函数winning_column(j, player)
。检查对角线有点不同。我们可以使用两个函数winning_main_diagonal(player)
和winning_other_diagonal(player)
。这是后者:
def winning_other_diagonal(player):
return all(dgrid[i,2-i] for i in range(3))
这些函数都采用player
参数,因为您只需在特定player
移动后检查获胜配置。你不需要检查整个网格,看看某些玩家是否赢了,如果是的话。
此功能报告刚刚通过标记方格player
移动的(i,j)
是否刚获胜:
def just_won(i, j, player):
if winning_row(i, player):
return True
if winning_column(j, player):
return True
if i == j and winning_diagonal(player):
return True
if I + j == 2 and winning_other_diagonal(player):
return True
return False