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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:53:49+00:00 2026-06-03T17:53:49+00:00

I’m trying to make a basic grid based dungeon crawler. When I build&run and

  • 0

I’m trying to make a basic grid based dungeon crawler. When I build&run and the program prints out the board[7][10] char array only one space on the board has a ‘T’ character. But if I go step by step with the debugger it works perfectly and places 3 ‘T’ characters on the board like I want it to. I have no idea how to fix this problem or what is even causing it. I’m getting no errors from the compiler, it’s just strange output:

main.cpp

#include <iostream>
#include "DungeonBoard.h"
#include "Tokens.h"


int main()
{
    DungeonBoard dungeonCrawl; // create DungeonBoard object
    dungeonCrawl.start(); // call start function
}

DungeonBoard.h

#ifndef DUNGEONBOARD_H
#define DUNGEONBOARD_H

class DungeonBoard
{
    public:
        DungeonBoard(){} // default constructor
        void start(); //gameplay
        void printBoard(); // print out the gameboard
    private:

};

#endif // DUNGEONBOARD_H

DungeonBoard.cpp

#include <iostream>
#include "DungeonBoard.h"
#include "Tokens.h"

const int ROW = 7;
const int COLUMN = 10;
char board[ROW][COLUMN]= {{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}, {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}};
bool occupied[ROW][COLUMN] = {{false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false}, {false, false, false, false, false, false, false, false, false, false}};

Tokens hero(0, 0); //creates user starting location at top left corner
Tokens treasure(6, 9); // creates treasure location at bottom left corner
Tokens trap1, trap2, trap3;

void DungeonBoard::start(){
    char userDirection; // user input
    bool gameOn = true; // sentinel value for while loop

    std::cout << "DUNGEON CRAWL 1.0\n";
    std::cout << "------------------\n\n";
    std::cout << "H is the hero, T are traps, X is the treasure and .'s are open spaces.\n";
    std::cout << "The goal is to move around the gameboard and reach the treasure while avoiding \ndtraps.\n\n";
    std::cout << "CONTROLS: W - UP, S - DOWN, A - LEFT, D - RIGHT.\n\n";
    std::cout << "Enjoy! :)\n\n";

    board[hero.get_row()][hero.get_col()] = 'H'; // set user starting position
    occupied[hero.get_row()][hero.get_col()] = true; // make space used
    board[treasure.get_row()][treasure.get_col()] = 'X'; // set treasure position
    occupied[treasure.get_row()][treasure.get_col()] = true; // make space used

    trap1.trap_loc(); // randomize location of trap 1
    while(occupied[trap1.get_row()][trap1.get_col()] == true){ //check to make sure space is not occupied
        trap1.trap_loc(); //if occupied, re-randomize
    }
    trap2.trap_loc(); //randomize location of trap 2
    while(occupied[trap2.get_row()][trap2.get_col()] == true){ //occupy check
        trap2.trap_loc(); // re-randomize
    }
    trap3.trap_loc(); //randomize location of trap 3
    while(occupied[trap3.get_row()][trap3.get_col()] == true){ //occupy check
        trap3.trap_loc(); //re-randomize
    }

    //set board spaces for traps to T and mark that space as used

    board[trap1.get_row()][trap1.get_col()] = 'T';
    occupied[trap1.get_row()][trap1.get_col()] = true;
    board[trap2.get_row()][trap2.get_col()] = 'T';
    occupied[trap2.get_row()][trap2.get_col()] = true;
    board[trap3.get_row()][trap3.get_col()] = 'T';
    occupied[trap3.get_row()][trap3.get_col()] = true;

    printBoard();
}

void DungeonBoard::printBoard(){
    for(int i = 0; i<7; i++){
        for(int r = 0; r<10; r++){
            if(r == 0)
                std::cout << "  ";
            std::cout << board[i][r] << " ";
        }
        std::cout << "\n\n";
    }
}

Tokens.h

#ifndef TOKENS_H
#define TOKENS_H


class Tokens
{
    public:
        Tokens(){} //default constructor
        Tokens(int row, int col); //constructor with coordinates already known
        void set_pos(int new_row, int new_col){ rowLoc = new_row; colLoc = new_col;} // set new row and col values
        int get_row(){return rowLoc;} //row getter
        int get_col(){return colLoc;} //col getter
        void trap_loc(); // randomize trap location
        int rand0toN1(int n); //psuedo random number generator
    private:
        int rowLoc, colLoc; //tokens location by row and column
};

#endif // TOKENS_H

Tokens.cpp

#include <ctime>
#include <cstdlib>
#include "Tokens.h"
#include "DungeonBoard.h"

Tokens::Tokens(int row, int col){ //sets the position of the token
    set_pos(row, col);
}


void Tokens::trap_loc(){ //gets random row and col for trap
    int trapRow = rand0toN1(6);
    int trapCol = rand0toN1(9);
    set_pos(trapRow, trapCol); // sets position of trap token to the random numbers created
}

int Tokens::rand0toN1(int n){ //psuedo-random num generator
    srand(time(NULL)); // set seed for randomization
    return rand()%n;
}
  • 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-03T17:54:01+00:00Added an answer on June 3, 2026 at 5:54 pm

    The problem is that you keep re-initialising srand with the same seed when you use it inside Tokens::rand0toN1. This is because time(NULL) has seconds resolution.

    By stepping through the code, you’re causing srand to be initialised with different values, hence the expected result.

    srand doesn’t need be called before every use of rand, it is only needed to initialise it, so if you move srand(time(NULL)); to somewhere like the start of DungeonBoard::start() (basically ensure it’s called once before the first use of rand), your program should work as expected.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.