Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:46:58+00:00 2026-05-29T19:46:58+00:00

So I’m trying to write a code that determines where shapes are being placed

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:47:03+00:00Added an answer on May 29, 2026 at 7:47 pm

    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) plus x is 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) plus y is 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 x and y, determine w and h, and then compare x + w to user2 and y + h to user1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.