【问题标题】:SOS game tkinter issue with checking affirming SOS has been made while staying within the bounds of the listSOS 游戏 tkinter 问题,检查确认 SOS 已在列表范围内进行
【发布时间】:2021-12-19 23:19:27
【问题描述】:

我目前正在开发一个程序,该程序允许您选择游戏板的大小,然后允许玩家选择 S 或 O 以实现 SOS 的目标。我已经用 gui 中的按钮列表完成了这项工作。现在我坚持的问题是检查是否已发出 SOS,同时还要保持在我的列表索引内。代码中的打印语句是为了帮助我确定正在调用什么方法,这是我遇到的另一个问题,具体取决于单击了哪个按钮,只有在调用语句时才能确定。

def checksos(i, j):
 
  for i in range (len(board[j])):
    for j in range(len(board[j])):
     
        
        if board[i][j]["text"]=='S':
            
            if not  i >= len(board[j]): 
                print("h")
                if board[j][i-1]["text"] == 'O' and board[j][i-2]["text"]== 'S':
                this will execute if there is a horizontal sos
                  print("found horizontal sos")

        #check if it will go out of boundaries horizontally and vertically
            if (not i >= len(board[j])-2) and (not j >= len(board)-2):
                    print("d")
                    if board[j+1][i+1]["text"] == 'O' and board[j+2][i+2]["text"] == 'S':
                #this will execute if there is a diagonal sos
                     print("found diagonal sos")

        #check if it will go out of boundaries vertically
            if not j >= len(board)-2:
                        print("v")
                        if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
                #this will execute if there is a vertical sos
                         print("found vertical sos")
            if not j >= len(board)-1:
                        print("v")
                        if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
                #this will execute if there is a vertical sos
                         print("found vertical sos")
            if not j >= len(board):
                        print("v")
                        if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
                #this will execute if there is a vertical sos
                         print("found vertical sos")


        elif board[i][j]["text"]=='O':
           
           if not  i >= len(board[j])-2: 
                print("not out of bounds")
                if board[j][i-1]["text"] == 'S' and board[j][i+1]["text"]== 'S':
                #this will execute if there is a horizontal sos
                 print("found horizontal sos")

        #check if it will go out of boundaries horizontally and vertically
                if (not i >= len(board[j])-2) and (not j >= len(board)-2):
                    if board[j+1][i+1]["text"] == 'S' and board[j-1][i-1]["text"] == 'S':
                #this will execute if there is a diagonal sos
                     print("found diagonal sos")

        #check if it will go out of boundaries vertically
                    if not j >= len(board)-2:
                        if board[j+1][i]["text"] == 'S' and board[j-1][i]["text"] == 'S':
                #this will execute if there is a vertical sos
                         print("found vertical sos")

【问题讨论】:

    标签: python list user-interface tkinter indexing


    【解决方案1】:

    你让这变得比它需要的更难。特别是,我会问为什么你让每个板单元都是一本字典?为什么不将字符本身存储在board[j][i] 中?这样会容易很多。

    我在这里所做的是,对于每一行和每一列,将整行或整列转换为单个字符串,然后在字符串中搜索“SOS”。

    我也对对角线做同样的事情,但它们有点棘手。如果您考虑我在这里使用的 5x5 网格作为示例,前两个字符串从 (2,0)、(1,0)、(0,0)、( 0,1) 和 (0,2)。后两个字符串收集从 (2,0)、(3,0)、(4,0)、(4,1) 和 (4,2) 开始的 NE 到 SW 对角线。那应该都是对角线。

    def checksos(board):
        w = len(board[0])
        h = len(board)
    
        # Check for horizontals.
    
        for row in board:
            s = ''.join([cell['text'] for cell in row])
            if 'SOS' in s:
                print("found horizontal sos")
    
        # Check for verticals.
    
        for col in range(w):
            s = ''.join([board[i][col]['text'] for i in range(h)])
            if 'SOS' in s:
                print("found vertical sos")
    
        # Check for diagonals.  There are N-2 diagonals in each direction;
        # the outermost 2 are too short to hold SOS.
    
        for offset in range(0,w-2):
            # Start from the top and go SE.  If offset is 1, the
            # first string gets 1,0 then 2,1 then 3,2; the other
            # string gets 0,1 then 1,2 then 2,3.
            s1 = []
            s2 = []
            s3 = []
            s4 = []
            for i in range(0,w-offset):
                s1.append( board[i+offset][i]['text'] )
                s2.append( board[i][i+offset]['text'] )
                s3.append( board[i+offset][w-i-1]['text'] )
                s4.append( board[h-i-1][i+offset]['text'] )
            if 'SOS' in ''.join(s1) or 'SOS' in ''.join(s2) or \
               'SOS' in ''.join(s3) or 'SOS' in ''.join(s4):
                print("found diagonal sos")
    
    board = []
    for i in range(5):
        board.append( [{'text':' '} for _ in range(5)] )
    
    board[1][1]['text'] = 'S'
    board[1][2]['text'] = 'O'
    board[1][3]['text'] = 'S'
    
    board[1][4]['text'] = 'S'
    board[2][4]['text'] = 'O'
    board[3][4]['text'] = 'S'
    
    board[2][2]['text'] = 'S'
    board[3][3]['text'] = 'O'
    board[4][4]['text'] = 'S'
    
    checksos(board)
    

    【讨论】:

    • 所以当前存储在 [ i ][ j ] 中的是从另一个实际创建游戏板的按钮功能创建的按钮。该板适用于用户在输入框中输入的任何尺寸。然后,每当玩家选择一个单选按钮来确定玩家是想在棋盘上使用 s 还是 o 时,他们就可以单击棋盘上的任何按钮,它会更改为相应的值。这是在 tkinter 中对我有意义的唯一方式。
    • 我明白了。如果我这样做,我会将董事会与董事会的代表分开。例如,让我编写一个控制台版本和一个 GUI 版本。但我明白你的意思。请注意,我的代码适用于任意方板;我只是凭空挑选了 5 个。
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2019-01-29
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多