Anfänger nach C++ und versucht einen Dungeon Crawler Anfänger Aufgabe, beim Testen einer der Klassen fand ich, dass das Drucken der 2D-Array funktioniert, aber wenn es bearbeitet und gedruckt wird, es setzt die Originalwerte zurück und druckt sie mit denen initialisiert wurde.Drucken 2D-Array vollständig ignoriert editierte Werte, druckt initialisierte Werte
InitGrid wird zum Initialisieren des Arrays verwendet.
class MAP
{
public:
/*
Difficulty used in number of enemies and traps created.
1-5 Difficulty
| 1 - Too easy | 2 - Easy | 3 - Normal | 4 - Hard | 5 - Insane |
*/
int Difficulty = Hard; //### Temporary, user selection needs implemented ###
int SpawnPos;
int TPosX; //Treasure Postion, used to checkwinstate.
int TPosY; //Treasure Postion, used to checkwinstate.
char Grid[MAPHeight][MAPWidth];
//InitGrid Function
void InitGrid()
{
for (int y = 0; y < 9; y++) //Row loop
{
for (int x = 0; x < 14; x++) //Column loop
{
Grid[y][x] = { '.' };
}
}
}
//PrintMap Function
void PrintMap()
{
for (int y = 0; y < 9; y++) //This loops on the rows.
{
for (int PrintX = 0; PrintX < 14; PrintX++) //This loops on the columns
{
cout << Grid[y][PrintX] << " ";
}
cout << endl;
}
}
//GenerateEnemies Function
void GenerateEnemies()
{
for (int i = 0; i < Difficulty; i++)
{
int TotEnemies = (Difficulty * 1.5);
for (TotEnemies; TotEnemies == 0; TotEnemies--)
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
if (Grid[y][x] == '.')
{
Grid[y][x] = '#';
}
else
{
GenerateEnemies();
}
}
}
}
//GenerateEnemies Function
void GenerateTraps()
{
for (int i = 0; i < Difficulty; i++)
{
int TotTraps = (Difficulty * 1.5);
for (TotTraps; TotTraps == 0; TotTraps--)
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
if (Grid[y][x] == '.')
{
Grid[y][x] = 'T';
}
else
{
GenerateTraps();
}
}
}
}
//GenerateTreasure Function
void GenerateTreasure()
{
int x = rand() % MAPWidth;
int y = rand() % MAPHeight;
/*
Randomly selects spawn location
uses tmp variables to overwrite
that grid location.
*/
if (Grid[y][x] == '.')
{
Grid[y][x] = 'X';
}
else
{
GenerateTreasure();
}
TPosX = x;
TPosY = y;
}
};
//PLAYER CLASS
class PLAYER : public MAP
{
public:
int Health = (Difficulty * 1.5);
int PPosX;
int PPosY;
char Direction; //Use cin to get user input after map is updated, used in switch case to move player.
//GenerateSpawn Function
void GenerateSpawn()
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
/*
Randomly selects spawn location
uses tmp variables to overwrite
that grid location.
*/
Grid[y][x] = 'P';
PPosX = x;
PPosY = y;
}
//AllowMove Function
void AllowMove(int y, int x)
{
if (Grid[y][x] == '.')
{
Grid[y][x] = '@';
}
else if (Grid[y][x] == 'X')
{
//GameWin();
}
else if (Grid[y][x] == '#')
{
//DamagePlayer();
}
else {}
}
//MovePlayer Function
void MovePlayer()
{
switch (Direction)
{
case 'w':
{
int x = PPosX;
int y = (PPosY + 1);
void AllowMove(int y, int x);
}
break;
case 'a':
{
int x = (PPosX - 1);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
case 's':
{
int x = (PPosX);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
case 'd':
{
int x = (PPosX + 1);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
default:
cout << "invalid character, try again." << endl;
Sleep(5000);
//Call function to retry
}
}
};
//######### End #########
//Main Function
int main() {
srand(time(NULL)); //Used to seed rand() values.
SetConsoleTitle(TEXT("Dungeon Crawler"));
//Objects
MAP Map;
PLAYER Player;
Map.InitGrid();
Player.GenerateSpawn();
//Map.GenerateTreasure();
//Map.GenerateEnemies();
//Map.GenerateTraps();
Map.PrintMap();
cout << endl;
}
Allerdings, wenn ich diese laufen, bekomme ich this.
ich das Debuggen in Visual Studio versucht haben, mit Hilfe von Haltepunkten und an einem gewissen Punkt wird es den Rasterwert zu 'P'
gesetzt, aber ich konnte nicht finden, wo Ich bekomme 'mangels eines besseren Begriffs' zurückgesetzt '.
Ist 'Grid' eine globale oder eine Mitgliedsvariable? Es sieht so aus, als ob Sie Member-Funktionen aufrufen, aber der Code, den Sie angezeigt haben, ist freie Funktionen. Es wäre am besten, ein kurzes vollständiges Beispiel zusammenzustellen und den tatsächlichen Code mit dem Problem zu veröffentlichen. –
Nun, Sie rufen 'GenerateSpawn()' aus 'Player' nicht aus' Map' (wenn 'Map' eigentlich eine 'GenerateSpawn()' Memberfunktion hat). Also initialisierst du "Map" und druckst es dann aus. – DimChtz
Behoben, ich habe versucht, "Grid" zu drucken, das bearbeitet wurde, aber es war ein separates 'Grid', das die Spieler geerbt,' Grid' in globale Variablen verschoben und korrigiert haben. – MattSG