我是一个全新的编码人员,他用c ++学习了不到两周的时间,并自学了很多if语句和开关。
如果有人能够通过我已经制作过的游戏并通过我应该和不应该做的所有事情与我交谈,那将是很棒的。
请注意,第二条跟踪 if语句尚未编码。
#include "MeadowGiant.h"
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
MeadowGiant::MeadowGiant()
{
int trail, caveEnter;
int fight1;
int loot;
int goblinAttack, goblinHP, HP, attack, action1, ability;
goblinAttack = 5;
goblinHP = 10;
HP = 100;
std::cout << -Insert story narrative- Choose a trail to go down.\n";
std::cin >> trail;
if(trail==1)
{
std::cout << "-Insert story narrative- Come across a cave. Do you go in?\n";
std::cin >> caveEnter;
if(caveEnter==1)
{
std::cout << "You are ambushed by a goblin. Run or fight?\n";
std::cin >> fight1;
goblinFight:
loot = rand() % 4 + 1;
srand(time(NULL));
if(fight1==1)
{
std::cout << "Your HP: " << HP << std::endl;
std::cout << "Enemy HP: " << goblinHP << std::endl;
std::cout << "What do you do?\n
<< "[1] Attack [2] Run \n";
std::cin >> action1;
}
if(goblinHP<=0)
{
std::cout << "You have slain the goblin!\n"
<< "You head towards the chest and take the spoils\n"
<< "of battle!\n";
switch(loot)
{
case 1: std::cout << "You find a bastard sword!\n"
attack = attack + 7;
goto exitCave;
case 2: std::cout << "You find an Enchanted Staff!\n"
attack = attack + 10;
goto exitCave;
case 3: std::cout << "You find an Obsidian Dagger!\n"
attack = attack + 9;
goto exitCave;
case 4: std::cout << "You find a Holy Mace!\n"
attack = attack + 10;
goto exitCave;
}
else if(action1==1)
{
std::cout << "You successfully hit the goblin!\n"
<< "He strikes back!\n";
attack = rand() % 10 + 1;
srand(time(NULL));
goblinHP = goblinHP - attack;
HP = HP - goblinAttack;
goto goblinFight;
}
else if(action==2)
{
std::cout << "You take the cowards way out and leave the cave.\n";
goto exitCave;
}
}
else if(caveEnter==2)
{
exitCave:
std::cout << "You have exited the cave.\n";
}
else
{
goto exitCave;
}
}
}
对于那些问过的人来说,这是MeadowGiant.h。
#define MEADOWGIANT_H
class MeadowGiant
{
public:
MeadowGiant();
protected:
};
#endif`
#ifndef MEADOWGIANT_H
#define MEADOWGIANT_H
class MeadowGiant
{
public:
MeadowGiant();
protected:
};
#endif
这就是我在main.cpp中所做的一切
#include <iostream>
#include <string>
#include "MeadowGiant.h"
int main()
{
MeadowGiant obj1;
}
答案 0 :(得分:0)
对我来说,唯一真正跳出来的一个好主意就是使用goto
。例如,在switch语句中,你使用break
来跳出来。
答案 1 :(得分:0)
您应该检查输入是否有切换条件。如果有人按下“9”之类的其他键,会发生什么?它只会跳过整个if条件部分。第二个std::cin >> action1;
action1是一个整数,如果有人输入你的程序将会出错的字母。
我推荐的方法:
fail:char select = _getch() - '0';
switch (select) {
case 1: {....; break;}
case 2: {....; break;}
default: goto fail;
}
srand(time(NULL)); // <-- first the random seed
attack = rand() % 10 + 1; // <-- then the random function
答案 2 :(得分:0)
else if(caveEnter==2)
{
exitCave:
std::cout << "You have exited the cave.\n";
}
else
{
goto exitCave;
}
您可以将此简化为
else {
exitCave:
std::cout << "You have exited the cave.\n";
}
此外,建议不要使用goto
。在switch
内,使用break
。