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:
[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]
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:
[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. The next available spot would be 3 like so:
[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.
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
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. If the width of the default shape (w) plusxis larger than the width of the board, then you know it has gone off the board to the right. And if the height of the default shape (h) plusyis larger than the height of the board, then you know it has gone off the board at the bottom.So you have to do three things: determine
xandy, determinewandh, and then comparex + wtouser2andy + htouser1.