Karto
Karto
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
The Start function is just my non static main method
public class Program
{
static void Main(string[] args)
{
new PokeGam().Start();

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
public class Program
{
static void Main(string[] args)
{
new PokeGam().Start();

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
So ideally I should put all the content currently in Start() into a some new method like ChoosePokemon() and have that return the chosen pokemon yeah?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Oh yeah I haven't gotten to that yet
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Could you elaborate? Do you mean extracting out that logic to it's own function? Or returning inspectedPokemon from start?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
I also wanted to ask about where to handle the recieved input. We have the validator to mask out completely irrelevant responses, I was thinking of ways to pass in what should happen when each particular choice is picked similarly, but then figured it might be easier to just get back a valid response and run conditional logic on the recieved response. What do you think?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
@Pobiega
public void Start()
{
Console.WriteLine("Welcome to PokeGaam!");
Console.WriteLine("");

while(true)
{
Console.WriteLine("Choose your pokemon!");

Pokemon inspectedPokemon = Input.GetChoiceFromArray("Enter a number to inspect the pokemon", starterPokemonArray);

Console.WriteLine("");
string inspectMessage = inspectedPokemon.GetDetails() + "\n" + "Press 'Y' to select this starter pokemon or 'N' go back.";
ConsoleKeyInfo info = Input.GetKey(inspectMessage, (info) => info.Key == ConsoleKey.Y || info.Key == ConsoleKey.N);

if(info.Key == ConsoleKey.Y)
{
Console.Clear();
Console.WriteLine($"You have selected {inspectedPokemon.Name} as your starter pokemon!");
break;
}
else
{
Console.Clear();
}
}
}
public void Start()
{
Console.WriteLine("Welcome to PokeGaam!");
Console.WriteLine("");

while(true)
{
Console.WriteLine("Choose your pokemon!");

Pokemon inspectedPokemon = Input.GetChoiceFromArray("Enter a number to inspect the pokemon", starterPokemonArray);

Console.WriteLine("");
string inspectMessage = inspectedPokemon.GetDetails() + "\n" + "Press 'Y' to select this starter pokemon or 'N' go back.";
ConsoleKeyInfo info = Input.GetKey(inspectMessage, (info) => info.Key == ConsoleKey.Y || info.Key == ConsoleKey.N);

if(info.Key == ConsoleKey.Y)
{
Console.Clear();
Console.WriteLine($"You have selected {inspectedPokemon.Name} as your starter pokemon!");
break;
}
else
{
Console.Clear();
}
}
}
Input.cs
public static ConsoleKeyInfo GetKey(string prompt, Func<ConsoleKeyInfo, bool>? validator = null, bool clearOnInvalidResponse = true)
{
while(true)
{
Console.WriteLine(prompt);
var key = Console.ReadKey();

if(validator?.Invoke(key) ?? true)
{
return key;
}
else
{
ShowErrorMessage("Invalid input. Please try again.", clearOnInvalidResponse);
}
}
}
public static ConsoleKeyInfo GetKey(string prompt, Func<ConsoleKeyInfo, bool>? validator = null, bool clearOnInvalidResponse = true)
{
while(true)
{
Console.WriteLine(prompt);
var key = Console.ReadKey();

if(validator?.Invoke(key) ?? true)
{
return key;
}
else
{
ShowErrorMessage("Invalid input. Please try again.", clearOnInvalidResponse);
}
}
}
I've refactored my code to be a lot neater thanks to your help. Both the GetChoiceFromArray() and GetKey() methods have a while loop in them, so I feel the urge to get rid of the while loop in Start() but I also have the feeling that it's fine to leave it in. Does the refactored code make sense?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Thank you very much
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
makes sense
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Ah right
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
So i'd have to make my Pokemon class implement IParsable if i want to do something like what you've done with your example?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Looks like it
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
IParsable<T> is a preexisting interface?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
We'd have to pass in a custom validator based on the situation each time we call the function yeah? (In your example)
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
I see, so we make a generalized function that takes in what to ask the user, handles incorrect entries and returns the valid response for any given situation
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
I rarely use while loops and the few times I do, I've developed this habit of just using while(true) to enter the loop and just break; out when i need to, is this bad practice?
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Yeah, that was my main concern, it felt like i was always doing it, and that being inside of nested functions made execution a lot harder to follow
44 replies
CC#
Created by Karto on 10/20/2024 in #help
Need pointers on code for console app.
Hey sorry for the late response! Could you give an example of how you'd extract that behaviour?
44 replies
CC#
Created by Karto on 6/14/2023 in #help
❔ How does the main method work?
Awesome, thanks guys
11 replies