Skip to main content

Validation (with Conditions)

Before acting on user input, we should check that it isn't trash. This is known as "input validation".

Example: Pokemon RPG​

Let's build a simple console RPG inspired by Pokemon.

pokemans

Each turn, the player picks one action:

  1. Fight: act with your current pokemon
  2. Pokemon: switch out your current pokemon
  3. Bag: use an item
  4. Run: attempt to flee

Planning Phase​

Before writing any code, it helps to draft the decision tree, so we know each branch's condition and outcome up front:

Coding Phase​

First, we must setup our variables:

string pokemon = "ditto";
string enemy = "magikarp";
string input;
int choice;

Next, we must prompt and read input:

Console.WriteLine($"""
What will {pokemon} do?

1. Fight
2. Pokemon
3. Bag
4. Run

""");

input = Console.ReadLine(); // users can technically put ANY string here

Since input could be ANYTHING, we must validate it before using it:

// if the input is not a valid, we want to to let them know
if(input != "1" && input != "2" && input != "3" && input != "4")
{
Console.WriteLine("❌ That's not a valid choice.");
}

Only once we know input is one of "1"–"4" is it safe to parse it as an integer:

// ... if statement from above
else
{
choice = int.Parse(input);

// handle choice 1, 2, 3, or 4
}

The parse has to live in a nested block because there's no way to fold "is valid" and "now use the value" into one && condition.

Therefore, inside the else, sketch each choice as a plain comment before writing the C#. This gives us a blueprint to code with:

// ... if statement from above
else
{
choice = int.Parse(input);

// 1. only dittos can transform if they choose to fight
// 2. any pokemon can be called back to be replaced by another one
// 3. any player can open their bag to see their inventory
// 4. We can only run from a pokemon that is exceptionally week
// ? Nothing happens otherwise lol
}

Once the blueprint makes sense to you, translate that into C#.

// 1. only dittos can transform if they choose to fight
if (choice == 1 && pokemon == "ditto")
{
Console.WriteLine($"{pokemon} transformed into {enemy}");
}
// 2. any pokemon can be called back to be replaced by another one
else if (choice == 2)
{
Console.WriteLine($"You called back {pokemon}");
pokemon = "pikachu";
Console.WriteLine($"Go! {pokemon}!");
}
// 3. any player can open their bag to see their inventory
else if (choice == 3)
{
Console.WriteLine($"""
1. Poke Ball x2
2. Great Ball x1
3. Close Bag
""");
}
// 4. We can only run from a pokemon that is exceptionally week
else if (choice == 4 && enemy == "magikarp")
{
Console.WriteLine("Got away safely!");
}
// ? Nothing happens otherwise lol
else
{
Console.WriteLine("Nothing happens...");
}

Clean up comments once the logic is ironed out in C#, and leave TODOs for known gaps (most IDEs highlight these differently from regular comments):

if (choice == 1 && pokemon == "ditto")
{
Console.WriteLine($"{pokemon} transformed into {enemy}");
}
else if (choice == 2)
{
Console.WriteLine($"You called back {pokemon}");
pokemon = "pikachu"; // TODO - maybe this should be defined by the player
Console.WriteLine($"Go! {pokemon}!");
}
else if (choice == 3)
{
// TODO - Maybe these should be stored in variables instead?
Console.WriteLine($"""
1. Poke Ball x2
2. Great Ball x1
3. Close Bag
""");
// TODO - we should probably manage this action as well
}
else if (choice == 4 && enemy == "magikarp")
{
Console.WriteLine("Got away safely!");
}
else
{
Console.WriteLine("Nothing happens...");
}

All together​

string pokemon = "ditto";
string enemy = "magikarp";
int choice;
string input;

Console.WriteLine($"""
What will {pokemon} do?

1. Fight
2. Pokemon
3. Bag
4. Run

""");

input = Console.ReadLine();

if(input != "1" && input != "2" && input != "3" && input != "4")
{
Console.WriteLine("❌ That's not a valid choice.");
}
else
{
choice = int.Parse(input);

if (choice == 1 && pokemon == "ditto")
{
Console.WriteLine($"{pokemon} transformed into {enemy}");
}
else if (choice == 2)
{
Console.WriteLine($"You called back {pokemon}");
pokemon = "pikachu"; // TODO - maybe this should be defined by the player
Console.WriteLine($"Go! {pokemon}!");
}
else if (choice == 3)
{
// TODO - Maybe these should be stored in variables instead?
Console.WriteLine($"""
1. Poke Ball x2
2. Great Ball x1
3. Close Bag
""");
// TODO - we should probably manage this action as well
}
else if (choice == 4 && enemy == "magikarp")
{
Console.WriteLine("Got away safely!");
}
else
{
Console.WriteLine("Nothing happens...");
}
}