ok so theres 2 things i need help with. in my code i have a random dynamic array with sides a minimum of 9 and max of 20. in this array i need to spawn characters randomly (here's where i need help) how can i randomly spawn characters in a random array?

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


class cMakeBoard
{
    private:
        int iSide;
        int iSize;
    
    public:
        int iBoard();
        int iCreate();
        
};

cMakeBoard::iBoard()
{
    srand ( time(NULL) );
    iSide = rand() % 12 + 9;
    return iSide;
}

cMakeBoard::iCreate()
{
    
    int **cSpace = 0;
    cSpace = new int *[iSide]; //create rows
    for( int i = 0 ; i < iSide ; i++ )
    {
        cSpace[i] = new int[iSide]; //create columns
    }
    
    int iXPos;
    int iYPos;
    
    // initialize board
    for(iXPos = 0; iXPos < iSide; iXPos++)
    {
        for(iYPos = 0; iYPos < iSide; iYPos++)
        {
            cSpace[iXPos][iYPos]= 8 ;
        }
    }
    
    // spawn player 0
    srand ( time(NULL) );
    cSpace[8][9] = 0; // then create character object
    // class object(0, '~', 500, 30); // set attributes in constructor
    
    // spawn player 1
    cSpace[2][3] = 1;
    // class object(1, 'M', 100, 60); // set attributes in constructor
    
    // display board
    for(iXPos = 0; iXPos < iSide; iXPos++)
    {
        for(iYPos = 0; iYPos < iSide; iYPos++)
        {
            cout << cSpace[iXPos][iYPos] << " "; 
        }
        cout << endl;
    }

    char cViewer[3][3];
    char cCell;
    int playerX = -1;
    int playerY = -1;
    for(iXPos = 0; iXPos < iSide; iXPos++)
    {
        for(iYPos = 0; iYPos < iSide; iYPos++)
        {
            cCell = cSpace[iXPos][iYPos];
            if(cCell == 0)
            {
                playerX = iXPos;
                playerY = iYPos;
            }
        }
        cout << endl;
    }
    cout << playerX << ", " << playerY << endl;
    int iTemp;
    char cTemp;
    
    iTemp = cSpace[playerX-1][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+1][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;

    iTemp = cSpace[playerX-1][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;    

    iTemp = cSpace[playerX+1][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;

    iTemp = cSpace[playerX-1][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+1][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;
    
    //delete dynamic arrays
    for( int i = 0 ; i < iSide ; i++ )
    {
        delete [] cSpace[i] ;
    }
    delete [] cSpace;
    return 0;
}



int main ()
{
    int iHold;

    cMakeBoard oBoard;

    oBoard.iBoard();
    cin >> iHold;
    oBoard.iCreate();
   
    cin >> iHold;
  return 0;
}

the second problem i have is that my teacher does not want me to "brute force" this area of code how can i simplify this code into a much more "finessed" code.

int iTemp;
    char cTemp;
    
    iTemp = cSpace[playerX-1][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+1][playerY-1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;

    iTemp = cSpace[playerX-1][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;    

    iTemp = cSpace[playerX+1][playerY+0];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;

    iTemp = cSpace[playerX-1][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+0][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;

    iTemp = cSpace[playerX+1][playerY+1];
    if(iTemp == 0)
    {
        cTemp = '~'; // get display char for player
    }
    else
    {
        cTemp = ' ';
    }
    cout << cTemp;
    cout << endl;

how can i randomly spawn characters in a random array?

Pick a row randomly, pick a column randomly, check if something is there already, if so pick another row and another column. I'm sure there are more random ways to do it, but for your assignment that is probably okay.

how can i simplify this code into a much more "finessed" code

See if you can finesse it into a for loop. There's a pattern to the indexes of cSpace. That way you only need the if/else once.

Line 23, you don't need to return anything the information is already within the object. Line 19 and 26, I'd be explicit about the return type as your compiler shouldn't allow implicit int, but since you're using the pre-standard headers it's probably letting you get away with it.

The problem i have with the random spawning is that sometimes where it spawns wont exist on the array and my program will break how can i confine it into the random array that is created?

and thanks i will try the loop

If you have the size of the array, you know how many rows and columns there are. Limit your random number selection to that range, using the same technique as you used on line 22 (but with different settings that correspond to the range you want).

how can i tell it to check the size of the array when its random ? cSpace can be anywhere from 9x9 to 20x20 how can i check the size once its created then tell the random function to give a random row and column. (sorry if youve already answered this im just confused lol)

No it's okay, ask away. iSide is a private member variable. As long as you've called iBoard() before you try to pick the spots for random characters, then you'll be all set. Then all you need is rand() % iSide (same for the row and for the column), as that private member variable will be accessible from any method within the class.

ok cool that works almost every time but occasionally when i hit run it will have an error and it disappears before i can read it any idea why? my guess was that somehow the character was being created outside of the parameters of the array but i honestly dont know why because i cant read the error message

ok i took another screenshot its not when it hits a zero so it must be that it isnt landing in the array thats all i can think of

I couldn't tell much of anything from the hex dump, but try outputting your coordinates that it's picking to the screen. Put a cin.get() after it so the program will pause before moving on. See if there's anything funky there.

and it doesnt show the array when that happens just the error

Right, I meant more like (just as an example)

//generate x and y
//check if they are already taken
cout<<"("<<x<<","<<y<<")"<<endl;
cin.ignore(); //will collect any stray newlines, etc.
cin.get();

This way you get a printout of like

(0,0)
(1,1)
(-13,444) //oops, that's probably it

I think the error is happening after you're picking the coordinates now that I think about it. If you are at 0,0 for example, when you're doing your checking for '~' you need to only check squares that are up and to the right.

i cant tell it doesnt make the grid from what i can see but it pops up so fast i cant see. on line 84 i already had the print out of the coordinates i added the ignore and get but it still closes.

ooooh so like if its at the edge of the array and its trying to check around it?

how could i tell it that theres a wall there or that it shouldn't check when its on a side.

Can you run your program at a DOS prompt instead of within your IDE, that way it shouldn't close at all. You already have a cin statement at the end of main() so I'm not sure why it's closing. Try changing that to a cin.ignore(); cin.get() instead, and if that doesn't work run it from a prompt.

i forgot how to run from a DOS prompt can u explain how please.

We're leapfrogging with our posts hehe. Treat the border of your board as a special case, do the first and last columns, minus the corners, and the first and last rows, minus the corners, the 4 corners, then the middle all at once.

im going to combine both my replies to get on track again lol.

i understand the cmd thing when i hit run through my ide it pops in a cmd like window not sure if thats the same though.


and would i need to make sperate pieces of code for the edges corners and middle or could i somehow through that into one piece if so explain.

No, it's not the same thing, I mean the main Start menu of Windows.

Separate pieces of code.

//corners by themselves

//first row
  for loop over all of the columns
     check only the sides and bottom
//last row
  for loop over all of the columns
     check only the sides and top

//etc.

So what I said initially about being able to use a for loop to clean up your second listing is only applicable to the middle of the board.

for loops kinda confuse me to be honest. ill try to do that though thanks.

I've gotta get going unfortunately, but give it a try and post your problems, someone should be able to give you a hand.

I'm going to keep looking into this - it's interesting.

Just loaded and then ran your prog several times until . . . -1, -1

(if your interested, yes, it happened with the nr 13!!!!)

It's like the Game of Life or any other of those

***********
***********
***********
***********

Along the border they don't all have the same number of nearest neighbors, so those cases have to be handled separately.

You can do it all in one for loop, but you require more if statements, that's why I thought it would be better organized with

//corners

//row0 

//row N-1

//column0

//column N-1

//the middle (all have 4 nearest neighbors)

There may be a neater way of doing it, but I'm not aware of it, so definitely share your findings Dingbats (sounds like I'm insulting you, LOL).

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.