Java Programming Assignment 6

Requirement

Problem:

  • You will be writing a program to play a Keno game (Program 6 will be a non-graphic version, Program 7 will be a graphic version).
  • Keno is a game that has a "game board" of 80 numbers on which 20 numbers are "drawn" randomly. The player(s) may choose 1 to 15 numbers of the possible 80 numbers (to simplify this program, there are no other ways to play). You will need several classes and an interface (specified below).

Solution

public class HW6_Spot 
{

private int value = 0;

private boolean chosen = false;


public HW6_Spot()

{

value = 0; // not needed but in case

chosen = false;

}


public HW6_Spot(int i_value)

{

value = (i_value > 0)? i_value : value; // not needed but in case

}


public int getValue()

{

return value;

}


public boolean getChosen()

{

return chosen;

}


public void setChosen(boolean b_Chosen)

{

chosen = b_Chosen;

}

}
/* Write an interface called NumberGameInterface which has only abstract public instance methods called 

drawNumbers (no parameters, no return value),

getResults (no parameters, no return value) and displayResults (no parameters, no return value).

*/
public interface HW6_NumberGameInterface 
{

public abstract void drawNumbers();

public abstract void getResults();

public abstract void displayResults();

}
public class HW6_KenoBoard 
{

public static int MAX_KENO_NUMBER = 80;

public static int DRAWN_NUMBERS = 20;


private HW6_Spot [] kenoSpots;

private HW6_Spot [] drawnSpots;


// a constructor (no parameters)-- allocate memory for the kenoSpots (of size MAX_KENO_NUMBER+1) , allocate memory for the drawnSpots array (of size DRAWN_NUMBERS). Initialize each element in the kenoSpots array to an instance of a Spot (pass the array index to the constructor). Note: MAX_KENO_NUMBER and DRAWN_NUMBERS are declared as static in the KenoGame class

public HW6_KenoBoard()

{

kenoSpots = new HW6_Spot[MAX_KENO_NUMBER + 1];

drawnSpots = new HW6_Spot[DRAWN_NUMBERS];


for(int i=1; i < kenoSpots.length; i++)

{

kenoSpots[i] = new HW6_Spot(i);

}

}


// a method that resets the boolean variable in each Spot (OR only the ones that were set, using drawnSpots) of kenoSpots to false, and then resets each element of the drawnSpots array to null.

public void resetKenoBoard()

{

for(int i = 1; i < kenoSpots.length; i++) // Note: Keno Spot index starts from 1, 0th index is null

kenoSpots[i].setChosen(false);


for(int i = 0; i < drawnSpots.length; i++)

drawnSpots[i] = null;

}


// an accessor (with an int parameter, Spot return value) for ONE of the kenoSpots with the index of the int pararmeter-- be sure that the int parameter is in range (check using KenoGame's static MAX_KENO_NUMBER variable) -- if in range, return the corresponding Spot, if not, return null

public HW6_Spot getKenoSpot(int i_Spot)

{

if(i_Spot > 0 && i_Spot <= MAX_KENO_NUMBER)

return kenoSpots[i_Spot];

else

return null;

}


// a method that will randomly choose Spots in kenoSpots by doing the following for each drawnSpots element:

public void createDrawnSpots()

{

int i_RandomKenoIndex;

for (int i = 0; i < drawnSpots.length; i++)

{

// formula to get random number: (int)(Math.random() * (MAX-MIN+1) ) + MIN;

i_RandomKenoIndex = (int)(Math.random() * (MAX_KENO_NUMBER - 1 + 1) ) + 1;

if(kenoSpots[i_RandomKenoIndex].getChosen())

i--; // already chosen than loop one more time, do it silently without any warning

else

{

drawnSpots[i] = new HW6_Spot(kenoSpots[i_RandomKenoIndex].getValue());

kenoSpots[i_RandomKenoIndex].setChosen(true);

}

}

}


// toString (overriding Object's toString) that will return a string with the value of each Spot in the drawnSpots array

public String toString()

{

String s_drawnSpots = "";

for(int i = 0; i < drawnSpots.length; i++)

{

s_drawnSpots = s_drawnSpots + drawnSpots[i].getValue() + " ";

}

return s_drawnSpots;

}

}
public class HW6_KenoGame implements HW6_NumberGameInterface
{

public static final int MAX_KENO_NUMBER = 80;

public static final int DRAWN_NUMBERS = 15;


private HW6_KenoBoard oKenoBoard;

private HW6_KenoPlayer oKenoPlayer;

public static double [][] PAYOFF = {{0},

{0, 2.75},

{0, 1., 5.},

{0, 0, 2.50, 25.00},

{0, 0, 1.0, 5.0, 80.0},

{0, 0, 0, 2.0, 10.0, 600.0},

{0, 0, 0, 1.0, 8.0, 50., 1499.0},

{0, 0, 0, 0, 5.0, 10.0, 250., 1500.0},

{0, 0, 0, 0, 4.00, 8.00, 40.00, 400.00, 10000.00},

{0, 0, 0, 0, 2.00, 5.00, 20.00, 80.00, 2500.00, 15000.00},

{0, 0, 0, 0, 0, 2.00, 30.00, 100.00, 500.00, 3000.00, 17500.00},

{0, 0, 0, 0, 0, 2.00, 15.00, 50.00, 80.00, 800.00, 8000.00, 27500.00},

{0, 0, 0, 0, 0, 1.00, 5.00, 30.00, 90.00, 500.00, 2500.00, 15000.00, 30000.00},

{0, 0, 0, 0, 0, 1.00, 2.00, 10.00, 50.00, 500.00, 1000.00, 10000.00, 15000.00, 30000.00},

{0, 0, 0, 0, 0, 0, 2.00, 8.00, 32.00, 300.00, 800.00, 2500.00, 12000.00, 18000.00, 30000.00},

{0, 0, 0, 0, 0, 0, 1.00, 7.00, 21.00, 100.00, 400.00, 2000.00, 8000.00, 12000.00, 25000.00, 30000.}

};


// default constructor that instantiates a KenoBoard (and assigns to the KenoBoard instance variable)

public HW6_KenoGame()

{

oKenoBoard = new HW6_KenoBoard();

}


// a method (2 int parameters) to instantiate a new KenoPlayer passing the int parameters (and assign to the instance variable)

public void createKenoPlayer(int i_numOfSpots, int i_amountOfBet)

{

oKenoPlayer = new HW6_KenoPlayer(i_numOfSpots, i_amountOfBet);

}


// a method with an int parameter to set ONE Spot in the KenoPlayer's (if the KenoPlayer variable is not null) markedSpots array, but check the int parameter first if it's in range (between 1 and KenoBoard's static MAX_KENO_NUMBER) before passing the Spot in the KenoBoard (use the KenoBoard's accessor for one Spot) -- return -2 if the KenoPlayer hasn't been instantiated yet, -1 if the int parameter is out of range, 1 if the KenoPlayer's method returns false, 0 if it was assigned OK

public int setUserDrawnSpot(int i_Spot)

{

if(oKenoPlayer != null)

{

if(i_Spot > 0 && i_Spot <= HW6_KenoBoard.MAX_KENO_NUMBER)

{

//HW6_Spot oHW6_Spot = new HW6_Spot(i_Spot);

//oHW6_Spot.setChosen(true);

if(oKenoPlayer.setSpot(oKenoBoard.getKenoSpot(i_Spot)))

return 0; // -- 0 if it was assigned OK

else

return 1; // -- 1 if the KenoPlayer's method returns false

}

else

return -1; // -- -1 if the int parameter is out of range

}

else

return -2; // -- return -2 if the KenoPlayer hasn't been instantiated yet

}


// a method to return the length of the KenoPlayer's markedSpots array

public int getLenOfMarkedSpots()

{

return oKenoPlayer.getLengthOfMarkedSports();

}


// a method to override the interface method drawNumbers (no parameters) that calling the KenoBoard methods to reset its variables and the method to assign "randomly" to the drawnArray

public void drawNumbers()

{

oKenoBoard.resetKenoBoard();

oKenoBoard.createDrawnSpots();

}


// a method to override the interface method getResults that calls the KenoPlayer's methods to calculate how many matched and calculate its winnings

public void getResults()

{

oKenoPlayer.calcNumMatches();

oKenoPlayer.calcWinnings();

}


// a method to override the interface method displayResults that will display the KenoBoard's toString and the KenoPlayer's toString to System.out.

public void displayResults()

{

System.out.println(oKenoBoard.toString());

System.out.println(oKenoPlayer.toString());

}

}
// Write a class called KenoPlayer to represent a player's chosen numbers for a Keno Game.
public class HW6_KenoPlayer 
{

private HW6_Spot [] markedSpots;

private int currentIndex = 0;

private int numOfMatches = 0;

private int amountOfBet = 1;

private double amountWin = 0.00;


// To Do: constructor with int for the number of spots to choose, and an int for the bet as parameters-- initialize the appropriate instance variables to the parameters (make sure to assign only if > 0 for numbers, and the number of spots < length of the payoff array in the KenoGame class), and allocate memory for the markedSpots array, using the parameter for the number of spots (or 1 if the parameter is out of range)

public HW6_KenoPlayer(int i_numOfSpots, int i_amountOfBet)

{

if(i_numOfSpots > 0 && i_numOfSpots < HW6_KenoGame.PAYOFF.length)

markedSpots = new HW6_Spot[i_numOfSpots];

else

markedSpots = new HW6_Spot[1];


amountOfBet = (i_amountOfBet > 0)? i_amountOfBet : amountOfBet;

}


// accessors (one for the length of the markedSpots array, one for the bet, one for the number of matches, and one for the winnings)

public int getLengthOfMarkedSports()

{

return markedSpots.length;

}


public int getAmountOfBet()

{

return amountOfBet;

}


public int geNumOfMatches()

{

return numOfMatches;

}


public double getAmountWin()

{

return amountWin;

}


// a method called setSpot (with a Spot parameter and boolean return value) that assigns to the next element in this' markedSpots array, the Spot parameter-- but check if the parameter isn't null and the currentIndex < length of the array AND/OR check each element in this' markedSpots array so far (that the parameter's not already there). If it doesn't, assign, increment the current index and return true, otherwise return false.

public boolean setSpot(HW6_Spot oHW6_Spot)

{

if(currentIndex < markedSpots.length && oHW6_Spot != null)

{

for (int i = 0; i < markedSpots.length; i++)

{

if(markedSpots[i] != null)

{

if(markedSpots[i].getValue() == oHW6_Spot.getValue()) // Spot comparison

return false;

}

}


markedSpots[currentIndex] = oHW6_Spot;

currentIndex ++;

return true;

}

else

{

return false;

}

}


// a method called calcNumMatches that counts how many elements in this KenoPlayer's markedSpots that were chosen (check each element's chosen variable) and assign to your instance variable for number of matches

public void calcNumMatches()

{

numOfMatches = 0; // reset before calculate in case

for(int i = 0; i < markedSpots.length; i++)

{

if(markedSpots[i].getChosen())

numOfMatches++;

}

}


// To Do: a method called calcWinnings (no parameters) that will access the PAYOFF static 2-dim. array in the KenoGame to find and assign the appropriate array element (multiplied by your bet), rounded to the nearest int to your instance variable for amount you won.

public void calcWinnings()

{

amountWin = Math.round(HW6_KenoGame.PAYOFF[markedSpots.length][numOfMatches] * amountOfBet);

}


// toString (overriding Object's toString) that will return a string with a label (like "Player's spots:"), each of the Spot's int values in the markedSpots array (make sure it's not null) , number of matches, bet, and winnings

public String toString()

{

// ex: Player's spots: 20 30 40 50, Player matched 2 spot(s), bet $10 and won $10

String s_PlayerSpot = "";

for(int i = 0; i < markedSpots.length; i++)

{

s_PlayerSpot = s_PlayerSpot + markedSpots[i].getValue() + " ";

}

return String.format("Player's spots: %s \nPlayer matched %d spot(s), bet $%d and won $%.2f", s_PlayerSpot, numOfMatches, amountOfBet, amountWin);

}

}

