Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Advice on Simplifying


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

#1 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

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


            //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 myInput = Console.ReadLine();
            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.");
            acceptInput = Console.ReadLine();


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


            Console.ReadLine();
        }
    }
}
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.");
            var myInput = Console.ReadLine();
            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?

 



Sponsor:

#2 Kaptein   Prime Members   -  Reputation: 2174

Like
4Likes
Like

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.


#3 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

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?



#4 Kaptein   Prime Members   -  Reputation: 2174

Like
1Likes
Like

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


#5 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

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. 



#6 Kaptein   Prime Members   -  Reputation: 2174

Like
1Likes
Like

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.


#7 cavendert   Members   -  Reputation: 170

Like
0Likes
Like

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.



PARTNERS