In: Computer Science
Hello, this is for C#. Initialize variable balance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to balance, and if it’s not, have the user reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase balance by wager and display the new balance. If the player loses, decrease balance by wager, display the new balance, check whether balance has become zero and, if so, display the message “Sorry. You busted!”
Hi,
please find the below code which will fulfill your requirement.
using System.IO;
using System;
class Program
{
public static int Balance = 1000;
public bool WagerCheck(int balance,int wager)
{ return wager<= balance;
}
static void Main()
{
Program p = new Program();
Console.WriteLine("Enter wager:");// Prompt
loop: string str = Console.ReadLine();
int value;
if (int.TryParse(str, out value)) // Try to parse the string as an
integer
{
if (p.WagerCheck(Balance,value))
{
CRAPGame c = new CRAPGame();
string result =c.Play();
if(result=="win")
{
Console.WriteLine(" you win !!!, balance:"+(Balance+value));
}
else
{Console.WriteLine(" you lost !!!,
balance:"+(Balance-value));}
}
else
{ Console.WriteLine("Please Enter wager less then or equal to your
balance:");
goto loop;
}
}
else
{
Console.WriteLine("Not an integer!");
}
}
}
public class CRAPGame
{
//Creating a random number generator for use in methd
RollDice
private Random randomNumber = new Random();
private enum Status //ENUM WITH CONSTATNT THAT REPRESENT GAME
STATUS
{
CONTINUE,
WON,
LOST
}
private enum DiceNames
{
SNAKE_EYES = 2,
TREY = 3,
SEVEN = 7,
YO_LEVEN = 11,
BOX_CARS = 12
}
// PLAY ONE GAME OF CRAPGame
public string Play()
{
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.BOX_CARS:
case DiceNames.SNAKE_EYES:
case DiceNames.TREY:
gameStatus = Status.LOST;
break;
default:
gameStatus= Status.CONTINUE;
myPoint = sumOfDice;
Console.WriteLine("Point is {0}", myPoint);
break;
}
while (gameStatus == Status.CONTINUE)
{
sumOfDice = RollDice(); // roll dice again
if (sumOfDice == myPoint)
gameStatus = Status.WON;
if (sumOfDice == (int)DiceNames.SEVEN)
gameStatus = Status.LOST;
}
// end of while method
// Display won or loss
if (gameStatus == Status.WON)
{Console.WriteLine("Player Wins");
return ("win");}
else
{return ("loss");}
Console.WriteLine("Player Losses");
}//end of method Play
public int RollDice()
{
int die1 = randomNumber.Next(1, 7);
int die2 = randomNumber.Next(1, 7);
int sum = die1 + die2;
//Display result of this roll
Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2,
sum);
return sum;
}// End method roll dice
}