HW6_KenoGameDriver

/*

Name of program: Keno Game without GUI

Programmer's name: ABC

Current Date: dd/mm/yyyy

Computer system and compiler you are using: Win 8, JDK 1.7

Brief description of the program (1-5 sentences): Play Keno game

Variable names (if any) and descriptions of what they represent:

*/
import java.util.Scanner;
public class HW6_KenoGameDriver 
{

static Scanner oScanner = new Scanner(System.in);


public static void main(String[] args)

{

HW6_KenoGame oKenoGame = new HW6_KenoGame();


boolean b_IsGameContinue = true; // determine if user want to repeat

while(b_IsGameContinue)

{

oKenoGame.drawNumbers();

getKenoGameUserChoices(oKenoGame);

oKenoGame.getResults();

oKenoGame.displayResults();

b_IsGameContinue = isGameContinue(); // Ask if user wants to repeat

//oKenoGame.drawNumbers();

}

}


public static void getKenoGameUserChoices(HW6_KenoGame oKenoGame)

{

int i_NumOfSpotToChose = 0;

int i_amountOfBet = 0;

while (i_NumOfSpotToChose < 1 || i_NumOfSpotToChose >= HW6_KenoGame.PAYOFF.length)

{

System.out.print("How many spots do you want to choose? (max. " + HW6_KenoGame.DRAWN_NUMBERS + "): ");

i_NumOfSpotToChose = oScanner.nextInt();

// if (i_NumOfSpotToChose < 1 || i_NumOfSpotToChose >= HW6_KenoGame.PAYOFF.length) // display error message

}

while(i_amountOfBet <= 0)

{

System.out.print("What is your bet (at least $1): ");

i_amountOfBet = oScanner.nextInt();

}


oKenoGame.createKenoPlayer(i_NumOfSpotToChose, i_amountOfBet);

int i_Counter = 0;

while (i_Counter < i_NumOfSpotToChose)

{

System.out.print(String.format("Enter number (1-%d) #%d: ", HW6_KenoGame.MAX_KENO_NUMBER, (i_Counter+1)));

switch(oKenoGame.setUserDrawnSpot(oScanner.nextInt()))

{

case -1:

System.out.println(String.format("Number is out of range (1-%d)", HW6_KenoGame.MAX_KENO_NUMBER));

break;

case 1:

System.out.println("You already chose that number!");

break;

case 0:

i_Counter++;

break;

default:

break;

}

}

}


// Ask if user wants to repeat --> Copied this method from HW2

public static boolean isGameContinue()

{

boolean isGameContinue;

System.out.print("Play another game? (y for yes): ");

String s_UserInput = oScanner.next();

if(s_UserInput != null && ! s_UserInput.isEmpty())

{

System.out.println(s_UserInput.substring(0, 0).toUpperCase());

isGameContinue = (s_UserInput.toUpperCase().charAt(0) == 'Y')? true : false;

}

else

isGameContinue = false;


return isGameContinue;

}

}
/*
Output:
How many spots do you want to choose? (max. 15): 4
What is your bet (at least $1): 10
Enter number (1-80) #1: 20
Enter number (1-80) #2: 30
Enter number (1-80) #3: 40
Enter number (1-80) #4: 10
76 15 69 21 43 27 8 62 65 4 50 5 68 44 57 17 54 2 31 52 
Player's spots: 20 30 40 10  
Player matched 0 spot(s), bet $10 and won $0.00
Play another game? (y for yes): yeah
How many spots do you want to choose? (max. 15): 2
What is your bet (at least $1): 100
Enter number (1-80) #1: 100
Number is out of range (1-80)
Enter number (1-80) #1: 0
Number is out of range (1-80)
Enter number (1-80) #1: -1
Number is out of range (1-80)
Enter number (1-80) #1: 33
Enter number (1-80) #2: 200
Number is out of range (1-80)
Enter number (1-80) #2: 33
You already chose that number!
Enter number (1-80) #2: 300
Number is out of range (1-80)
Enter number (1-80) #2: 33
You already chose that number!
Enter number (1-80) #2: 44
16 57 19 40 80 26 42 20 6 35 70 76 68 27 8 18 1 7 3 17 
Player's spots: 33 44  
Player matched 0 spot(s), bet $100 and won $0.00
Play another game? (y for yes): yes
How many spots do you want to choose? (max. 15): 20
How many spots do you want to choose? (max. 15): 2
What is your bet (at least $1): -5
What is your bet (at least $1): 50
Enter number (1-80) #1: 10
Enter number (1-80) #2: 20
16 17 65 23 57 52 38 30 48 73 47 44 27 53 35 76 22 8 63 66 
Player's spots: 10 20  
Player matched 0 spot(s), bet $50 and won $0.00
Play another game? (y for yes): YYYYY
How many spots do you want to choose? (max. 15): 5
What is your bet (at least $1): 80
Enter number (1-80) #1: 1
Enter number (1-80) #2: 70
Enter number (1-80) #3: 10
Enter number (1-80) #4: 1
You already chose that number!
Enter number (1-80) #4: 10
You already chose that number!
Enter number (1-80) #4: 60
Enter number (1-80) #5: 80
19 18 17 54 33 49 38 31 67 78 29 11 75 22 46 79 37 69 12 23 
Player's spots: 1 70 10 60 80  
Player matched 0 spot(s), bet $80 and won $0.00
Play another game? (y for yes): yes
How many spots do you want to choose? (max. 15): 4
What is your bet (at least $1): 22
Enter number (1-80) #1: 44
Enter number (1-80) #2: 66
Enter number (1-80) #3: 77
Enter number (1-80) #4: 22
53 60 25 5 7 32 75 68 73 65 42 2 58 15 36 14 35 20 13 6 
Player's spots: 44 66 77 22  
Player matched 0 spot(s), bet $22 and won $0.00
Play another game? (y for yes): yes
How many spots do you want to choose? (max. 15): 8
What is your bet (at least $1): 15
Enter number (1-80) #1: 10
Enter number (1-80) #2: 20
Enter number (1-80) #3: 30
Enter number (1-80) #4: 40
Enter number (1-80) #5: 50
Enter number (1-80) #6: 60
Enter number (1-80) #7: 70
Enter number (1-80) #8: 80
15 19 7 26 49 58 6 5 48 68 28 12 16 73 47 36 23 17 50 22 
Player's spots: 10 20 30 40 50 60 70 80  
Player matched 1 spot(s), bet $15 and won $0.00
Play another game? (y for yes): no
*/