Followers 0

Stuck in C#......

10 posts in this topic

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;

namespace Practice_Battle
{
class Program
{
{
string name;
int health;
};
static void Main(string[] args)
{

{

}

//string[] chadPokemon = new string[6] { "Charizard", "Garchomp", "Metagross", "Aegislash", "Quagsire", "Gardevoir" };

//Random Pokemon Generator
Random ran = new Random();

Console.WriteLine("Do you accept? (yes or no)");

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("Blastoise, Blazekin, Meganium");

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);

}
}
}

Thank you for any advice that you can give!

0

Share on other sites

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.

1

Share on other sites

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.

2

Share on other sites
//Chad's List of Pokemon
{
{ "Charizard", 100 },
{ "Garchomp", 100 }
};

//Random Pokemon Generator
Random ran = new Random();


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

This might be to good use, Collections.

Edited by ChillyFlashER
1

Share on other sites

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?

0

Share on other sites

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

0

Share on other sites

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;

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)
{

//string[] chadPokemon = new string[6] { "Charizard", "Garchomp", "Metagross", "Aegislash", "Quagsire", "Gardevoir" };

//Random Pokemon Generator
var rnd = new Random();

Console.WriteLine("Do you accept? (yes or no)");

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("1. Blastoise, 2. Blazekin, 3. Meganium");

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.WriteLine("Type yes to Attack.");

}
}
}

0

Share on other sites

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("1. Blastoise, 2. Blazekin, 3. Meganium");

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);

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  http://www.csharp-station.com/Tutorial/CSharp/Lesson01

Edited by Brian Sandberg
1

Share on other sites

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!!!!

0

Create an account

Register a new account