该计划应该让英雄们互相争斗,并且显示赢得他们相互战斗并获胜的胜利者和数量。 就像这种格式一样;
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
The thing is, the program would print out like this :
Greek Heroes
Quarter Finals
Ancient Battle! vs Hector : Winner is in 7 rounds.
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
Ancient Battle! Hercules vs Theseus : Winner is Hercules in 3 rounds.
Ancient Battle! Odysseus vs Ajax : Winner is Ajax in 3 rounds.
Ancient Battle! Atalanta vs Hippolyta : Winner is Atalanta in 3 rounds.
Semi Finals
Ancient Battle! Hector vs Hercules : Winner is Hector in 6 rounds.
Ancient Battle! Ajax vs Atalanta : Winner is Ajax in 2 rounds.
Finals
Ancient Battle! Hector vs Ajax : Winner is Hector in 3 rounds.
However, what it should logically print out is :
Greek Heroes
Quarter Finals
Ancient Battle! vs Hector : Winner is in 7 rounds.
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
Ancient Battle! Hercules vs Theseus : Winner is Hercules in 4 rounds.
Ancient Battle! Odysseus vs Ajax : Winner is Ajax in 3 rounds.
Ancient Battle! Atalanta vs Hippolyta : Winner is Atalanta in 4 rounds.
Semi Finals
Ancient Battle! Hector vs Hercules : Winner is Hector in 7 rounds.
Ancient Battle! Ajax vs Atalanta : Winner is Ajax in 2 rounds.
Finals
Ancient Battle! Hector vs Ajax : Winner is Hector in 4 rounds.
我猜它与某些事情有关 - =运算符逻辑,我似乎无法弄明白。有什么帮助吗?
Hero.cpp
#include "Hero.h"
#include <iostream>
#include <cstring>
using namespace std;
namespace sict {
Hero::Hero()
{
m_name[0] = '\0';
m_health = 0;
m_attack = 0;
}
Hero::Hero(char name[], int health, int attack)
{
if (m_name != nullptr || m_name != "") {
strcpy(m_name, name);
}
else {
m_name[0] = '\0';
}
m_attack = attack;
m_health = health;
}
void Hero::operator-=(int attack) {
if (attack > 0) {
m_health -= attack;
}
if (attack > m_health) {
m_health = 0;
}
}
bool Hero::isAlive() const {
if (m_health > 0) {
return true;
}
else {
return false;
}
}
int Hero::attackStrength() const {
if (m_attack == 0) {
return 0;
}
else {
return m_attack;
}
}
ostream& operator<<(ostream& os, const Hero& hero) {
if (hero.m_name == '\0') {
os << "No Hero";
}
else {
os << hero.m_name;
}
return os;
}
const Hero& operator*(const Hero& first, const Hero& second) {
cout << "Ancient Battle! ";
cout << first;
cout << " vs ";
cout << second;
cout << " : ";
Hero A = first;
Hero B = second;
const Hero *winner = nullptr;
int max_rounds = 0;
while (A.isAlive() && B.isAlive() && max_rounds < 200) {
max_rounds++;
A -= B.attackStrength();
B -= A.attackStrength();
}
bool draw;
if (A.isAlive() && B.isAlive()) { draw = true; }
else { draw = false; }
if (draw) {
winner = &first;
}
else if (A.isAlive())
{
winner = &first;
}
else {
winner = &second;
}
cout << "Winner is ";
cout << *winner;
cout << " in " << max_rounds << " rounds. " << endl;
return *winner;
}
}
Hero.h
#ifndef SICT_HERO_H_
#define SICT_HERO_H_
#include <iostream>
namespace sict {
class Hero {
char m_name[41];
int m_health;
int m_attack;
public:
Hero();
Hero(char name[], int health, int attack);
void operator-=(int attack);
bool isAlive() const;
int attackStrength() const;
friend std::ostream& operator<<(std::ostream& os, const Hero& hero);
};
const Hero& operator*(const Hero& first, const Hero& second);
}
#endif
Main.cpp的
#include <iostream>
#include "Hero.h"
using namespace std;
using namespace sict;
int main() {
cout << "Greek Heroes";
Hero moneyhunger("", 40, 4);
Hero hercules("Hercules", 32, 4);
Hero theseus("Theseus", 14, 5);
Hero oddyseus("Odysseus", 15, 3);
Hero ajax("Ajax", 17, 5);
Hero achilles("Achilles", 20, 6);
Hero hector("Hector", 30, 5);
Hero atalanta("Atalanta", 10, 3);
Hero hippolyta("Hippolyta", 10, 2);
cout << endl << "Quarter Finals" << endl;
const Hero& greek_winner0 = moneyhunger * hector;
const Hero& greek_winner1 = achilles * hector;
const Hero& greek_winner2 = hercules * theseus;
const Hero& greek_winner3 = oddyseus * ajax;
const Hero& greek_winner4 = atalanta * hippolyta;
cout << endl << "Semi Finals" << endl;
const Hero& greek_winner_semifinal1 = greek_winner1 * greek_winner2;
const Hero& greek_winner_semifinal2 = greek_winner3 * greek_winner4;
cout << endl << "Finals" << endl;
greek_winner_semifinal1 * greek_winner_semifinal2;
system("pause");
return 0;
}
答案 0 :(得分:0)
运营商需要返回*this
并拥有Hero&
返回类型。您还需要使用else语句,因为您有逻辑错误。
答案 1 :(得分:0)
你有这个功能
void Hero::operator-=(int attack) {
if (attack > 0) {
m_health -= attack;
}
if (attack > m_health) {
m_health = 0;
}
}
这有时会不止一次地减少健康。
假设生命值为15且攻击为10.第一个if语句将生命值降低到5.第二个if语句将再次将其降低为0.
为避免这种情况,你可以这样做:
void Hero::operator-=(int attack) {
if (attack > 0) {
if (attack > m_health) {
m_health = 0;
} else {
m_health -= attack;
}
}
}
只有当攻击至少与剩余生命值一样大时,才会将生命值降低到0。