In: Computer Science
The code is needed in Java.
Console Example
Welcome to Casino Royal
++++++++++++++++++++++++
How many players are in the game? 2
Enter a player’s name: Rocky
How much money is Rocky willing to put on the table? 200
Enter a player’s name: Donna
How much money is Donna willing to put on the table? 1500
Rocky, you must play three numbers at a time.
The valid numbers are between 0 and 20 inclusive.
Rocky, enter your number? xyz
Error! Invalid integer. Try again.
Rocky, enter your number? 100
Error! Number must be less than 21.
Rocky, enter your number? -10
Error! Number must be greater than -1.
Rocky, enter your number? 0
Rocky, enter your number? 10
Rocky, enter your number? 20
Donna, you must play three numbers at a time.
The valid numbers are between 0 and 20 inclusive.
Donna, enter your number? 1
Donna, enter your number? 2
Donna, enter your number? 3
Rocky, you can now place a single bet of any size on these numbers
as long as you have that much money on the table.
Rocky, how much do you wager? 1.99
Donna, you can now place a single bet of any size on these numbers
as long as you have that much money on the table.
Donna, how much do you wager? 3000
Donna, you did not have $3,000.00 on the table to bet. Try a lower wager.
Donna, how much do you wager? -10
Donna, you cannot wager less than 0 dollars. Try a higher wager.
Donna, how much do you wager? quit
Invalid input. You must enter a number to play. Try again.
Donna, how much do you wager? 100
Very good. Spinning the wheel.....
The winning numbers are: 12, 14, and 5
Sorry Rocky, you lose. You have $198.01 left on the table.
The winning numbers are: 12, 14, and 5
Sorry Donna, you lose. You have $1,400.00 left on the table.
Play Again? (yes/no) y
You must enter yes or no. Try again.
Do you want to continue (yes/no)? nope
You must enter yes or no. Try again.
Do you want to continue (yes/no)? yes
These are the players and their ending balances:
Rocky's balance is $198.01
Donna's balance is $1400.0
Take your money off the table when you leave. Come back soon.
Welcome to Casino Royal
++++++++++++++++++++++++
How many players are in the game? 1
Enter a player’s name: Jane Doe
How much money is Jane willing to put on the table? 9999
Jane, you must play three numbers at a time.
The valid numbers are between 0 and 20 inclusive.
Jane, enter your number? 5
Jane, enter your number? 8
Jane, enter your number? 17
Jane, you can now place a single bet of any size on these numbers
as long as you have that much money on the table.
Jane, how much do you wager? 9999
Very good. Spinning the wheel.....
The winning numbers are: 4, 14, and 9
Sorry Jane, you lose. You have $0.00 left on the table.
Play Again? (yes/no) no
These are the players and their ending balances:
Jane's balance is $0.0
Take your money off the table when you leave. Come back soon.
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;
class Player
{
private String name;
private double amount,wagerAmount;
private int[]guess;
public Player()
{
this.name="";
this.amount=0;
this.guess=new int[3];
this.wagerAmount=0;
}
public Player(String name, double amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public double getWagerAmount() {
return wagerAmount;
}
public int[] getGuess() {
return guess;
}
public void setName(String name) {
this.name = name;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void setWagerAmount(double wagerAmount) {
this.wagerAmount = wagerAmount;
}
public void setGuess(int[] guess) {
this.guess = guess;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner scnr =new Scanner(System.in);
String userChoce="yes";
NumberFormat nf = NumberFormat.getInstance(new Locale("en",
"US"));
int numPlayers;
int winnigNumbers[]=new int[3];
do
{
numPlayers=welcome();
Player players[]=new Player[numPlayers];
for(int i=0;i<numPlayers;i++)
{
players[i]=constructPlayer();
}
for(int i=0;i<numPlayers;i++)
{
System.out.println(players[i].getName()+", you must play three
numbers at a time.\n" +
"\n" +
"The valid numbers are between 0 and 20 inclusive.");
getBattingNumber(players[i]);
}
for(int i=0;i<numPlayers;i++)
{
players[i].setWagerAmount(getWageAmount(scnr,nf,players[i]));
}
System.out.println("Very good. The wheel is spinning.");
for(int i=0;i<3;i++)
winnigNumbers[i]=genrateRandom();
for(int i=0;i<numPlayers;i++)
displayResult(winnigNumbers,nf,players[i]);
userChoce=askContinue(scnr);
}while(userChoce.equalsIgnoreCase("yes"));
System.out.println("\nTake your money off the table when you leave.
Come back soon.");
}
public static int genrateRandom()
{
// create instance of Random class
Random rand = new Random();
int max=10,min=0;
return min + rand.nextInt(max - min + 1);
}
public static void getBattingNumber(Player player)
{
Scanner scnr =new Scanner(System.in);
int battingNum[]=new int[3];
for(int i=0;i<battingNum.length;i++)
{
while(true)
{
System.out.print(player.getName()+", enter your number? ");
try
{
battingNum[i]=scnr.nextInt();
if( battingNum[i]>=0 && battingNum[i]<21)
break;
else if(battingNum[i]>20)
System.out.println("Error! Number must be less than 21.");
else if(battingNum[i]<0)
System.out.println("Error! Number must be greater than -1.");
}
catch(Exception e)
{
System.out.println("Error! Invalid integer. Try again.");
}
}
}
player.setGuess(battingNum);
}
public static int welcome()
{
Scanner input =new Scanner(System.in);
System.out.print("Welcome to Casino Royal\n" +
"\n++++++++++++++++++++++++\n" +
"How many players are in the game? ");
int numPlayers=0;
do
{
try
{
numPlayers=input.nextInt();
if(numPlayers>0)
break;
else
System.out.println("Please enter the positive intger.");
}
catch(Exception e)
{
System.out.println("Error! Invalid integer. Try again.");
}
}while(numPlayers<=0);
return numPlayers;
}
private static Player constructPlayer()
{
String name;
double amount;
Scanner input =new Scanner(System.in);
System.out.print("Enter a player’s name: ");
name=input.next();
System.out.print("How much money is "+name+" willing to put on the
table?");
amount=input.nextDouble();
return new Player(name, amount);
}
private static double getWageAmount(Scanner scnr, NumberFormat
nf, Player player)
{
double amount;
System.out.println(player.getName()+", you can now place a single
bet of any size on these numbers\n" +
"as long as you have that much money on the table.");
while(true)
{
System.out.print(player.getName()+", how much do you
wager?");
try
{
amount=scnr.nextDouble();
if(amount>0 && amount<=player.getAmount())
break;
else if(amount<0)
System.out.println(player.getName()+", you cannot wager less than 0
dollars. Try a higher wager.");
else if(amount>player.getAmount())
System.out.println(player.getName()+", you did not have
$"+nf.format(amount)+" on the table to bet. Try a lower
wager.");
}
catch(Exception e)
{
System.out.println("Invalid input. You must enter a number to play.
Try again.");
}
}
return amount;
}
private static void displayResult(int[] winnigNumbers,
NumberFormat nf, Player player)
{
System.out.print("The winning numbers are: ");
for(int i=0;i<winnigNumbers.length-1;i++)
System.out.print(winnigNumbers[i]+",");
System.out.println(" and
"+winnigNumbers[winnigNumbers.length-1]);
int numMatchig=checkMatching(winnigNumbers,player);
if(numMatchig==0)
{
double
remainingAmount=player.getAmount()-player.getWagerAmount();
player.setAmount(remainingAmount);
System.out.println("Sorry "+player.getName()+", you lose. You have
$"+nf.format(remainingAmount)+" left on the table.");
}
else if(numMatchig==1)
{
double
remainingAmount=player.getAmount()+player.getWagerAmount();
player.setAmount(remainingAmount);
System.out.println("Congratulations you have a winning number. You
now have $"+nf.format(remainingAmount)+" on the table.");
}
else if(numMatchig==2)
{
double
remainingAmount=player.getAmount()+(2*player.getWagerAmount());
player.setAmount(remainingAmount);
System.out.println("Congratulations you have two winning number.
You now have $"+nf.format(remainingAmount)+" on the table.");
}
else if(numMatchig==3)
{
double
remainingAmount=player.getAmount()+(3*player.getWagerAmount());
player.setAmount(remainingAmount);
System.out.println("Congratulations you have all winning number.
You now have $"+nf.format(remainingAmount)+" on the table.");
}
}
private static int checkMatching(int[] winnigNumbers, Player
player)
{
int count=0;
for(int i=0;i<player.getGuess().length;i++)
{
for(int j=0;j<winnigNumbers.length;j++)
if(player.getGuess()[i]==winnigNumbers[j])
{
count++;
break;
}
}
return count;
}
private static String askContinue(Scanner scnr)
{
String ans;
System.out.print("Play Again? (yes/no) ");
ans=scnr.next();
while(true)
{
if(ans.equals("yes") || ans.equals("no"))
return ans;
System.out.println("You must enter yes or no. Try again.");
System.out.print("Do you want to continue (yes/no)?");
ans=scnr.next();
}
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.