Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

If statements not working

Discussion in 'Scripting' started by pauliina, Feb 9, 2016.

  1. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Hey! This is probably very easily solved, but I simply can't find what's wrong with my code. This is what I want it to do: If I enter "attack" it attacks, then if I enter "defend" it defends, and if I then enter "attack" again, it does that, and so on and so on. It simply loops from the beginning and ignores what you just wrote.

    And this is what it does:
    Let's say I enter "attack". It then does the "attack", but then no matter what I enter after that, it only does "attack". So even if I write "defend" or "bkjwkjfknd" it still just attacks.

    What am I missing? I've tried so many different things, for example, changing the if else-statements to just several if-statements, I've also tried using switch cases, and that got a different error (the first answer I entered was ignored completely, the second answer was done, and then the first answer was ignored, but the second was done and so on).

    Here's my code:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. namespace ConsoleApplication1
    8. {
    9.     class Program
    10.     {
    11.         static void Main(string[] args)
    12.         {
    13.  
    14.             Random random = new Random();
    15.             Random attack = new Random();
    16.  
    17.             //player variables
    18.             int playerHealth = 50;
    19.             int playerAttack = attack.Next(5, 15);
    20.             string playerName;
    21.  
    22.             //strings with enemy type and name + randomising
    23.             string[] enemyType = "warrior|goblin|elf|evil princess".Split('|');
    24.             int typeChoice = new Random().Next(enemyType.Length);
    25.             string[] enemyName = "Leia|Nora|Baron|Cahira".Split('|');
    26.             int nameChoice = new Random().Next(enemyType.Length);
    27.  
    28.             // enemy variables
    29.             int enemyHealth = 50;
    30.             int enemyAttack = attack.Next(5, 15);
    31.             string randomEnemyType = enemyType[typeChoice];
    32.             string randomEnemyName = enemyName[nameChoice];
    33.  
    34.  
    35.             // Starting of the game
    36.             Console.WriteLine("What is your name?");
    37.                 playerName = Console.ReadLine();
    38.  
    39.             // here I have a lot of text not relevant to the problem
    40.  
    41.             Console.WriteLine("What will you do? Type 'attack' or 'defend'.");
    42.             string input = Console.ReadLine();
    43.  
    44.             do
    45.             {
    46.                 if (input == "attack")
    47.                 {
    48.                     enemyHealth -= playerAttack;
    49.                     playerHealth -= enemyAttack;
    50.                     Console.WriteLine("\n" + playerName + " attacks " + randomEnemyName + "! " + randomEnemyName + " attacks back!");
    51.                 }
    52.                 else if (input == "defend")
    53.                 {
    54.                     Console.WriteLine("\n" + playerName + " puts up their shield and defends themselves. ");
    55.                     playerHealth -= enemyAttack / 2;
    56.                 }
    57.                 else
    58.                 {
    59.                     Console.WriteLine("\nThat is not a valid command. Please write 'attack' or 'defend'.");
    60.                 }
    61.  
    62.  
    63.                 // health status
    64.                 Console.WriteLine(playerName + "s health is now " + playerHealth + " and " + randomEnemyName +
    65.                             "s health is now " + enemyHealth + ". What will you do next?");
    66.  
    67.                 Console.ReadLine();
    68.  
    69.             } while (playerHealth > 0 && enemyHealth > 0);
    70.  
    71.  
    72.             if (playerHealth <=0)
    73.             {
    74.                 Console.WriteLine(randomEnemyName + " slayed " + playerName + "!!");
    75.                 Console.ReadLine();
    76.             }
    77.             if (enemyHealth <=0)
    78.             {
    79.                 Console.WriteLine(playerName + " slayed " + randomEnemyName + "!!");
    80.                 Console.ReadLine();
    81.             }
    82.         }
    83.         }
    84.     }
    85.  
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    add input = Console.ReadLine(); instead of just Console.ReadLine(). You're only assigning your string once (before the loop)
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    A.Killingbeck is spot on.

    Protip, you can create arrays directly, instead of using split:

    Code (csharp):
    1. string[] enemyType = {
    2.     "warrior",
    3.     "goblin",
    4.     "elf",
    5.     "evil princess"
    6. };
     
    pauliina likes this.
  4. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Haha, ah! It was that easy! Much thanks to you, sir. It now works (almost) flawlessly. The only thing not working now is when you die/win. I've put down that if the health is 0, or less than zero, they're dead, but for some reason, say I have 5 in health, and then the enemy hits 6 in health, then instead of just dying, it tells me I have -1 in health, and then the next move it tells me I died. Why's that? And how do I fix this?

    I tried the code you have, or something similar before I did the code I currently have, but that didn't work perfectly. Maybe I just had some other errors, but are there any differences between the list you have up here, and the one I have? Could I literally just exchange my block of code for yours, and I wouldn't have to change anything else within my code?
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Yes, pretty much. In both instances you'll get a string array with those four entries in it. The Split command is slightly slower, but not to the degree that the difference is ever noticeable.

    The advice is mainly because the way I suggested is easier to read and add new entries to.

    The printing that you've got less than 0 health before you're dead is because that's the order in which you've written the code in the while loop.
     
    pauliina likes this.
  6. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Aha, okay! Thanks for the explanation!

    Also, how would I go about it if I don't want that to happen then? Because I want it to as soon as you go under 0, or is at 0, you die. It's not ideal that you see that you have -something instead of just dying.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You could use the break; statement, which instantly ends a loop, if either the player or the enemy has less than 0 health after applying damage.

    If you do that, you don't need that check in the while part of the loop, so you can just do while(true).

    EDIT: by the way, how does this relate to Unity?
     
  8. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Where would I put the break? Inside, before or after the do while loop? And do I have to remove some of my code? (because you said that I don't need that check in the while part, but I don't understand how the loop then knows what is true, if I just put true?)

    And about Unity: I didn't know it had to do with Unity as long as it was made in Unity. Sorry.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    So your do-while code looks like:

    Code (CSharp):
    1. do
    2. {
    3.     *check input*
    4.     *print health info*
    5.     *read input*
    6. } while (playerHealth > 0 && enemyHealth > 0);
    you could instead do:

    Code (CSharp):
    1. do
    2. {
    3.     *check input*
    4.     if(playerHealth <= 0 || enemyHealth <= 0) {
    5.         break;
    6.    }
    7.    *print health info*
    8.     *read input*
    9. } while (true);
    That means that the printing health info and reading input part of the loop would be skipped when you break out of the loop.