这是我第一次尝试用C++编写程序,我想知道我的代码是好的还是应该改进的。下面是代码。任何帮助调试将不胜感激。有些事情不像预期的那样起作用,比如战斗。当他的健康降到零时,他的咕噜声还会继续发作。我只想确保我的代码是好的。
#include <iostream>
using namespace std;
int main()
{
cout <<"\tIt is the END OF TIME AND SPACE!\n";
cout <<"\tA Darkness looms in the Horizon\n";
cout <<"\tA Ahead of you is a bleak, dark wasteland\n";
char userName[100];
cout <<"\nState your name: ";
cin >>userName;
cout <<userName<<" Journey forth, nothing remains!\n\n";
cout <<"Who are you?: \n";
cout <<"1 - Mage\n";
cout <<"2 - Warrior\n";
cout <<"3 - Ninja\n";
int pickRace;
cout <<"Pick your race: ";
cin >>pickRace;
int Attack;
int Defence;
int Magic;
int Speed;
int HP;
int x;
int Reason;
int What;
int Difficulty;
int Health;
switch (pickRace)
{
case 1:
cout <<"You picked the Warrior race.\n";
Attack=107;
Defence=122;
Magic=74;
Speed=64;
HP=88;
Health=88;
x=0;
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
break;
case 2:
cout <<"You picked the Mage race.\n";
Attack=69;
Defence=72;
Magic=114;
Speed=104;
HP=75;
Health=75;
x=1;
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
break;
case 3:
cout <<" You picked the Ninja race.\n";
Attack=95;
Defence=67;
Magic=103;
Speed=122;
HP=72;
Health=72;
x=0;
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
break;
default:
cout <<"You IDIOT, all is lost\n";
return main();
}
int difficulty;
cout <<"\nPick your level difficulty: \n";
cout <<"1 - Casual\n";
Difficulty=1;
cout <<"2 - Medium\n";
Difficulty=2;
cout <<"3 - Hardcore\n";
Difficulty=3;
cout <<"Pick your level difficulty: ";
cin >>difficulty;
switch (difficulty)
{
case 1:
cout <<"You picked Casual.\n\n";
difficulty=1;
break;
case 2:
cout <<"You picked Medium.\n\n";
difficulty=2;
break;
case 3:
cout <<"You picked Hardcore.\n\n";
difficulty=3;
break;
default:
cout <<"Error - Wow, aren't you sooo smart?.\n";
return main();
}
{
cout <<"\tTime and Space are irrelevant!\n";
cout <<"\tA lone person journeys through the end of time\n";
cout <<userName<<" Give up now!\n\n";}
cout <<"\nWHY ARE YOU HERE?: \n";
cout <<"1 - I Don't Know\n";
cout <<"2 - Help ME\n";
cout <<"3 - Where am I?\n";
cout <<"Why are you here?: ";
cin >>Reason;
switch (Reason)
{
case 1:
cout <<"You Started walking North.\n\n";
break;
case 2:
cout <<"You have lost all sainity.\n\n";
break;
case 3:
cout <<"You are nowhere to be found.\n\n";
break;
default:
cout <<"YOU WILL DIE.\n";
}
cout <<"\nWHAT DO YOU DO?: \n";
cout <<"1 - Run\n";
cout <<"2 - Fight\n";
cout <<"3 - Look\n";
cout <<"What do you do?: ";
int a;
cin >>a;
switch (a)
{
case 1:
cout <<"What are you running from?.\n\n";
break;
case 2:
cout <<"You try to attack, but realise there is nothing to attack.\n\n";
break;
case 3:
cout <<"Nothing.exe.\n\n";
break;
default:
cout <<"ƃuᴉɥʇoN.\n";
}
cout <<"\t An Enemy appears!\n";
cout <<"\t He is lost in Time\n";
cout <<"\t The enemy confuses you, your eyes don't understand what is happening;\n";
int EnemyAttack;
int EnemyDefence;
int EnemyMagic;
int EnemySpeed;
int EnemyDamage;
int EnemyHP;
int EnemyFire;
int A;
EnemyHP=HP/2*(difficulty/2);
EnemyDamage=Attack-EnemyDefence;
EnemyFire=Magic-EnemyDefence;
EnemySpeed=Speed/2*(difficulty/2);
EnemyMagic=Magic/2*(difficulty/2);
EnemyDefence=Defence/2*(difficulty/2);
EnemyAttack=Attack/2*(difficulty/2);
cout <<"\t The enemy has\n" << EnemyHP <<" Health remaining\n";
do
{cout <<"\nWHAT DO YOU DO?: \n";
cout <<"1 - Fight\n";
cout <<"2 - Magic\n";
cout <<"3 - Examine\n";
cout <<"4 - Run\n";
cout <<"What do you do?: ";
cin >>What;
switch (What)
{
case 1:
cout <<"You flail around until you hit something... Sucess\n";
cout <<" You deal " << EnemyDamage<< " Damage\n";
cout <<"The grunt has" <<EnemyHP-EnemyDamage<< " Health Left";
break;
case 2:
cout <<"You try to conjure up a spell.\n";
if (x==1)
{cout <<"You set the foe on fire\n";
cout <<"You deal" <<EnemyFire<< " Damage\n";
cout <<"The grunt has" << EnemyHP-EnemyFire << " Health Left";
}
else if (x==0)
{cout <<"You fail at summoning a spell \n";
cout <<"JUST LIKE YOU FAIL AT LIFE";}
if (EnemyHP > 0);
{cout <<"The grunt attacks you ";
cout <<"The grunt deals " <<HP-(EnemyAttack-Defence) << "Damage";
cout <<"You have"<< HP<< "Health Remaining";
HP=HP-EnemyDamage;}
if (HP<=0)
{cout<<"Game OVER\n";
cout<<"The world fades into obsceruity\n";
cout<<"You open your eyes slowly\n";
cout<<"A small voice calls out\n";}
else if (HP>0){cout<<"You find the strength to keep fighting";}
if (EnemyHP <=0)
{ cout <<"The enemy fades away";
cout <<"You win";
cout <<"level a deniag uoY";
cout <<"Your attack is now" <<Attack-5<< "points ";
Attack=Attack-5;
cout <<"A new foe materializes in place of the grunt";
cout <<"IT IS A SPAWN OF HELLFIRE AND BRIMESTONE";
HP=Health;
goto RealBattle;}
case 3:
cout <<"The Grunt seems to be confused. He has " << EnemyHP <<" Health remaining \n";
if (EnemyHP > 0);
{cout <<"The grunt attacks you ";
cout <<"The grunt deals " <<HP-(EnemyAttack-Defence) << "Damage";
cout <<"You have"<< HP<< "Health Remaining";}
HP=HP-EnemyDamage;
if (HP<=0)
{cout<<"Game OVER\n";
cout<<"The world fades into obsceruity\n";
cout<<"You open your eyes slowly\n";
cout<<"A small voice calls out\n";}
else if (HP>0){cout<<"You find the strength to keep fighting";}
break;
case 4:
cout<<"You escaped successfully \n";
default:
cout <<"What are you trying to prove'.\n";
break;
cin >>A;
return main();
break;
} }while(HP > 0 && EnemyHP > 0);
RealBattle:do{ cout <<"Nothing remains all is lost, there is nothing programed here";}while(HP>0&&EnemyHP>0);
system("PAUSE");
return 0;}发布于 2016-06-16 14:33:20
依我看,这是一份很好的高中家庭作业。
如果你想更进一步:
无论如何,如果您在网络上搜索小型的、类似的软件(例如,请参阅github),并查看它们是如何工作的,这将是非常有用的。
一些(未经检验的)例子:
1.
把这些东西收集到一个“玩家”类中:
class Player {
public:
int attack;
int defence;
int magic;
int speed;
int hp;
int difficulty;
int health;
Player(int attack, int defence, int magic, int speed, int hp, int reason, int health) {
this->attack = attack;
this->defence = defence;
this->magic = magic;
this->speed = speed;
this->hp = hp;
this->reason = reason;
this->health = health;
}
void showStat() {
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
}
};使用它,您可以只编写一次播放机统计数据的打印代码。
而且,这一点更重要:您可以在以后扩展该类的功能,而不提高主程序的复杂性。
2.
稍后,您甚至可以使用这个类来处理玩家与之搏斗的怪物。怪物也可以被“玩家”对象描述,所以:
Player firstEnemy(10, 25, ...<stats here>);
firstEnemy.doCombat(playerCharacter);...where playerCharacter是描述玩家的对象。当然,doCombat()方法应该由您开发(可能是通过调用RealBattle.do() );
3.
我为您找到了一个“如何在C++中开发RPG游戏”教程这里。
4.
网站上的这个问题也会对你有帮助。
5.
无论如何,我也看到,您所矛盾的问题是如何处理代码的复杂性。您已经在main()函数中有了大约30个局部变量,并且您开始不记得什么是什么。这是编程中一个众所周知的问题,整个C++ (和大多数编程语言)都是为了能够轻松地处理这个问题而开发的。这台机器看起来一切都很好,但你是一个人,而你不是。你需要在你的代码中的结构,以很好地理解一切。
https://codereview.stackexchange.com/questions/132193
复制相似问题