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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:24:46+00:00 2026-06-04T23:24:46+00:00

I have made this method that creates a 2D array for co-ordinates and places

  • 0

I have made this method that creates a 2D array for co-ordinates and places objects (item, treasure, enemy) in random locations. I want to make a new method that takes in the current Player object (so I can keep the current hp, stats, points) and spawn a set of co-ordinates and objects in random locations.

void Game::newGame() {
    srand(time(0));

    int count = 0;
    int row = 0;
    int col = 0;

    Room* newRoom = new Room;
    room = newRoom;
    m_alive = true;

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            room->aRoom[i][j] = NULL;
        }
    }

    //spawn player at 0,0
    room->aRoom[0][0] = new Player("Lemmiwinks", rand()%9+11, rand()%6+7, rand()%6+7);
    m_player = room->aRoom[0][0];
    room->currentRow = 0;
    room->currentCol = 0;
    printMoves();

    //spawn boss
    room->aRoom[rand()%rows][cols-1] = new Boss("Demon Lord", rand()%9 + 10, rand()%7 + 8, rand()%7 + 8);

    //spawn meat cleaver
    count = 0;
    while(count < 1) {
        row = rand()%rows;
        col = rand()%cols;
        if (room->aRoom[row][col] == NULL) {
            room->aRoom[row][col] = new Item("Rusty Shank", 0, 3, 1);
            count++;
        }
    }

    //spawn barrel lid
    count = 0;
    while(count < 1) {
        row = rand()%rows;
        col = rand()%cols;
        if (room->aRoom[row][col] == NULL) {
            room->aRoom[row][col] = new Item("Barrel Lid", 0, 1, 3);
            count++;
        }
    }

    //place potion at random loc
    count = 0;
    while(count < 2) {
        row = rand()%rows;
        col = rand()%cols;
        if (room->aRoom[row][col] == NULL) {
            room->aRoom[row][col] = new Item("Potion", 10, 0, 0);
            count++;
        }
    }   

    //place 5 enemies at random loc
    count = 0;
    while(count < 5) {
        row = rand()%rows;
        col = rand()%cols;
        if (room->aRoom[row][col] == NULL) {
            room->aRoom[row][col] = new Enemy("Demon", rand()%5 + 5, rand()%4 + 4, rand()%4 + 4);
            count++;
        }
    }

    //place 5 treasures at random loc
    count = 0;
    while(count < 5) {
        row = rand()%rows;
        col = rand()%cols;
        if (room->aRoom[row][col] == NULL) {
            room->aRoom[row][col] = new Treasure("Artifact", rand()%5 + 5);
            count++;
        }
    }

    cout << "\n---Stats---" << endl;
    m_player->printEntity();

}

Is there any way I can dupe this method and pass in a player object so I can reuse the same player on a new map?

  • 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-06-04T23:24:47+00:00Added an answer on June 4, 2026 at 11:24 pm

    Not sure if this is what you are after, but should do it:

    void Game::newGame() {
    
    ...
        if (!m_player) {
            m_player = new Player("Lemmiwinks", rand()%9+11, rand()%6+7, rand()%6+7);
        }
        room->aRoom[0][0] = m_player; 
    ...
    }
    
    void Room::clear(Item* player) {
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                if (room->aRoom[i][j] != player) {
                    delete room->aRoom[i][j];
                }
                room->aRoom[i][j] = NULL;
            }
        }
    }
    

    I’d consider maybe using smart pointers for your game objects. Maybe you are already, but I wouldn’t be able to tell from you example code.

    You could always restructure your program and simply have a list(s) of game objects and each contains its coordinates. This would save iterating over a potentially large 2d array in the rest of the game logic. Would also potentially avoid many of the double-dispatches that you’ll be having.

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

Sidebar

Related Questions

Hi have made this function which is made to replicate an error that I
Hey guys and girls. I have a method that creates rain particles, tiny images,
I have made this class to retrive parts of an datatable like a pagination,
I have made this code to open a database(created in SQLite browser) stored in
I have made this centered navigation bar, and I wanted to add a :active
I have made this webpage, you can check it out here http://www.bettingtowin.net/webcam.html I have
OK, I was just fooling around in my spare time and have made this
Hi I have recently made this script to rename files I scan for work
I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>
I have made a flash application and in this flash application I have included

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.