Warum kann C++ nichts addieren, subtrahieren, dividieren und multiplizieren?

2 Antworten

Hier ist der Quell code:

#include <iostream>
#include <string>


using namespace std;


class Var {
public:
	int roundsUntilTheEnemyComes = 3;
	int player_power = 0;
	int enemy_power = 0;
	int rounds = 0;
	bool bIsDead = 0;
	int Money = 0;
	int towers = 0, soldiers = 0, schools = 0, soldierSchools = 0, archerSchools = 0, archers = 0, walls = 0, goldMines = 0;
};
class Voids {
public:
	static void Enemy() {
		Var v;
		if (v.rounds == 0) {
			v.enemy_power = 50;
		} else if (v.rounds <= 5 && v.rounds != 0) {
			v.enemy_power += 90;
		} else if (v.rounds <= 15 && v.rounds >= 5) {
			v.enemy_power += 150;
		} else if (v.rounds <= 50 && v.rounds >= 15) {
			v.enemy_power *= 2;
		} else if (v.rounds <= 75 && v.rounds >= 50) {
			v.enemy_power *= 5;
		} else if (v.rounds > 75) {
			v.enemy_power *= 10;
		} if (v.player_power > v.enemy_power) {
			v.bIsDead = false;
		} else {
			v.bIsDead = true;
		}
	} static void IsDeadCheck() {
		Var v;
		if (v.bIsDead) {
			cout << "\n \n \n!!!You are dead!!!";
		}
	} static void MainGame() {
		int dec;
		Var v;
		cin >> dec;
		if (dec == 1) {
			if (v.Money >= 50) {
				v.towers++;
				v.player_power += 5 * (v.archers / 2) + (v.soldiers / 2);
				v.Money -= 50;
				cout << "You have " + to_string(v.Money) + "G left. Your power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.towers) + "Towers! \n";
			} else {
				cout << "You don't have enough money to buy a tower! \n";
			}
		} else if (dec == 2) {
			if (v.archerSchools > 0) {
				if (v.Money >= 5) {
					v.archers++;
					v.player_power = v.archers * v.archerSchools;
					v.Money -= 5;
					cout << "You have " + to_string(v.Money) + "G left. Your Power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.archers) + "archers! \n";
				} else {
					cout << "You don't have enough money to buy a tower! \n";
				}
			}
		} else if (dec == 3) {
			if (v.Money >= 50) {
				v.archerSchools++;
				v.player_power++;
				v.Money -= 50;
				schools("archer");
				cout << "You have " + to_string(v.Money) + "G left. Your Power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.archerSchools) + "Schools for Archers! \n";
			} else {
				cout << "You don't have enough money to buy a school for archers! \n";
			}
		} else if (dec == 4) {
			if (v.soldierSchools > 0) {
				if (v.Money >= 5) {
					v.soldiers++;
					v.player_power += 5 * v.soldierSchools;
					v.Money -= 5;
					cout << "You have " + to_string(v.Money) + "G left. Your Power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.soldierSchools) + "schools for soldiers! \n";
				} else {
					cout << "You don't have enough money to buy a soldier! \n";
				}
			} else {
				cout << "You can't make any soldiers without a soldier school! \n";
			}
		} else if (dec == 5) {
			if (v.Money >= 50) {
				v.soldiers++;
				v.soldierSchools++;
				v.Money -= 50;
				v.player_power += 10;
				schools("soldier");
				cout << "You have " + to_string(v.Money) + "G left. Your Power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.archerSchools) + "Schools for Archers! \n";
			} else {
				cout << "You don't have enough money to buy a soldier school! \n";
			}
		} else if (dec == 6) {
			if (v.walls > 4) {
				if (v.Money >= 20) {
					v.walls++;
					v.player_power += 4 * (v.archers + v.soldiers);
					v.Money -= 20;
					cout << "You have " + to_string(v.Money) + "G left. You Power now is " + to_string(v.player_power) + ", and you now have " + to_string(v.walls) + "Walls for you castle! \n";
				} else {
					cout << "You don't have enough money to buy a wall. \n";
				}
			}
		} else if (dec == 7) {
			v.goldMines++;
			v.Money += 25;
			cout << "You bought a Goldmine! \n";
			if (v.rounds <= 1) {
				v.player_power += 50;
				v.Money += 50;
				cout << "You now have " + to_string(v.Money) + "G and you Power is " + to_string(v.player_power) + "! \n";
			}
		} else if (dec == 8) {
			if (v.Money >= 60) {
				v.archers++;
				v.soldiers++;
				v.schools++;
				v.player_power++;
				v.Money -= 60;
				schools("all");
			} else {
				cout << "You don't have enough money to buy a school!";
			}
		}
	} static void schools(string typeOfSchool) {
		Var v;
		if (typeOfSchool == "archer") {
			v.archers++;
		} else if (typeOfSchool == "soldier") {
			v.soldiers++;
		} else if (typeOfSchool == "all") {
			v.soldiers++;
			v.archers++;
		}
	}
};


