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 6607487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:30:31+00:00 2026-05-25T19:30:31+00:00

I am making a Battleships-like game and need to place a battleship objects into

  • 0

I am making a Battleships-like game and need to place a battleship objects into a battlemap array randomly. Except when I do this, the ships are never placed in the bottom right quadrant (but they are successfully placed in the other 3 quadrants), and I don’t know why. Basically, I get a random integer between 0 and the length and height of the map and a random direction, then check if the ship will fit there, if it can, place it on the map. But it never places them in the bottom right.

void BattleMap::placeRandomly(BattleShip& ship) {
bool correct = true; 
int x_start,y_start,dir;
// size_x, size_y denote the length and height of the array respectively
int length = ship.getLength();      
do{
    correct = true; 
    x_start = abs(rand()%(size_x-length));
    if(x_start+length > size_x) x_start -= length;  
    y_start = abs(rand()%(size_y-length));
    if(y_start+length > size_y) y_start -= length;
    dir = rand()%2; // 0 for vertical, 1 for horizontal;
    for ( int i = 0;  i < length;i++) {
        switch(dir){ // Check if there is already a ship in the candidate squares
          case 0:
            if(this->at(x_start,y_start+i)){
              correct = false;
            }
            break;
          case 1:
            if(this->at(x_start+i,y_start)){
              correct = false;
            }
            break;
        }
      }
   }while(!correct);
   // Place the ships into the array
   ....

}

The at() function is this:

BattleShip*& BattleMap::at(int x, int y){
    if(x > size_x || y > size_y)return 0; 
// error: invalid initialization of non-const reference of type 'BattleShip*&' from a temporary of type 'int'
    return board[x*size_y +y];
}
  • 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-25T19:30:32+00:00Added an answer on May 25, 2026 at 7:30 pm

    You are trying too hard to keep the ship from going off the side. Just allow x_start and y_start to be anywhere:

    x_start = rand()%size_x;
    y_start = rand()%size_y;
    

    And let your at() function return true if it goes off the side:

    bool BattleMap::at(int x,int y) const
    {
      if (x>=size_x || y>=size_y) return true;
      // regular check for a ship at x,y here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a battleships game, so when I pass something such as A10 into
I am making a battleship game for a project. While I have completed the
I am making a battleships game to play in console - How do you
Making game of life I need to a have a grid that is 30x20
Making a 2D OpenGL game. When rendering a frame I need to first draw
Im making a game, when my mainCharacter in this game dies, or when he
Making an argument parser. I want to split a string into an array where
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
Making the transition from Vim to gVim, I would like to disable all toolbars
Making a ternary logic table, and I would like to make my own function

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.