0

私はアリーナ風のコンソールゲームに取り組んでいます。毎回モンスターと戦わなければならないのではなく、特定のモンスターと戦わせるためのボイドを持つことができるようにしたい. これが私のコードです:

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;


char select;
int randnum;
int level=1;
int maxhp=10;
int hp=maxhp;
int mhp;
int exp;
int dmg;
int m_maxhp;
int req_exp=10;
int day;
char boot;
int a;

ifstream leveli;
ofstream levelo;

ifstream playerhpi;
ofstream playerhpo;

ifstream expi;
ofstream expo;

ifstream maxhpi;
ofstream maxhpo;

ifstream req_expi;
ofstream req_expo;

ifstream dayi;
ofstream dayo;

void wait( time_t delay )
{
time_t timer0, timer1;
time( &timer0 );
do {
time( &timer1 );
} while (( timer1 - timer0 ) < delay );
}


// This is the void that's not being called
void bat()
{

    m_maxhp=10;
    mhp=m_maxhp;
    do{
    dmg= rand() % 4 + 1;
    cout << "Batt attacks you for " << dmg;
    hp=hp-dmg;
    cout << ", leaving you with [" << hp << "/" << maxhp << "]" << endl;
    wait(1);

    if(hp<=0)
    {
        cout << "You've lose. Try harder next time.";
        exp=exp/2;
        day=day+1;

        dayo.open("daynumber.dat");
        dayo << day;
        dayo.close();
    }

    else if(mhp<=0)
    {
        cout << "You have killed the Bat. You gain 6 EXP." << endl;
        exp=exp+6;
        day=day+1;

        dayo.open("daynumber.dat");
        dayo << day;
        dayo.close();

        expo.open("experience.dat");
        expo << exp;
        expo.close();
    }





        }while(mhp>0);
    }



int main()
{

leveli.open("level.dat");
leveli >> level;
leveli.close();

playerhpi.open("health.dat");
playerhpi >> hp;
playerhpi.close();

expi.open("experience.dat");
expi >> exp;
expi.close();

maxhpi.open("maxhealth.dat");
maxhpi >> maxhp;
maxhpi.close();

req_expi.open("req_exp.dat");
req_expi >> req_exp;
req_expi.close();


cout << "Data successfully loaded. Start the game now? y/n" << endl;
boot=_getch();

if(boot=='n')
{

    cout << "Exiting..." << endl;
    wait(3);
    exit(0);
}

else if(boot=='y'){
do{
if (exp==req_exp)
{
    level=level+1;
    cout << "Level Up! You are now level " << level << "!" << endl;

    exp=0;
    req_exp=req_exp*1.5;

    req_expo.open("req_exp.dat");
    req_expo << req_exp;
    req_expo.close();

    levelo.open("level.dat");
    levelo << level;
    levelo.close();
}


else{
cout << endl << "Day " << day << " in The Arena." << endl << "1. Fight" << endl << "2. Stats" << endl << "3. Full Heal" << endl << "4. Half Heal" << endl;
select=_getch();


    if(select=='1')
    {
            srand((unsigned)time(0));
            randnum = rand() % 500 + 1;

         //   cout << "*DEBUG* " << randnum << endl;

           // This part doesn't work
            if(randnum<300 && level<4)
            {
                cout << "You've been chosen to fight a Bat!" << endl;
                wait(1.5);
                void bat();

}
            else
            {

            }
    }


    else if(select=='2')
    {
        cout << endl << "Health: [" << hp << "/" << maxhp << "]" << endl << "Level: [" << level << "]" << endl << "Experience: " << "[" << exp << "/" << req_exp << "]" << endl;
        wait(0.5);
    }

    else
    {
        cout << "Invalid Command";
    }

}

}while (boot=='y');



}

return 0;
}

ボイドの使い方が間違っていませんか? もしそうなら、誰かが私が何を変えるべきかを指摘できますか?

4

2 に答える 2

2
if(randnum<300 && level<4)
{
    cout << "You've been chosen to fight a Bat!" << endl;
    wait(1.5);
    void bat(); // ?
}

ここで期待される動作は何ですか? void bat();関数宣言です。これは名前を紹介するだけで、bat他には何もしません。という名前の関数は呼び出しませんbat。を呼び出すだけbatの場合は、次のように記述します。

bat(); // a call to "bat"
于 2012-05-12T14:24:13.443 に答える
0

関数を呼び出すときは、コードなどの戻り値の型を省略します。

       // This part doesn't work
        if(randnum<300 && level<4)
        {
            cout << "You've been chosen to fight a Bat!" << endl;
            wait(1.5);
            void bat();

...

読むべき:

       // This part doesn't work
        if(randnum<300 && level<4)
        {
            cout << "You've been chosen to fight a Bat!" << endl;
            wait(1.5);
            bat();

...

于 2012-05-12T14:22:50.627 に答える