anonymous
2012-02-20 20:03:12 UTC
using System;
public class Craps
{
private static Random randomNumbers = new Random();
private enum Status { CONTINUE, WON, LOST }
private enum DiceNames
{
SNAKE_EYES = 2,
TREY = 3,
SEVEN = 7,
YO_LEVEN = 11,
BOX_CARS = 12
}
public static void Main()
{
Status gameStatus = Status.CONTINUE;
int myPoint = 0;
int sumOfDice = RollDice();
switch ((DiceNames)sumOfDice)
{
case DiceNames.SEVEN:
case DiceNames.YO_LEVEN:
gameStatus = Status.WON;
break;
case DiceNames.SNAKE_EYES:
case DiceNames.TREY:
case DiceNames.BOX_CARS:
gameStatus = Status.LOST;
break;
default:
gameStatus = Status.CONTINUE;
myPoint = sumOfDice;
Console.WriteLine("Point is {0}", myPoint);
break;
}
while (gameStatus == Status.CONTINUE)
{
sumOfDice = RollDice();
if (sumOfDice == myPoint)
gameStatus = Status.WON;
else
if (sumOfDice == (int)DiceNames.SEVEN)
gameStatus = Status.LOST;
}
if (gameStatus == Status.WON)
Console.WriteLine("Player Wins\n");
else
Console.WriteLine("Player Loses\n");
}
public static int RollDice()
{
int die1 = randomNumbers.Next(1, 7);
int die2 = randomNumbers.Next(1, 7);
int sum = die1 + die2;
Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);
return sum;
}
} // end
If myPoint is not initialized upon declaration, the compiler says "use of unassigned local variable" in "if (sumOfDice == myPoint)"...
However the while condition is only true if gameStatus == CONTINUE, which means that myPoint was given a value in the default switch statement. So my question is why does it work only when myPoint is initially given a value (doesn't matter what that value is)?
Thanks