首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SOS游戏tkinter问题,检查确认SOS是否已在列表范围内

SOS游戏tkinter问题,检查确认SOS是否已在列表范围内
EN

Stack Overflow用户
提问于 2021-11-06 02:49:27
回答 1查看 77关注 0票数 0

我目前正在开发一个程序,它允许你选择游戏板的大小,然后允许玩家在目标SOS中选择S或O。我已经用gui中的一个按钮列表做到了这一点。现在,我遇到的问题是检查是否已经创建了SOS,同时又保留在我的列表索引中。代码中的print语句是为了帮助我确定调用了什么方法,这是我遇到的另一个问题,即只有在调用语句时,才会根据单击哪个按钮来确定。

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2021-11-06 03:17:44

你让事情变得更难了。特别是,我会问为什么你让每个板子单元都是一个字典?为什么不直接将角色本身存储在board[j][i]中呢?这将使这件事变得容易得多。

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

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69861090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档