简体   繁体   English

我需要Python中的帮助来检查列表的边界

[英]I need help in Python to check boundaries of a list

So I'm trying to write a code that determines where shapes are being placed on a board, and if there are already pieces on the board it will know if they overlap. 因此,我正在尝试编写代码来确定将形状放置在板上的位置,并且如果板上已经存在一些块,它将知道它们是否重叠。

So what I have now is code that takes user input to get the length and with of the board. 因此,我现在所拥有的代码需要用户输入来获得电路板的长度和宽度。

Then assuming the user knows the size of the board it prompts you to input any pieces already on the board. 然后假设用户知道木板的尺寸,它会提示您输入木板上已经存在的任何零件。

So if the board was a 5 x 4 and had an L shape on it, it would look like this: 因此,如果木板是5 x 4并在上面带有L形,则它看起来像这样:

 [1,1,0,0,0,
  1,0,0,0,0,
  1,0,0,0,0,
  0,0,0,0,0]

But you would enter in [1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0] 但是您需要输入[1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0]

Then it asks you to enter in the shape starting at at the default position, which would be the top right corner. 然后,它要求您从默认位置(即右上角)开始输入形状。

When entering the shapes the board would look like this: 输入形状时,面板将如下所示:

 [1, 2, 3, 4, 5,
  6, 7, 8, 9, 10,
  11,12,13,14,15,
  16,17,18,19,20]

So an L shape would have the default coordinates of: 因此,L形的默认坐标为:

[1,2,6,11]

Then it asks where the next available spot is, so if you were going to add an L to the board where an L already exists. 然后它询问下一个可用位置在哪里,因此是否要将L添加到已存在L的板上。 The next available spot would be 3 like so: 下一个可用位置将是3,如下所示:

 [1,1,X,0,0,
  1,0,0,0,0,
  1,0,0,0,0,
  0,0,0,0,0]

So the program outputs the new coordinates of the shape which would be: 因此程序将输出形状的新坐标,该坐标为:

[3,4,8,13]

But what I need help with is error checking for overflow. 但是我需要帮助的是错误检查溢出。 In other words if the piece goes off the board the program will print failed. 换句话说,如果一块不合格,程序将打印失败。

For example: If I want to put an L piece at 5 the coordinates would output [5,6,11,16] and that is not right. 例如:如果我想将L片放在5,则坐标将输出[5,6,11,16],这是不对的。

What it should look like: 它应该是什么样的:

 [0,0,0,0,X,X
  0,0,0,0,X,
  0,0,0,0,X,
  0,0,0,0,0]

But what happens: 但是会发生什么:

 [0,0,0,0,X,
  X,0,0,0,0,
  X,0,0,0,0,
  X,0,0,0,0]

How can I make it print failed if the piece goes off the board? 如果一块脱离板面,如何使打印失败? I already made it so that you can't input negative coordinates and that you can't make a piece go farther than the largest coordinate. 我已经这样做了,所以您不能输入负坐标,也不能使一个片段比最大坐标更远。

Here is my code: 这是我的代码:

user1 = input ('Enter the length of the board: ')
#Length of the board

user2 = input ('Enter the width of the board: ')
#Width of the board

user3 = user1 * user2
#Area of the board

board = input ('Enter in the contenets of the board, as a list (i.e. [0,1,....,0,0]): ')
#Use list to enter in contents of board if any

listA = input ('Enter the shape, in terms of default coordinates (i.e. [1,2,6,11]): ')
#Enter in shape of piece you are placing on board

if listA[-1] > user3:
    print ('Failed!')
    #Piece fails if end goes off board

else:
    if listA[0] < 1:
        print ('Failed!')
        #Piece fails if begining goes off board

    else:
        c = int(input ('Enter the next free slot: '))
        #Enter where the piece fits next

        a = int(listA[0])
        #First coordinate of piece

        b = [((x + c) - a) for x in listA]
        #Finds location of moved piece

        overlap = False
        #Boolean to check for piece overlap

        for y in b:
            if board[y - 1] == 1:
                overlap = True
                #Overlap is true if piece ovelaps
                #another piece already on the board

            else:
                overlap = overlap
                #If no overlapping occurs then continue

        if overlap == True:
            print ('Failed!')
            #Fails if piece overlaps

        else:
            print b
            #prints the coordinates of the moved piece

It's impossible to do with just the list of new coordinates; 仅用新坐标列表是不可能的。 you must also have how these coordinates relate . 您还必须具有这些坐标的关系 Once you do that, you can see if an edge goes over the edge of the board by checking for a transition of pos % WIDTH from 0 to 1 or vice versa. 完成此操作后,通过检查pos % WIDTH01的转换(反之亦然),可以查看边缘是否超过了板的边缘。

I agree with others that this smells like homework. 我同意其他人的看法,这闻起来像作业。 So here's a hint. 所以这是一个提示。 Think about what causes a shape to go off the board. 考虑一下导致形状偏离板面的原因。 The user enters "default" values that you verify; 用户输入您验证的“默认”值; you know they are on the board. 你知道他们是董事会成员。 But then the piece is shifted some number of spaces ( x ) to the right, and some number of spaces ( y ) down. 但是随后,该棋子向右移动了一定数量的空格( x ),并向右移动了一定数量的空格( y )。 If the width of the default shape ( w ) plus x is larger than the width of the board, then you know it has gone off the board to the right. 如果默认形状( w )加上x的宽度大于木板的宽度,则说明它已经离开木板右侧。 And if the height of the default shape ( h ) plus y is larger than the height of the board, then you know it has gone off the board at the bottom. 而且,如果默认形状( h )加上y的高度大于板的高度,那么您知道它已经离开板的底部。

So you have to do three things: determine x and y , determine w and h , and then compare x + w to user2 and y + h to user1 . 因此,您必须做三件事:确定xy ,确定wh ,然后将x + wuser2y + huser1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM