I have an issue where my declaration of the dynamic array of type Mob, breaks my code. It makes it unable to move the player. Memory issue that I've never encountered... Somehow the invalid way of declaring mob is overwriting player? Or something like that? I am tempted to use a vector however I have no reason to add or remove from the array. It may be easier to manage a complicated dynamic array with a vector?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Mob * Monster;
cout << "Enter # of Monsters you would like to face. 0-4 \n";
cin >> NumOfMobs;
while (NumOfMobs < 0 || NumOfMobs > 4)
{
cout << "Invalid Input, Please input 1-4 \n";
cin >> NumOfMobs;
}
Monster = new Mob[NumOfMobs]();//Declares dynamic array Monster of type mob size UserInput
//I added below in an attempt to make it initialize properly, Not sure this is even correct
for (int i = 0; i < NumOfMobs; i++)
{
Monster[i] = Mob();
}
int main()
{
srand(time(NULL));//Seed random
int TrapCount = 3;
int NumOfMobs;
Game Game;
Player Player;
Mob * Monster;
cout << "Enter # of Monsters you would like to face. 0-4 \n";
cin >> NumOfMobs;
while (NumOfMobs < 0 || NumOfMobs > 4)
{
cout << "Invalid Input, Please input 1-4 \n";
cin >> NumOfMobs;
}
Monster = new Mob[NumOfMobs]();//Declares dynamic array Monster of type mob size UserInput
for (int i = 0; i < NumOfMobs; i++)
{
Monster[i] = Mob();
}
Game.InitializeBoard();
Game.PlaceGameObjects(TrapCount);
Game.PlacePlayer(Player);
//TODO: Ask how many mobs to be placed
Game.PlaceMob(Monster,NumOfMobs);
do
{
Game.PrintBoard();
Game.MovePlayer(Player);
Game.MoveMob(Monster, NumOfMobs);//Allows mob to move into player... Maybe bad.
} while (Game.CheckGameState(Player) == true);
Game.PrintBoard(); //Prints final move where you won/lost
system("pause");
return 0;
}
The Mob functions run well, adds mobs and moves them all independently. Just no more player movement(at all) It wont even detect that the player has tried to move towards a boundary. If I omit all mob related code in main it runs smoothly, back to player being able to move.
Apparently its cin breaking my code. I changed it to a vector and the only issue I have is that if i use cin in any form or fashion, even as an initializer for a number I dont even use in the code. Then I can no longer move player. Im lost.
Solved. Getchar in moveplayer function was grabbing new line so i was skipping the entire move.
Just switched it to cin. Was originally wanting to get input without pressing enter but couldn't get that to work properly.
Didn't realize the newline was stored throughout whole program even using cin.clear()
DONE. (Though with vector I can add a lot more interesting things...)
edit: cin.clear() only resets flags. Needed to flush buffer. By replacing getchar with cin it now flushes the buffer before accepting the new input