I am trying to make a simple shotgun game where the user vs the CPU and the both pick shot, shield or reload but it displays the results I get the error:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
//Declare Variables
int CPUBullets = 3, userBullets = 3;
ShotgunOption UserOption;
ShotgunOption CPUOption;
int userScore = 0;
int CPUscore = 0;
double gameCount = 0.0;
Random computer = new Random();
Console.Clear();
Console.WriteLine("Welcome To The Shotgun Game");
Console.WriteLine("SHOOT RELOAD SHIELD");
do
{
// Prompt User/ Get Random Value From CPU
//Console.Write("Best of 5, Please enter choice: ");
UserOption = GetOptionFromUser();
CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield
switch (UserOption)
{
case ShotgunOption.Shoot:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", UserOption);
; userBullets--; CPUBullets--;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Reload:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", UserOption);
++gameCount; ++CPUscore;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", UserOption);
userBullets++; CPUBullets++; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
case ShotgunOption.Shield:
if ((int)CPUOption == 1)
{
Console.WriteLine("You chose {0} and the computer chose Shoot. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 2)
{
Console.WriteLine("You chose {0} and the computer chose Reload. You win!", UserOption);
++userScore; ++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
else if ((int)CPUOption == 3)
{
Console.WriteLine("You chose {0} and the computer chose Shield. It's a draw!", UserOption);
++gameCount;
Console.ReadLine();
DisplayResults(UserOption, CPUOption, userScore, userBullets, CPUBullets);
}
break;
}
} while (gameCount < 4);
And here's the DisplayResults()
method were the error happens:
static void DisplayResults(ShotgunOption UserOption, ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
{
Console.Clear();
Console.WriteLine("Round Over");
Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
Console.WriteLine("Thanks for playing!");
Console.ReadKey();
}
Your format string is missing an index (specifically, position 2):
Should be: