In: Computer Science
The premise of this project is to make a basic GUI and code for the game Keno. The specifics of the gui and game are below.
Description:
In this project you will implement the popular casino and state lottery game, Keno. This is a somewhat simple game to understand and play which should allow you to focus on learning GUI development in JavaFX and trying your hand at event driven programing.
Implementation Details:
You may add as many classes, data members, interfaces and methods as necessary to implement this program. You may only use JavaFX components for your GUI. You may NOT use Java Swing of Java AWT.
The GUI:
You are welcome to use/discover any widget, pane, node, layout or other in JavaFX to implement your GUI. For this project, you are not allowed to use Scene Builder or FXML layout files. The following elements are required:
1) Your program must start with a welcome screen that is it’s own JavaFX scene. It will consist of: It will have a menu bar at the top with one tab “Menu”. Under “Menu”, you will have the following three options:
• display the rules of the game
• display the odds of winning
• exit the game
***Each of the menu options should be implemented***
It will also have a button that allows the player to start playing. This will change the GUI to the game play screen.
2) The game play screen is its own JavaFX scene. It will consist of:
• The same menu as the welcome screen with an additional menu option: New Look. This option, when implemented, will change the look of the GUI; such as new colors, fonts, images….etc. While there is no minimum for elements to change, the new look must be noticeable to the average user.
• The bet card will be displayed and you must use JavaFX GridPane to implement it. It will be a 8X10 grid of clickable Nodes (Buttons, ImageViews, etc.). Each of the Nodes should display the number it represents (1-80).
• The Nodes in the GridPane should be disabled until the player decides on how many spots they want to play.
• There must be a way for the player to pick how many spots to play(1,4,8 or 10). This can not change once the drawings begin.
• There must be a way for the player to pick how many drawings they will play their bet card for (minimum of 1 and maximum of 4). This can not change once the drawings begin.
• Once the player decides on how many spots they want to play, the Nodes of the GridPane should be enabled to allow the user to choose their numbers. The user should not be able to select duplicate numbers or select more spots than they decided originally.
• Once a number is selected on the bet card, it should show that it has been chosen. Players can edit their choices as often as they want before the drawings occur. They can not change once the drawings begin.
• There should be a way that the player can select to have their numbers chosen automatically and randomly for them if they don’t want to choose themselves.
• It is up to you to ensure that all input is correct and no illegal selections, choices or options have been made.
• When the number of spots has been chosen, bet card filled and number of drawings selected the user should have a way to start the first drawing.
• The drawing will display 20, randomly selected numbers (1-80) with no duplicates, one by one with a pause in between selection. How they are displayed is up to you. After each drawing, the user should be able to see how many numbers they matched, which numbers they matched and how much they won in that particular drawing. They should also be able to see what they have won since the program was started.
• There should be a way for the player to decide to continue to the next drawing.
• When all drawings are complete, the user should be able to fill out a new bet card, number of spots and number of drawings or exit the program.
Playing the game in your Program:
Your game must play and feel like the user is actually playing in real time. You must include pause transitions or add buttons like “continue” to control the flow of the game. If you did not, the program would move too fast and not allow the user to understand what is happening. You must also provide ways to prompt the player as to what they should do next. It will not always be obvious to someone using your software what they are supposed to do. It is also up to you to ensure that all input is correct and no illegal selections, choices or options have been made.
This is the sample code given:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class JavaFXTemplate extends Application { public static void main(String[] args) { // TODO Auto-generated method stub launch(args); } //feel free to remove the starter code from this method @Override public void start(Stage primaryStage) throws Exception { // TODO Auto-generated method stub primaryStage.setTitle("Welcome to JavaFX"); Scene scene = new Scene(new VBox(), 700,700); primaryStage.setScene(scene); primaryStage.show(); } }
import java.util.Scanner; //used to get input in the game
import java.util.Random; //used to generate random numbers by the
computer
public class kenoTest
{
public static void main(String[] args)
{
Scanner input =
new Scanner(System.in);
double
playerMoney = 100; //Start the player off with some money
int playerNums[]
= new int[15];
int
computerNums[] = new int[20];
int kenoSpot,
kenoCatch;
double
bet;
boolean
continueGame = true;
String
userInput;
System.out.println("Hello, and welcome to the game Keno!");
System.out.println("This is a high stakes game. You can win and
lose money quickly!");
while(continueGame)
{
System.out.println("You currently have: $" +
playerMoney);
System.out.println("Let's get some numbers to
begin.");
System.out.println("You may enter up to 15
numbers");
playerNums = getUserInput();
bet = getBet(playerMoney);
computerNums = getComputerNums();
kenoSpot = getSpot(playerNums);
kenoCatch = getCatch(playerNums,
computerNums);
System.out.println("Catch: " + (kenoCatch +
1));
playerMoney += payout(kenoSpot, kenoCatch,
bet);
playerMoney -= bet;
System.out.println("You now have: $" +
playerMoney);
if (playerMoney <= 0)
{
continueGame = false;
System.out.println("Sorry,
you ran out of money!");
System.out.println("Better
luck next time! :)");
}
else
{
System.out.println("Would you
like to continue?");
userInput =
input.nextLine();
if ((userInput.equals("y"))||
userInput.equals("yes"))
{
continueGame = true;
}
else
{
continueGame = false;
}
}
}
System.out.println("Thanks for playing!");
System.out.println("Overall, you now have: $" + playerMoney);
}
/*
getUserInput function
@return array userInput
the array userInput is all of the
numbers that the user entered
this function initally sets all the
numbers in the array to 0.
It checks to make sure the user has
not inputted the number before
and also that the number entered is
greater than 0 and less than 80
*/
public static int[] getUserInput()
{
Scanner input = new
Scanner(System.in); // for input
boolean invalidInput = true;
boolean continuePlayer =
true;
int playerNums[] = new
int[15];
int index;
int numberEntered;
String
enterString;
for (index = 0; index <
playerNums.length; index++ )
{
playerNums[index] = 0;
}
index = 0;
while(continuePlayer &&
index < 15)
{
do
{
do
{
System.out.println("Enter
number " + (index+1));
numberEntered =
input.nextInt();
if ((numberEntered > 0)
&& (numberEntered < 81))
{
if(isUnique(numberEntered, playerNums))
{
invalidInput = false;
playerNums[index] = numberEntered;
}
else
{
System.out.println("Sorry, you already entered
that number before!");
System.out.println("Try again!
");
invalidInput = true;
}
}
else
{
invalidInput = true;
System.out.println("Sorry, the number you entered is either less
than 0 or greater than 80");
System.out.println("Try again");
System.out.println("");
}
} while(invalidInput);
if(index < 14) //makes sure program doesn't
ask to continue when on the last number
{
System.out.println("Do you
wish to continue? (yes/no)");
input.nextLine();
enterString =
input.nextLine();
if ((enterString.equals("n"))
|| (enterString.equals("no")))
{
invalidInput = false;
continuePlayer = false;
}
else if
((enterString.equals("y")) || (enterString.equals("yes")))
{
invalidInput = false;
continuePlayer = true;
}
else
{
System.out.println("Sorry, I didn't understand that.");
System.out.println("");
invalidInput = true;
}
}
}while(invalidInput);
index++;
}
return
playerNums;
}
/*
isUnique function
@return boolean isUnique
@params number and array
Returns whether the number is
unique to the array or not
if it is, it will return true, if
it is not then it will return false
Used to ensure that there are no
repeats in both playerNums and computerNums
*/
public static boolean isUnique(int number, int[]
array)
{
int index;
for (index = 0; index <
array.length; index++)
{
if(number ==
array[index])
{
return false;
}
}
return true;
}
/*
getComputerNums function
@return array computerNums
This function generates the numbers
for the computer
The numbers are generated randomly.
Follows same rules as user input
but array has 20 instead of 15
numbers.
*/
public static int[] getComputerNums()
{
int[] computerNums = new
int[20];
Random rand = new Random();
int index;
int randomNumber = 0;
for (index = 0; index <
computerNums.length; index++)
{
randomNumber =
rand.nextInt(80) + 1;
if(isUnique(randomNumber, computerNums))
{
computerNums[index] = randomNumber;
}
else
{
index--;
}
}
return computerNums;
}
/*
getSpot function
@return spot
@params playerArray
The getSpot function returns the
spot, which is
the number of numbers the user
initially picked
it makes spot go up when it doesn't
find a 0.
A 0 shows that the user did not
enter a number for that index
*/
public static int getSpot(int[] playerArray)
{
int index;
int spot = 0;
for (index = 0; index <
playerArray.length; index++)
{
if(playerArray[index] != 0)
{
spot++;
}
}
spot -= 1; //Subtract one so it
works properly in array
return spot;
}
/*
getBet function
@return bet
@param playerMoney
This function gets the bet from the
user.
It makes sure that the best is
greater than or equal to 0
It also makes sure that the user
has enough money to bet
*/
public static double getBet(double
playerMoney)
{
Scanner input = new
Scanner(System.in);
double bet = 0;
boolean invalidInput = true;
while(invalidInput)
{
System.out.print("Enter the bet amount in whole dollars: $");
bet =
input.nextInt();
if(bet <
0)
{
System.out.println("Bet amount can't be less
than 0!");
invalidInput = true;
}
else if(bet >
playerMoney)
{
System.out.println("You don't have enough money
to bet that!");
invalidInput = true;
}
else
{
invalidInput = false;
}
}
return bet;
}
/*
getCatch function
@return catch
@params arrays playerArray and
computerArray
This function returns the catch
which is
how many of the user's numbers
matched the computer's.
*/
public static int getCatch(int[] playerArray, int[]
computerArray)
{
int playerIndex;
int computerIndex;
int kenoCatch = 0;
for (playerIndex = 0; playerIndex
< playerArray.length; playerIndex++)
{
for(computerIndex = 0; computerIndex < computerArray.length;
computerIndex++)
{
if(playerArray[playerIndex] ==
computerArray[computerIndex])
{
kenoCatch++;
}
}
}
kenoCatch -= 1;//subtact one so it
works in array
return kenoCatch;
}
/*
payout function
@return payoutAmount
@param spot and catch
This function will return the total
amount that should be multiplied
by the bet amount. For the payout
array, each line is a spot and
each number within the array is the
catch
*/
public static double payout(int kenoSpot, int
kenoCatch, double betAmount)
{
double payoutAmount = 0;
double multiplier;
double payout[][] =
{
{3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //1
{1, 9, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //2
{1, 2, 16, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //3
{0.5, 2, 6, 12,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //4
{0.5, 1, 3, 15,
50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//5
{0.5, 1, 2, 3,
30, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0},//6
{0.5, 0.5, 1, 6,
12, 36, 100, 0, 0, 0, 0, 0, 0, 0, 0},//7
{0.5, 0.5, 1, 3,
6, 19, 90, 720, 0, 0, 0, 0, 0, 0, 0},//8
{0.5, 0.5, 1, 2,
4, 8, 20, 80, 1200, 0, 0, 0, 0, 0, 0},//9
{0, 0.5, 1, 2,
3, 5, 10, 30, 600, 1800, 0, 0, 0, 0, 0},//10
{0, 0.5, 1, 1,
2, 6, 15, 25, 180, 1000, 3000, 0, 0, 0, 0},//11
{0, 0, 0.5, 1,
2, 4, 24, 72, 250, 500, 2000, 4000, 0, 0, 0},//12
{0, 0, 0.5, 0.5,
3, 4, 5, 20, 80, 240, 500, 3000, 6000, 0, 0},//13
{0, 0, 0.5, 0.5,
2, 3, 5, 12, 50, 150, 500, 1000, 2000, 7500, 0},//14
{0, 0, 0.5, 0.5,
1, 2, 5, 15, 50, 150, 300, 600, 1200, 2500, 10000}//15
};
if(kenoCatch < 0)
{
multiplier =
0;
}
else
{
multiplier =
payout[kenoSpot][kenoCatch];
payoutAmount =
multiplier * betAmount;
}
return payoutAmount;
}
}