0

我正在学习制作 MUD(基于文本的 RPG)的教程,但我的主要功能有问题。如果你看一下代码,你会看到当玩家移动时,它会检查随机遭遇,如果 monster != 0,它将进入战斗循环。当我在命令提示符下执行此操作时,它将允许我攻击怪物,但它永远不会进入 monster->attack(mainPlayer) 函数。它只是回到显示我是否要移动、休息、查看统计数据或退出的屏幕。对此的任何帮助将不胜感激!

#include "stdafx.h"
#include "Map.h"
#include "Player.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
    srand( time(0) );
    Map gameMap;
    Player mainPlayer;

    mainPlayer.createClass();

    // Begin adventure
    bool done = false;
    while( !done )
    {
        // Each loop cycle we output the player position and
        // a selection menu.

        gameMap.printPlayerPos();

        int selection = 1;
        cout << "1) Move 2) Rest 3) View Stats 4) Quit: ";
        cin >> selection;

        Monster* monster = 0;
        switch( selection )
        {
        case 1:
            // Move the player
            gameMap.movePlayer();

            // Check for a random encounter.  This function
            // returns a null pointer if no monsters are
            // encountered.
            monster = gameMap.checkRandomEncounter();

            // 'monster' not null, run combat simulation.
            if( monster != 0)
            {
                // Loop until 'break' statement.
                while( true )
                {
                    // Display hitpoints
                    mainPlayer.displayHitPoints();
                    monster->displayHitPoints();
                    cout << endl;

                    // Player's turn to attack first.
                    bool runAway = mainPlayer.attack(*monster);

                    if( runAway )
                    {
                        break;
                    }

                    if( monster->isDead() )
                    {
                        mainPlayer.victory(monster->getXPReward());
                        mainPlayer.levelUp();
                        break;
                    }

                    monster->attack(mainPlayer);

                    if( mainPlayer.isDead() )
                    {
                        mainPlayer.gameover();
                        done = true;
                        break;
                    }
                }

                // The pointer to a monster returned from
                // checkRandomEncounter was allocated with
                // 'new', so we must delete it to avoid
                // memeory leaks.
                delete monster;
                monster = 0;
            }

            break;
        case 2:
            mainPlayer.rest();
            break;
        case 3:
            mainPlayer.viewStats();
            break;
        case 4:
            done = true;
            break;
        } // End switch statement
    } // End While statement
} // End main function

这是 Player::attack 函数:

bool Player::attack(Monster& monster)
{
    int selection = 1;
    std::cout << "1) Attack 2) Run: ";
    std::cin >> selection;
    std::cout << std::endl;

    switch( selection )
    {
    case 1:
        std::cout << "You attack the " << monster.getName()
                  << " with a " << mWeapon.mName << std::endl;

        if( Random(0, 20) < mAccuracy )
        {
            int damage = Random(mWeapon.mDamageRange);

            int totalDamage = damage - monster.getArmor();

            if( totalDamage <= 0)
            {
                std::cout << "Your attack failed to penetrate the " 
                          << monster.getName() << "'s armor." << std::endl;
            }
            else
            {
                std::cout << "You attack for " << totalDamage
                          << " damage!" << std::endl;

                // Subtract from monster's hitpoints.
                monster.takeDamage(totalDamage);
            }
        }
        else
        {
            std::cout << "You miss!" << std::endl;
        }
        std::cout << std::endl;
        break;

    case 2:
        // 25% chance of being able to run.
        int roll = Random(1, 4);

        if( roll == 1 )
        {
            std::cout << "You run away!" << std::endl;
            return true; //<-- Return out of the function.
        }
        else
        {
            std::cout << "You could not escape!" << std::endl;
            break;
        }
    }
}

这里是 Monster::attack 函数:

void Monster::attack(Player& player)
{

    cout << "A " <<mName << " attacks you "
         << "with a " << mWeapon.mName << std::endl;

    if( Random(0,20) < mAccuracy )
    {
        int damage = Random(mWeapon.mDamageRange);

        int totalDamage = damage - player.getArmor();

        if( totalDamage <= 0 )
        {
            cout << "The " << mName << "'s attack failed to "
                 << "penetrate your armor." << endl;
        }

        else
        {
            cout << "You are hit for " << totalDamage
                 << " damage!" << endl;

            player.takeDamage(totalDamage);
        }
    }
    else
    {
        cout << "The " << mName << " missed!" << endl;
    }
    cout << endl;
}
4

2 回答 2

3

您的Player::attack()方法只有一个返回语句:return true;. 您忘记将最后一行添加return false;到您的方法中。

如果您启用警告(并注意它们!),这很容易被阻止

于 2013-09-16T18:36:24.120 回答
1

Player::attack不是return在所有情况下(特别是当它需要 return 时false)。当调用函数试图访问它的返回值时,Player::Attack它会变成垃圾,所以你进入if(ranAway)块并跳出你的 while 循环

于 2013-09-16T18:38:07.543 回答