Silight
Silight

Reputation: 37

If Else statement logic

I'm doing a little text based game and one of my if/else statement are not giving me the expected results. When the user loses it immediately jumps to "Game Over!" It is supposed to say monsterName + " has defeated you!" then jump to game over.

Could you guys help point out my logic error?

// Main Game Loop
while (playerHealth > 0 && monsterHealth > 0)
{
    bool playerAct = askAction();

    if (playerAct == true)// User attacks
    {
        monsterHealth = monsterHealth - playerAtk;
        if (monsterHealth < 0)
        {
            Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
        }
        else
        {
            Console.WriteLine(monsterName + " takes a mighty swing back at you!");
            playerHealth = playerHealth - monsterAtk;
        }
    }                
    else// User defends
    {
        playerHealth = playerHealth - defendAtk;
        if (playerHealth < 0)
        {
            Console.WriteLine(monsterName + " has defeated you!");
        }
        else
        {
            Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
        }
    }               
}

// End Game
Console.WriteLine("Game Over!");
Console.ReadLine();            

I think that is the relevant code, but just in case, here is the whole program.

namespace textBasedGame
{
    class Program
    {
        static void Main(string[] args)
        {
        //Variables
        string name;
        string monsterName;
        int playerAtk = 5;
        int monsterAtk = 2;
        int defendAtk = 1;
        int playerHealth = 2;
        int monsterHealth = 3;

        // Gathers information from the user
        Console.WriteLine("What is your name?");
        name = Console.ReadLine();
        Console.WriteLine("What is your enemies name?");
        monsterName = Console.ReadLine();

        //Story Intro
        Console.WriteLine("You are hunting in the swamp and in the darkness of the overgrowth you see a movement.");
        Console.WriteLine("You creep closer to the silouette.");
        Console.WriteLine("A little closer....");
        Console.WriteLine("It looks familiar....");
        Console.WriteLine("It is " + monsterName + "!!!");
        Console.WriteLine("Now is your chance!");

        // Main Game Loop
        while (playerHealth > 0 && monsterHealth > 0)
        {
            bool playerAct = askAction();

            if (playerAct == true)// User attacks
            {
                monsterHealth = monsterHealth - playerAtk;
                if (monsterHealth < 0)
                {
                    Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
                }
                else
                {
                    Console.WriteLine(monsterName + " takes a mighty swing back at you!");
                    playerHealth = playerHealth - monsterAtk;
                }
            }                
            else// User defends
            {
                playerHealth = playerHealth - defendAtk;
                if (playerHealth < 0)
                {
                    Console.WriteLine(monsterName + " has defeated you!");
                }
                else
                {
                    Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
                }
            }               
        }

        // End Game
        Console.WriteLine("Game Over!");
        Console.ReadLine();            
    }

    static bool askAction()//Displays HP and asks for user action. Returns true or false
    {
        bool move;

        Console.WriteLine("What would you like to do?(type: attack or defend)");
        string playerAction = Console.ReadLine();

        if (playerAction == "attack")
        {
            return move = true;
        }
        else
        {
            return move = false;
        }

    }

}
}

Upvotes: 1

Views: 230

Answers (3)

Lightshadow
Lightshadow

Reputation: 56

First, I corrected this function...Basically you don't need the boolean "move", you're creating it for nothing, since you are not using it in the function.

When your function is a "bool", it returns a "bool". In other words, you can do

return true;

or

return false;

no need for

return move = true;

Try this instead:

static bool askAction()//Displays HP and asks for user action. Returns true or false
{
    Console.WriteLine("What would you like to do?(type: attack or defend)");
    string playerAction = Console.ReadLine();

    if (playerAction == "attack")
    {
        return true;
    }

    return false;
}

Here is your main loop. I guess that your monster dies at 0 health, so I changed the sign

while (playerHealth > 0 && monsterHealth > 0)
{
    bool playerAct = askAction();

    if (playerAct == true)// User attacks
    {
        monsterHealth = monsterHealth - playerAtk;
        if (monsterHealth <= 0)
        {
            Console.WriteLine(monsterName + " falls to the ground, defeated. Congradulation, " + name + "!");
        }
        else
        {
            Console.WriteLine(monsterName + " takes a mighty swing back at you!");
            playerHealth = playerHealth - monsterAtk;
        }
    }                
    else// User defends
    {
        playerHealth = playerHealth - defendAtk;
        if (playerHealth <= 0)
        {
            Console.WriteLine(monsterName + " has defeated you!");
        }
        else
        {
            Console.WriteLine(monsterName + " swings at you. It glances off your defenses.");
        }
    }               
}

Oh and by the way, at the end of the program, you say "Game Over", but what if the player won?

Upvotes: 1

brainless coder
brainless coder

Reputation: 6430

The only logical error I see is, you have monsterHealth = 2 and playerAtk = 5, so the monster is dead in every first attack and also monsterAtk = 2; and playerHealth=2. Similarly player dies on first attack. So it has become a one move game.

Then for defending, after player health is zero it exists without printing.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361585

while (playerHealth > 0 && monsterHealth > 0)

if (playerHealth < 0)

The while loop continues as long as the player's health is above zero. The if statement fires when their health is below zero. What happens if their health is exactly zero?

Upvotes: 8

Related Questions