Jump to content

  • Log In with Google      Sign In   
  • Create Account

Stuck in C#......


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
10 replies to this topic

#1 cavendert   Members   -  Reputation: 164

Like
0Likes
Like

Posted 08 August 2014 - 04:40 PM

Hey guys. I'm sure that this is beyond simple, but I can't figure this out. As you will see in the code below, I'm trying to simulate a Pokemon battle (stupid easy). I'm trying to create a structure for 6 pokemon, and associate a string name, and an int for health. 
 
I've figured out how to make that in the "Class" section. So, my question is, how do I then input the pokemon into the structure, so the health is associated with a pokemon? I know how to write it, and call it in C++, but I'm at a complete loss here. So, code below (please be gentle), and let me know what I'm missing please. 
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Practice_Battle
{
    class Program
    {
        struct chadList
        {
            string name;
            int health;
        };
        static void Main(string[] args)
        {
 
            chadList chadPokemon;
            {
                
 
            }
 
 
            //Chad's List of Pokemon
            //string[] chadPokemon = new string[6] { "Charizard", "Garchomp", "Metagross", "Aegislash", "Quagsire", "Gardevoir" };
 
            //Random Pokemon Generator
            Random ran = new Random();
            string randomChoice = chadPokemon[ran.Next(0, chadPokemon.Length)];
 
 
 
            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("I choose you, " + randomChoice);
            Console.WriteLine("Please choose your Pokemon:");
            Console.WriteLine("Blastoise, Blazekin, Meganium");
            string myChoice = Console.ReadLine();
 
            if (myChoice == "Blastoise")
                message = "A water Pokemon, with lots of health. Good Choice!";
            else if (myChoice == "Blazekin")
                message = "A fire Pokemon, with lots of Power. Let's do this!";
            else
                message = "A plant Pokemon, with lots of health. Best of luck!";
 
            Console.WriteLine(myChoice + ", " + message);
            Console.ReadLine();
 
        }
    }
}

Thank you for any advice that you can give!



Sponsor:

#2 Glass_Knife   Moderators   -  Reputation: 4443

Like
1Likes
Like

Posted 08 August 2014 - 05:25 PM


I know how to write it, and call it in C++, but I'm at a complete loss here.

 

I am not sure what you mean here.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#3 Spyderzone   Members   -  Reputation: 182

Like
2Likes
Like

Posted 08 August 2014 - 05:45 PM

In C++ you would probably use something like a vector with 6 elements.  Then just fill your vector with instances of the Pokemon object.

In C# you would do the same thing but I believe the proper container is the ArrayList.



#4 ChillyFlashER   Members   -  Reputation: 189

Like
1Likes
Like

Posted 08 August 2014 - 06:10 PM

//Chad's List of Pokemon
chadList[] chadPokemon = 
{ 
	{ "Charizard", 100 },
	{ "Garchomp", 100 }
};
 
//Random Pokemon Generator
Random ran = new Random();
chadList randomChoice = chadPokemon[ran.Next(0, chadPokemon.Length)];

I have no idea of what you want to do... this it?

 

 

This might be to good use, Collections.


Edited by ChillyFlashER, 08 August 2014 - 06:15 PM.


#5 Brian Sandberg   Members   -  Reputation: 1330

Like
3Likes
Like

Posted 08 August 2014 - 06:10 PM

In the .NET world, you would use a System.Collections.Generic.List<T> to hold your pokemons.  Maybe this will help you - if not, ask again and try to better describe exactly what it is you would like to do.

using System;
using System.Collections.Generic;
using System.Linq;

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

	class Program
	{
		static List<Pokemon> Pokemons = new List<Pokemon>() {
			new Pokemon {
				Name = "Charizard",
				Health = 200
			},
			new Pokemon {
				Name = "Garchomp",
				Health = 180
			},
			new Pokemon {
				Name = "Metagross",
				Health = 220
			}
		};

		static void Main ( string[] args )
		{
			var rnd = new Random();
			var randomPokemon = Pokemons[rnd.Next( Pokemons.Count )];

			Console.WriteLine( "You were given a {0} with {1} health.", randomPokemon.Name, randomPokemon.Health );
		}
	}
}



#6 cavendert   Members   -  Reputation: 164

Like
0Likes
Like

Posted 14 August 2014 - 07:54 PM

I'm really sorry for the poor description, but Brian, that's exactly what I was looking for. If I could ask a question about it. When you write List<Pokemon>, Pokemon is the name of the list?

 