int main() {
	Voids GameVoids;
	Var v;
	v.roundsUntilTheEnemyComes = 3;
	v.Money = 0;
	v.player_power = 0;
	cout << "!!!Game Start!!! \n";
	while (true) {
		if (v.roundsUntilTheEnemyComes != 0) {
			cout << "What do you want to buy? \n";
			cout << "(1) A Tower | (2) An Archer | (3) An Archer School | (4) A Soldier | (5) A Soldier School | (6) A castle Wall | (7) A Goldmine | (8) A School \n";
			GameVoids.MainGame();
			if (v.archerSchools > 0) {
				v.archers += v.archerSchools;
				v.player_power += v.archerSchools;
			} if (v.soldierSchools > 0) {
				v.soldiers += v.soldierSchools;
				v.player_power += v.soldierSchools;
			} if (v.goldMines > 0) {
				v.Money += 25 + (v.goldMines / 2);
			} if (v.schools > 0) {
				GameVoids.schools("all");
				v.player_power += 2 + v.schools + (v.archerSchools / 2) + (v.soldierSchools / 3);
			}
			v.roundsUntilTheEnemyComes--;
		} else {
			GameVoids.Enemy();
			GameVoids.IsDeadCheck();
			if (v.bIsDead) {
				cout << "\n \n \nThe Power of the Enemy was" + v.enemy_power;
				break;
			} else {
				v.rounds++;
			}
		}
	}


	return 0;
}
BossWither 
Fragesteller
 04.04.2023, 16:09

(Hat nicht mehr in die Frage gepasst.)

0
  1. Du solltest ein minimales Codebeispiel, das das Problem verdeutlicht, hochladen - oder wenigstens in deinem Code die interessanten Zeilen highlighten. Sonst dauert es für uns einfach zu lange, den Code nachzuvollziehen.
  2. In jedem Game-Tick erstellst Du in GameVoids::MainGame() eine neue Instanz von Vars, die dann verändert wird. Das ist doch gar nicht die gleiche Instanz wie in deiner Main-Funktion. Das sind zwei völlig unterschiedliche Objekte; entsprechend werden die Änderungen auch nicht auf v in der main() übertragen. Ich würde entweder den ganzen Code (also auch die Game-Loop) in MainGame() setzen und dort einfach sämtliche Spiellogik ablaufen lassen (d.h. main() ruft lediglich GameLoop() auf, macht sonst aber nichts). Oder einfach die Instanz von Vars per Referenz an die Funktion übergeben und dort verwenden, statt in der Funktion jedes Mal ein neues, anderes Objekt zu verwenden.
  3. Abgesehen davon solltest Du Dir noch mal Gedanken darüber machen, wie Du den Game State speicherst und verarbeitest, und wie Du in OOP Klassen sinnvoll verwendest.
BossWither 
Fragesteller
 04.04.2023, 16:46

Danke für die Kritik, ich werde das nächste mal versuchen das zu verbessern, aber dass beantwortet trotzdem nicht meine Frage.

0
W00dp3ckr  04.04.2023, 16:58
@BossWither

Die Kritik ist recht fundamental. Er sagt: aufräumen und dabei lösen sich Deine Probleme.

0
unsignedInt  04.04.2023, 20:25
@BossWither

Doch, mein zweiter Punkt beantwortet deine Frage vollständig. Du erstellst ein Objekt a und ein Objekt b, veränderst dann b und wunderst dich, warum a nicht verändert wurde. Du solltest in die verändernden Funktionen entweder das Zielobjekt als Pointer/Referenz hineinschicken oder dir eine andere, sinnvollere Programmstruktur überlegen, die dir alles vereinfacht. Wie gesagt würde ich lieber sämtliche Logik in die Klasse verschieben, den Gamestate durch die Klasse abbilden und entsprechend die Funktionen non-static machen (d.h. OOP so anwenden, wie es eigentlich vorgesehen ist).

0