https://blog.csdn.net/gouqinan/article/details/122411736
在上方的井字棋实现基础上,自己想用wxpython来做gui页面
但是逻辑好像出现问题,求解决
import wx
class Tictactoe(wx.Dialog):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(500,400))
#默认就是x
self.userchoice='x'
panel=wx.Panel(self,wx.ID_ANY)
self.notice = wx.StaticText(panel,wx.ID_ANY,'新游戏')
self.on=wx.TextCtrl(panel,wx.ID_ANY,'',style=wx.TE_PROCESS_ENTER)
self.game = wx.StaticText(panel,wx.ID_ANY,'',size=(300,300))
exitBtn=wx.Button(panel,wx.ID_ANY,'退出')
topSizer=wx.BoxSizer(wx.VERTICAL)
editSizer=wx.BoxSizer(wx.VERTICAL)
btnSizer=wx.BoxSizer(wx.HORIZONTAL)
editSizer.Add(self.notice,0,wx.ALL,5)
editSizer.Add(self.on,0,wx.ALL,5)
editSizer.Add(self.game,0,wx.ALL,5)
btnSizer.Add(exitBtn,0,wx.ALL,5)
topSizer.Add(editSizer,0,wx.ALL|wx.CENTER,5)
topSizer.Add(btnSizer,0,wx.ALL|wx.CENTER,5)
panel.SetSizer(topSizer)
topSizer.Fit(self)
self.Bind(wx.EVT_TEXT_ENTER,self.tic_tac_toe,self.on)
self.Bind(wx.EVT_BUTTON,self.onExit,exitBtn)
self.board = list("012345678")
action=self.userchoice
if action=="x":
self.turn = "player"
self.playerletter = "x"
self.computerletter = "o"
else:
self.turn = "AI"
self.computerletter = "x"
self.playerletter = "o"
#self.notice.SetLabel("{}先走!".format(self.turn))
self.notice.SetLabel("请选择落子位置(0-8):(回车键确认))")
self.game.SetLabel(self.dis_board(self.board))
def onExit(self,e):
wx.MessageBox('退出游戏,本次游戏不记录')
self.Close(True)#关闭顶层框架窗口
def dis_board(self,board):
#显示出棋盘
a="\t{0} | {1} | {2}".format(board[0], board[1], board[2])
b="\t_ | _ | _"
c="\t{0} | {1} | {2}".format(board[3], board[4], board[5])
d="\t_ | _ | _"
e="\t{0} | {1} | {2}".format(board[6], board[7], board[8])
return a+'\n'+b+'\n'+c+'\n'+d+'\n'+e+'\n'
def moves(self,board):
#寻求可落子的位置
moves = []
for i in range(9):
if board[i] in list("012345678"): #遍历了棋盘的位置如果位置为0-8那么这个位置可以落子
moves.append(i)
return moves
def playermove(self,board):
#询问并确定玩家的选择落子位置,无效落子重复询问
move = 9
while move not in self.moves(board):
move = self.on.GetValue()
return move
def computermove(self,board,computerletter,playerletter):
#核心算法:计算AI的落子位置
boardcopy = board.copy()
#规则一:判断如果某位置落子可以获胜,则选择这个位置
for move in self.moves(boardcopy):
boardcopy[move] = computerletter
if self.winner(boardcopy):
return move
boardcopy[move] = str(move)
#规则二:某个位置玩家下一步落子就可以获胜,则选择该位置
for move in self.moves(boardcopy):
boardcopy[move] = playerletter
if self.winner(boardcopy):
return move
boardcopy[move] = str(move)
#规则三:按照中心、角、边的选择空的位置
for move in(4,0,2,6,8,1,3,5,7):
if move in self.moves(board):
return move
def winner(self,board):
#判断所给棋子是否获胜
_to_win = {(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)}
for r in _to_win:
if board[r[0]] == board[r[1]] == board[r[2]]:
return True
return False
def Tie(self,board):
#判断是否平局
for i in list("012345678"):
if i in board:
return False
return True
def tic_tac_toe(self,e):
#井字棋
if self.turn == 'player':
move = int(self.playermove(self.board))
self.board[move] = self.playerletter
if self.winner(self.board):
self.game.SetLabel(self.dis_board(self.board))
self.notice.SetLabel("恭喜玩家获胜!")
else:
self.turn = "AI"
if self.turn == 'AI':
move = int(self.computermove(self.board, self.computerletter, self.playerletter))
self.notice.SetLabel("人工智能AI落子位置:"+str(move))
self.board[move] = self.computerletter
if self.winner(self.board):
self.game.SetLabel(self.dis_board(self.board))
self.notice.SetLabel("人工智能AI获胜!")
else:
self.turn = "player"
if self.Tie(self.board):
self.game.SetLabel(self.dis_board(self.board))
self.notice.SetLabel('平局!')
def main():
app = wx.App()
ex = Tictactoe(parent=None,title='游戏管理')
ex.Show()
app.MainLoop()
if __name__=='__main__':
main()