Console.WriteLine("Please, choose your weapon. \n1: Glock 18 \n2:");
switch (choice)
{
case 1:
Glock18 glock18 = new Glock18() ;
break;
}
Console.WriteLine("Let's shoot. Press space to fire and 'r' to reload. ('q' to quit) \n");
while (true)
{
ConsoleKeyInfo input = Console.ReadKey(true);
if (char.IsWhiteSpace(input.KeyChar))
{
pistol.Shoot();
}
}
I have to write a program in which I can select a weapon using a switch-case, but the implementation of the specific weapons differ from one another (A glock sounds different and has a different mag size than a desert eagle for instance)
How can I make sure that the shoot and reload methods for each of the weapons are the ones that are being run? I.e. how to save the chosen gun as a variable so that I can run something like currentWeapon.Shoot
, which will ensure that it is the weapon currently selected, instead of the pistol.Shoot()
method?