风610 2022-06-09 13:54 采纳率: 100%
浏览 49
已结题

wxpython问题咨询

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()



  • 写回答

1条回答 默认 最新

  • 懒羊羊的南瓜屋 2022-06-09 15:04
    关注

    img

    img


    代码问题蛮多的,我改了点,没什么时间帮你看了,你自己再继续调试调试吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月10日
  • 已采纳回答 6月9日
  • 创建了问题 6月9日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改