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();
}
}
}
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?