|
-
December 22nd, 2017, 05:20 PM
#1
[RESOLVED] 2D Array, randomisation updates every time the 2D array updates
Basically I am making an adventure game in a 2D array, I would like the character to move around fighting monsters and have that represented in a 2D array. I have the movement done and I have just figured out how to detect if there is another character on the players coordinates. Right now my problem is because I've used rand() to determine where each of my monsters are every time my map updates the monsters also move, I understand why this is happening but the only way I can think of to fix this is to put the randomisation of the monsters coordinates into main or somewhere that only gets called once.
i have tried doing that by making a structure and a class which should only make one randomised coordinate per item in the array.
Code:
struct Character
{
int X;
int Y;
int health;
string name;
Character()
{
health = 99999;
name = "ChangeMe";
}
};
class Monster
{
public:
Character _Character[AMMOUNT_OF_MONSTERS];
string type;
Monster()
{
for (int howManyMonsters = 0; howManyMonsters < AMMOUNT_OF_MONSTERS; howManyMonsters++)
{
_Character[howManyMonsters].X = rand() % GRID_SIZE;//will randomize each monsters X position
_Character[howManyMonsters].Y = rand() % GRID_SIZE;//will randomize each monsters Y position
}
}
};
for (howManyMonsters = 0; howManyMonsters < AMMOUNT_OF_MONSTERS; howManyMonsters++) // to create the ammount of monsters i want
{
if (map[x][y] != grid && map[x][y] != map[playerX][playerY])
{
map[_Monster._Character[howManyMonsters].X][_Monster._Character[howManyMonsters].Y] = 'M';
}
}
Thank you,
Michael Burdge
full code:
the original code for the Monster is the same as the church and survivor, i have changed it just trying out new things.
Code:
// My Game For C++ Neil.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <ctime>
#include <iostream>
#define GRID_SIZE 29 //The Grid size is GRID_SIZE by GRID_SIZE
#define AMMOUNT_OF_MONSTERS 17
#define AMMOUNT_OF_CHURCHES 1//10
#define AMMOUNT_OF_SURVIVORS 1//6
using namespace std;
int NewMap(int playerX, int playerY);
//int ContinueMap(int moveDistance, int direction);
void FightMonster();
void HealAtChurch();
void RecruitSurvivor();
struct Character
{
int X;
int Y;
int health;
string name;
Character()
{
health = 99999;
name = "ChangeMe";
}
};
class Monster
{
public:
Character _Character[AMMOUNT_OF_MONSTERS];
string type;
Monster()
{
for (int howManyMonsters = 0; howManyMonsters < AMMOUNT_OF_MONSTERS; howManyMonsters++)
{
_Character[howManyMonsters].X = rand() % GRID_SIZE;//will randomize each monsters X position
_Character[howManyMonsters].Y = rand() % GRID_SIZE;//will randomize each monsters Y position
}
}
};
int main()
{
int playerX;
int playerY;
bool canContinue;
playerX = GRID_SIZE / 2;
playerY = GRID_SIZE / 2;
int direction;
char directionCHR;
int moveDistance;
time_t t;
srand((unsigned)time(&t));
cout << "The Aim Is To Kill" << endl;
cout << "A = Adventurer: You are the Adventurer" << endl;
cout << "M = Monsters: Fight Monsters" << endl;
cout << "C = Churches: Heal After Fighting Monsters" << endl;
cout << "S = Survivors: Recruit People To Help Fight Monsters" << endl;
NewMap(playerX, playerY);
for (int i = 0; i < 5; i++)
{
do
{
do
{
cout << "Which Direction Would You Like To Move?" << endl << "N for North, E for East, S for South, W for West" << endl;
cin >> directionCHR;
if (directionCHR == 'N' || directionCHR == 'n')
{
direction = 0;
}
else if (directionCHR == 'E' || directionCHR == 'e')
{
direction = 1;
}
else if (directionCHR == 'S' || directionCHR == 's')
{
direction = 2;
}
else if (directionCHR == 'W' || directionCHR == 'w')
{
direction = 3;
}
} while (directionCHR != 'N' && directionCHR != 'n' && directionCHR != 'E' && directionCHR != 'e' && directionCHR != 'S' && directionCHR != 's' && directionCHR != 'W' && directionCHR != 'w');
canContinue = true;
cout << "How Far Would You Like To Move?" << endl;
cin >> moveDistance;
if (direction == 0) // the reason why i did it like this is because originally i had the movement in the function along with the generation of the map.
{
playerX -= moveDistance;
if (playerX < 0)
{
cout << "out of bounds" << endl;
canContinue = false;
playerX += moveDistance;
}
}
else if (direction == 1)
{
playerY += moveDistance;
if (playerY > GRID_SIZE)
{
cout << "out of bounds" << endl;
canContinue = false;
playerY -= moveDistance;
}
}
else if (direction == 2)
{
playerX += moveDistance;
if (playerX > GRID_SIZE)
{
cout << "out of bounds" << endl;
canContinue = false;
playerX -= moveDistance;
}
}
else if (direction == 3)
{
playerY -= moveDistance;
if (playerY < 0)
{
cout << "out of bounds" << endl;
canContinue = false;
playerY += moveDistance;
}
}
} while (!canContinue);
NewMap(playerX, playerY);
}
return 0;
}
int NewMap(int playerX, int playerY)
{
Monster _Monster;
char map[GRID_SIZE][GRID_SIZE];
char grid = 176; //what ive chosen as the map ground, i chose this because it looks good as a background
int x; //Map X
int y; //Map Y
int howManyMonsters; //How many Monsters on the map
int monsterX; //Monsters X
int monsterY; //Monsters Y
//int howManyChurches; //How many Churches on the map
//int churchX; //Church X
//int churchY; //Church Y
//int howManySurvivors; //How many Survivors on the map
//int survivorX; //Survivor X
//int survivorY; //Survivor Y
for (x = 0; x < GRID_SIZE; x++)
{
for (y = 0; y < GRID_SIZE; y++)
{
map[x][y] = grid; //chooses what the map is
map[playerX][playerY] = 'A';
}
}
for (howManyMonsters = 0; howManyMonsters < AMMOUNT_OF_MONSTERS; howManyMonsters++) // to create the ammount of monsters i want
{
if (map[x][y] != grid && map[x][y] != map[playerX][playerY])
{
map[_Monster._Character[howManyMonsters].X][_Monster._Character[howManyMonsters].Y] = 'M';
}
//do // to randomize the coordinates of the monsters so they arent overwriting anything.
//{
// x = rand() % GRID_SIZE;
// y = rand() % GRID_SIZE;
// _Monster._Character[spawnMonsters].monsterX;
// _Monster._Character[spawnMonsters].monsterY;
// map[hereMonsterX][hereMonsterY] = 'M';// place monsters at map[monsterX][monsterY]
//} while (map[x][y] != grid && map[x][y] != map[playerX][playerY]); // this is so the monster wont replace the adventurer with monsters
}
//for (howManyChurches = 0; howManyChurches < AMMOUNT_OF_CHURCHES; howManyChurches++)
//{
// do
// {
// x = rand() % GRID_SIZE;
// y = rand() % GRID_SIZE;
// churchX = rand() % GRID_SIZE; //will randomize each church X position
// churchY = rand() % GRID_SIZE; //will randomize each church Y position
// } while (map[x][y] != grid && map[x][y] != 'M' && map[x][y] != map[playerX][playerY]); // so we dont replace the adventurer or monsters with churches
// map[churchX][churchY] = 'C';
//}
//for (howManySurvivors = 0; howManySurvivors < AMMOUNT_OF_SURVIVORS; howManySurvivors++)
//{
// do
// {
// x = rand() % GRID_SIZE;
// y = rand() % GRID_SIZE;
// survivorX = rand() % GRID_SIZE; //will randomize each survivor X position
// survivorY = rand() % GRID_SIZE; //will randomize each survivor Y position
// } while (map[x][y] != grid && map[x][y] != 'M' && map[x][y] != 'C' && map[x][y] != map[playerX][playerY]); //so we dont replace the adventurer, monsters or churches with survivors
// map[survivorX][survivorY] = 'S';
//}
for (x = 0; x < GRID_SIZE; x++)//this here makes the spacing between each background character, i made a space between them because
{
for (y = 0; y < GRID_SIZE; y++)
{
cout << map[x][y] << " ";
}
cout << endl;
}
for (x = 0; x < GRID_SIZE; x++)
{
for (y = 0; y < GRID_SIZE; y++)
{
if (map[playerX][playerY] == char('M'))
{
FightMonster(); //runs the function to fight a monster.
map[playerX][playerY] = 'A'; // places the Adventurer aka the player in that position
}
else if (map[playerX][playerY] == char('C'))
{
HealAtChurch(); //runs the function to heal at the churche.
map[playerX][playerY] = 'A'; // places the Adventurer aka the player in that position
}
else if (map[playerX][playerY] == char('S'))
{
RecruitSurvivor(); // runs the function to recruit a survivor;
map[playerX][playerY] = 'A'; // places the Adventurer aka the player in that position
}
else
{
map[playerX][playerY] = 'A'; // places the Adventurer aka the player in that position
}
}
}
return 0;
}
void FightMonster()
{
cout << "FIGHT A MONSTER" << endl;
}
void HealAtChurch()
{
cout << "HEAL AT A CHURCH" << endl;
}
void RecruitSurvivor()
{
cout << "RECRUIT A SURVIVOR" << endl;
}
-
December 23rd, 2017, 07:12 AM
#2
Re: 2D Array, randomisation updates every time the 2D array updates
I think it's something like this you're after.
Code:
#include <iostream>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
const int GRID_SIZE = 29; //The Grid size is GRID_SIZE by GRID_SIZE
const int AMMOUNT_OF_MONSTERS = 17;
const int AMMOUNT_OF_CHURCHES = 10;
const int AMMOUNT_OF_SURVIVORS = 6;
const char grid = 176; //what i've chosen as the map ground, i chose this because it looks good as a background
const int nogoes = 5;
int NewMap(int playerX, int playerY, char[GRID_SIZE][GRID_SIZE]);
void FightMonster();
void HealAtChurch();
void RecruitSurvivor();
void draw(char map[GRID_SIZE][GRID_SIZE]);
struct Character
{
int X = 0;
int Y = 0;
int health = 99999;
string name = "ChangeMe";
Character() {}
};
int main()
{
srand((unsigned)time(NULL));
char map[GRID_SIZE][GRID_SIZE];
cout << "The Aim Is To Kill" << endl;
cout << "A = Adventurer: You are the Adventurer" << endl;
cout << "M = Monsters: Fight Monsters" << endl;
cout << "C = Churches: Heal After Fighting Monsters" << endl;
cout << "S = Survivors: Recruit People To Help Fight Monsters" << endl;
for (int i = 0; i < nogoes; ++i) {
bool canContinue = true;
int playerX = GRID_SIZE / 2;
int playerY = GRID_SIZE / 2;
NewMap(playerX, playerY, map);
do {
char directionCHR;
do {
cout << "Which Direction Would You Like To Move?" << endl << "N for North, E for East, S for South, W for West" << endl;
cin >> directionCHR;
directionCHR = (char)toupper(directionCHR);
} while (directionCHR != 'N' && directionCHR != 'E' && directionCHR != 'S' && directionCHR != 'W' && (cout << "Invalid direction" << endl));
cout << "How Far Would You Like To Move?" << endl;
int moveDistance;
cin >> moveDistance;
const int oldx = playerX;
const int oldy = playerY;
map[oldx][oldy] = grid;
canContinue = true;
if ((directionCHR == 'N') && ((playerX -= moveDistance) < 0))
canContinue = false;
else
if ((directionCHR == 'E') && ((playerY += moveDistance) >= GRID_SIZE))
canContinue = false;
else
if ((directionCHR == 'S') && ((playerX += moveDistance) >= GRID_SIZE))
canContinue = false;
else
if ((directionCHR == 'W') && ((playerY -= moveDistance) < 0))
canContinue = false;
if (canContinue) {
if (map[playerX][playerY] == 'M')
FightMonster(); //runs the function to fight a monster.
else
if (map[playerX][playerY] == 'C')
HealAtChurch(); //runs the function to heal at the church.
else
if (map[playerX][playerY] == 'S')
RecruitSurvivor(); // runs the function to recruit a survivor;
map[playerX][playerY] = 'A'; // places the Adventurer aka the player in that position
} else {
cout << "Out of bounds" << endl;
playerX = oldx;
playerY = oldy;
map[oldx][oldy] = 'A';
}
draw(map);
} while (canContinue);
}
return 0;
}
int NewMap(int playerX, int playerY, char map[GRID_SIZE][GRID_SIZE])
{
cout << "Generating a new map...." << endl;
for (int x = 0; x < GRID_SIZE; ++x)
for (int y = 0; y < GRID_SIZE; ++y)
map[x][y] = grid; //chooses what the map is
map[playerX][playerY] = 'A';
for (int howManyMonsters = 0, monx, mony; howManyMonsters < AMMOUNT_OF_MONSTERS; ++howManyMonsters) { // to create the ammount of monsters i want
while (map[monx = (rand() % GRID_SIZE)][mony = (rand() % GRID_SIZE)] != grid);
map[monx][mony] = 'M';
}
for (int howManyChurches = 0, monx, mony; howManyChurches < AMMOUNT_OF_CHURCHES; ++howManyChurches) { // to create the ammount of monsters i want
while (map[monx = (rand() % GRID_SIZE)][mony = (rand() % GRID_SIZE)] != grid);
map[monx][mony] = 'C';
}
for (int howManySurvivors = 0, monx, mony; howManySurvivors < AMMOUNT_OF_SURVIVORS; ++howManySurvivors) { // to create the ammount of monsters i want
while (map[monx = (rand() % GRID_SIZE)][mony = (rand() % GRID_SIZE)] != grid);
map[monx][mony] = 'S';
}
draw(map);
return 0;
}
void draw(char map[GRID_SIZE][GRID_SIZE])
{
for (int x = 0; x < GRID_SIZE; x++) { //this here makes the spacing between each background character, i made a space between them because
for (int y = 0; y < GRID_SIZE; y++)
cout << map[x][y] << " ";
cout << endl;
}
}
void FightMonster()
{
cout << "FIGHT A MONSTER" << endl;
}
void HealAtChurch()
{
cout << "HEAL AT A CHURCH" << endl;
}
void RecruitSurvivor()
{
cout << "RECRUIT A SURVIVOR" << endl;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 23rd, 2017, 07:32 AM
#3
Re: 2D Array, randomisation updates every time the 2D array updates
 Originally Posted by 2kaud
I think it's something like this you're after.
Thank you very much, I appreciate the help in fixing my problem also thank you for tidying my code up a bit, also I just wanted you to know what you have defined as noGoes is what I was just using until I had finished testing the movement and character detection.
thank you,
Michael
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|