• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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.

6 replies to this topic

### #1cavendert  Members

Posted 18 August 2014 - 08:33 AM

So, the code that I have works, and as far as I know, doesn't have any bugs. But, I was unsure how to write a piece of code near the end, and was able to do it (with lots of unnecessary strings), and would like some advice on how to simplify it. Code below, and then I'll explain which part (though you can probably guess).

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 = 70
},
new Pokemon {
Name= "Metagross",
Health = 220,
Attack = 60
},
new Pokemon {
Name = "Aegislash",
Health = 250,
Attack = 40
},
new Pokemon {
Name = "Quagsire",
Health = 220,
Attack = 50
},
new Pokemon {
Name = "Gardevoir",
Health = 170,
Attack = 70
}
};

static List<Pokemon> myPokemon = new List<Pokemon>() {
new Pokemon {
Name = "Blastoise",
Health = 250,
Attack = 60
},
new Pokemon {
Name = "Blazekin",
Health = 180,
Attack = 70
},
new Pokemon {
Name = "Meganium",
Health= 200,
Attack = 60
}
};

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();
Console.WriteLine("I choose you, " + randomChoice.Name);
Console.WriteLine();
Console.WriteLine("Press 1 for Blastoise.");
Console.WriteLine("Press 2 for Blazekin.");
Console.WriteLine("Press 3 for Meganium.");
Console.WriteLine();
var myName = myPokemon[int.Parse(myInput) - 1].Name;
var myHealth = myPokemon[int.Parse(myInput) - 1].Health;
var myAttack = myPokemon[int.Parse(myInput) - 1].Attack;
Console.WriteLine(myName + ", I choose you!");

Console.WriteLine("Type yes to Attack.");

while(myHealth > 0 && randomChoice.Health>0)
{
myHealth -= randomChoice.Attack;
randomChoice.Health -= myAttack;
};

if (myHealth <= 0)
Console.WriteLine("You are out of useable Pokemon! You blacked out!");
else
Console.WriteLine("You have defeated Gamer Chad! You earn a pat on the back!");

}
}
}
So, with the random selection in the beginning, I can always reference that, whether it be health or attack. But, I wasn't able to figure out how to take myInput, and save that as a reference point. So, here is the long part.

Console.WriteLine("Press 1 for Blastoise.");
Console.WriteLine("Press 2 for Blazekin.");
Console.WriteLine("Press 3 for Meganium.");
Console.WriteLine();
var myName = myPokemon[int.Parse(myInput) - 1].Name;
var myHealth = myPokemon[int.Parse(myInput) - 1].Health;
var myAttack = myPokemon[int.Parse(myInput) - 1].Attack;
Console.WriteLine(myName + ", I choose you!");

As you can see at the end of the code in the While statement, I use myName, myHealth, and myAttack whenever necessary, while I can just keep sing randomChoice.etc.

Is there a way to simplify the code above?

### #2Kaptein  Prime Members

Posted 18 August 2014 - 09:01 AM

Quickly looking at this, you can at least enumerate your own pokemon:

for (int i = 0; i < myPokemon.Count; i++)
{
Console.WriteLine("Press " + (i + 1) " for " + myPokemon[i].Name + ".");
}


Also

var myInput = Console.ReadLine();
int index = int.Parse(myInput) - 1;

Pokemon selected = myPokemon[index];

var myName = selected.Name;
var myHealth = selected.Health;
var myAttack = selected.Attack;



Edited by Kaptein, 18 August 2014 - 09:05 AM.

### #3cavendert  Members

Posted 18 August 2014 - 09:59 AM

Ok. I follow you with the first part. In the 2nd part, wouldn't I still need to use all 3 variables in the while loop? I guess my next question would be this. Is it possible to avoid creating the 3 variables, or is that just how it has to be?

### #4Kaptein  Prime Members

Posted 18 August 2014 - 10:27 AM

No, they are only there so the code looks like what you had before.

You don't have to create new variables in this case, since selected.Name is very straight-forward and tells anyone reading it exactly what the code would be doing.

Console.WriteLine(selected.Name + ", I choose you!");


### #5cavendert  Members

Posted 19 August 2014 - 10:24 AM

Alright, I needed to step away from this for a bit, because I wasn't following.

Having said that, and going back and writing it again fresh with your advice, it all makes perfect since to me.

The

 index, and Pokemon selected

was exactly what I was looking for. Thank you for the help! I'm writing this again fresh, so I can get it to memory.

### #6Kaptein  Prime Members

Posted 19 August 2014 - 11:25 AM

Right, again I wrote it so that you could try to follow what happened easier.

1. We figured out which pokemon the user selected

2. We turned that number into an index into the Pokemon-we-own array

3. We turned that index into a selected Pokemon, which is not a copy (notice no NEW keyword)

Instead it's just a reference, or better yet, just a name for a Pokemon existing somewhere else. It originally came from myPokemon[index].

The object will continue to stay alive/exist as long as the object has existing references.

An example:

Pokemon P1 = myPokemon[index];
Pokemon P2 = P1;
Pokemon P3 = P2;
Pokemon P4 = P3;
Pokemon P5 = P4;

Console.WriteLine(P5.Name + ", I choose you!");


So, now that I have a Pokemon named P5, which is a reference to the Pokemon named P4, which is ... to a Pokemon named P1, which is a reference to myPokemon[index].

They are all one and the same Pokemon.

Edited by Kaptein, 19 August 2014 - 11:27 AM.

### #7cavendert  Members

Posted 19 August 2014 - 11:46 AM

Awesome! Thank you again for the advice and the help!

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.