And later, after the random selection (since I won't know what was chosen), could I attach that randomPokemon.name to a string, (chadChoice=randomPokemon.name), and when I call that string, it would still associate the correct Health stat, with the Name that was chosen?



#7 Endurion   Crossbones+   -  Reputation: 3575

Like
3Likes
Like

Posted 14 August 2014 - 11:06 PM

List<Pokemon> is the typename of the class. The container is a class called List. The < > are a sign that the List uses Generics. The type inside the brackets defines the type of class this List contains.

 

In this line:

 

List<Pokemon> Pokemons = new List<Pokemon>();

 

List<Pokemon> is the type

Pokemans is the name of the list


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#8 cavendert   Members   -  Reputation: 164

Like
0Likes
Like

Posted 15 August 2014 - 08:52 AM

Ok, thank you! I will be giving this a shot today, and will be able to check back in. 



#9 cavendert   Members   -  Reputation: 164

Like
0Likes
Like

Posted 15 August 2014 - 09:42 AM

Ok. The list worked just fine, so thank you all for that. I have one more question pertaining to the list now. I have successfully had my Opponent randomly select from his list, and display no problem. Now, I want to reference my selection. I have given myself a choice of 3 pokemon, and call them by choosing 1,2 or 3. 

 

How do I call that up? With something along the lines of Console.WriteLine(myChoice.length -1); ?

 

Here is the code to try and make more sense of it. 

 

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("I choose you, " + randomChoice.Name);
            Console.WriteLine("Please choose your Pokemon: (1,2,3)");
            Console.WriteLine("1. Blastoise, 2. Blazekin, 3. Meganium");
            string myChoice = Console.ReadLine();


            if (myChoice == "1")
                message = "Blastoise, a water Pokemon, with lots of health. Good Choice!";
            else if (myChoice == "2")
                message = "Blazekin, a fire Pokemon, with lots of Power. Let's do this!";
            else
                message = "Meganium, a plant Pokemon, with lots of health. Best of luck!";


            Console.WriteLine(message);
            Console.ReadLine();


            Console.WriteLine("Type yes to Attack.");
            acceptInput = Console.ReadLine();
                       
        }
    }
}


#10 Brian Sandberg   Members   -  Reputation: 1330

Like
1Likes
Like

Posted 15 August 2014 - 11:43 AM

When the user enters something, you get a piece of text back.  What you need to do is to find out if they actually wrote a number, and then determine what number that is, so you can use it to retrieve an element from your list of pokemons. Luckily, the built-in number types have methods for parsing text.  To convert text to an integer, you might use Convert.ToInt32() or Int32.Parse() or Int32.TryParse().

 

You also want to do some sort of error handling; if the user didn't enter a number, or they wrote a larger number than you have pokemons, then you should ask them again until they enter something valid.

 

Here's a simple loop to do that.

Pokemon SelectedPokemon = null;
do {
	Console.WriteLine("Please choose your Pokemon: (1,2,3)");
	Console.WriteLine("1. Blastoise, 2. Blazekin, 3. Meganium");
	string myChoice = Console.ReadLine();

	if (myChoice == "1")
		message = "Blastoise, a water Pokemon, with lots of health. Good Choice!";
	else if (myChoice == "2")
		message = "Blazekin, a fire Pokemon, with lots of Power. Let's do this!";
	else
		message = "Meganium, a plant Pokemon, with lots of health. Best of luck!";

	Console.WriteLine(message);
	Console.ReadLine();

	int SelectedPokemonNo = 0;
	if( !Int32.TryParse( myChoice, out SelectedPokemonNo ) )
	{
		// uh oh, myChoice wasnt a number
		Console.WriteLine( "That's not a pokemon! Try again" );
		continue;
	}
	SelectedPokemonNo--; // make it a 0-based index
	if( SelectedPokemonNo < 0 || SelectedPokemonNo >= myPokemon.Count )
	{
		// uh oh, myChoice out of range
		Console.WriteLine( "That's not a pokemon! Try again" );
		continue;
	}

	SelectedPokemon = myPokemon[SelectedPokemonNo];
	Console.WriteLine( "You selected {0} with {1} health.", SelectedPokemon.Name, SelectedPokemon.Health );

}
while( null == SelectedPokemon );

Something else you might think about is how you have built the pokemon names and descriptions directly into the code that asks to choose between them.  A better design would be to put the descriptions ("a water Pokemon, with lots of health") into another field in the Pokemon class, and have the menu retrieve it from there.

 

I think it would be good for you to read some tutorials on C#; they would have taught you about converting texts to numbers, and using lists, and countless more details.  I just googled up this one; there are 23 pages and it looks decent - if you go through all those those, you'll be much better prepared.  Enjoy smile.png  http://www.csharp-station.com/Tutorial/CSharp/Lesson01


Edited by Brian Sandberg, 15 August 2014 - 11:44 AM.


#11 cavendert   Members   -  Reputation: 164

Like
0Likes
Like

Posted 15 August 2014 - 12:33 PM

I'm currently going through the Microsoft Virtual Academy for the tutorial, but I will definitely read up on that. I don't mind having to read things. I totally forgot about the "parse", and found it in my notes right before I got back on this forum. 

 

So, I'm having a slightly different issue, but I've put that in a different thread while I try to figure that one out. Thank you guys for all of the help on this topic!!!!






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