Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C# While Statement


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

Posted 15 August 2014 - 11:43 AM

This should be simple, and I'm really hung up on this here......

 

As you will see in the code below, I'm trying to make a simple Pokemon battle. I'm trying to create a While loop, to constantly subract Health and Attack, until somebody has 0 health.

 

Though the code will be written incorrectly, he is what I'm trying to do:

while (myPokemon[int parse(myChoice)-1].Health >0 && chadPokemon[randomChoice].Health>0)

This is giving me errors. I will post the rest of my current, working code below. Please look it over, and let me know what I am missing!!

 

 

            

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Practice_Battle
{
    class Pokemon
    {
        public string Name;
        public int Health;
        public int Attack;
    }


    class Program
    {
        static List<Pokemon> chadPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Charizard",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Garchomp",
                Health = 180,
                Attack = 50
            },
            new Pokemon {
                Name= "Metagross", 
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Aegislash",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Quagsire",
                Health = 200, 
                Attack = 50
            },
            new Pokemon {
                Name = "Gardevoir",
                Health = 200,
                Attack = 50
            }
        };


        static List<Pokemon> myPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Blastoise",
                Health = 200,
                Attack = 50
            },
            new Pokemon {
                Name = "Blazekin",
                Health = 200, 
                Attack = 50
            },
            new Pokemon {
                Name = "Meganium", 
                Health= 200, 
                Attack = 50
            }
        };
        
        static void Main(string[] args)
        {
                       
            //Chad's List of Pokemon
            //string[] chadPokemon = new string[6] { "Charizard", "Garchomp", "Metagross", "Aegislash", "Quagsire", "Gardevoir" };


            //Random Pokemon Generator
            var rnd = new Random();
            var randomChoice = chadPokemon[rnd.Next(chadPokemon.Count)];
           




            Console.WriteLine("Gamer Chad wants to battle!");
            Console.WriteLine("Do you accept? (yes or no)");
            string acceptInput = Console.ReadLine();


            string message = "";


            if (acceptInput == "yes")
                message = "It's time to put your game face on!";
            else
                message = "Too bad. It's time to battle!";


            //Random Pokemon is Chosen, I choose a Pokemon
            Console.WriteLine(message);
            Console.WriteLine();              
            Console.WriteLine("I choose you, " + randomChoice.Name);
            Console.WriteLine();
            Console.WriteLine("Please choose your Pokemon:");
            Console.WriteLine("Press 1 for Blastoise.");
            Console.WriteLine("Press 2 for Blazekin.");
            Console.WriteLine("Press 3 for Meganium.");
            var myChoice = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine(myPokemon[int.Parse(myChoice)-1].Name + ", I choose you!");
           
            Console.WriteLine("Type yes to Attack.");
            acceptInput = Console.ReadLine();


            while(myPokemon.Health >0 && chadPokemon.Health>0)
            {


            }
                       
        }
    }
}
 


Sponsor:

#2 Josh Petrie   Moderators   -  Reputation: 3888

Like
2Likes
Like

Posted 15 August 2014 - 11:57 AM

What exactly are the errors?

 

The first line of code won't compile because "myPokemon[int parse(myChoice) - 1]" isn't a valid expression. You probably mean "int.Parse(myChoice)" not "int parse(myChoice)."


Josh Petrie | Game Developer, Undead Labs


#3 ChillyFlashER   Members   -  Reputation: 189

Like
1Likes
Like

Posted 15 August 2014 - 12:24 PM

"randomChoice" is also a Pokemon and not an integer. "randomChoice.Health > 0" instead of "chadPokemon[randomChoice].Health>0)".



#4 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

Posted 15 August 2014 - 12:42 PM

Oh.........duh. Lol. I was using an example that I had from C++. 

 

"randomChoice" is also a Pokemon and not an integer. "randomChoice.Health > 0" instead of "chadPokemon[randomChoice].Health>0)".

This makes much more sense. My only question to that is, will randomChoice.Health remember what it chose earlier in the code, or will it run the random selection again?



#5 Dave Hunt   Crossbones+   -  Reputation: 2479

Like
1Likes
Like

Posted 15 August 2014 - 01:17 PM


or will it run the random selection again?

Why would it? randomChoice won't change unless you have code inside the while loop that changes it.

 

But, the best way to determine that for yourself would be for you to actually try the code and see.



#6 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

Posted 15 August 2014 - 02:15 PM

Right, and that makes perfect sense. I just wanted to make sure that I fully understood it. I have a really bad habit of just doing things, and knowing that they work, and not why they work. Thanks for the information! I seem to be on the right path now. I just need to find a more efficient way to write it. 

 

Thanks for the help, and I'll be back in a new forum if I'm stuck again. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS