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;