Java Project This project provides an opportunity to practice object oriented pr
ID: 3711243 • Letter: J
Question
Java Project
This project provides an opportunity to practice object oriented programming. The intent of this project is to create an object model of a bingo game, and implement all the classes needed to simulate a collection of players at a bingo competition.
The Bingo Game Overview
The first item that needs to be modeled is the bingo cage which contains 75 bingo balls, each of which has a letter and number (ie. B15). As the game is being played, a random bingo ball is removed from the cage and that bingo ball’s letter and number are the next value to be played. To begin a new game, all the bingo balls are placed in the cage.
The second item that needs to be modeled is the bingo card which contains 5 columns of 5 numbers. The 5 columns correspond to the letters B, I, N, G, O. Each column contains random values in the following ranges:
Column 0: B 1 thru 15
Column 1: I 16 thru 30
Column 2: N 31 thru 45
Column 3: G 46 thru 60
Column 4: O 61 thru 75
Each spot on the card must be able to be marked as played whenever a bingo ball is played. For instance, if ball N35 is played, and the spot in column 2 row 4 contains a 35, then that spot must be marked in some way to indicate that the number 35 was already played. The center spot on a bingo card is a free spot, so the spot in column 2 row 2 must always be marked as played.
A bingo card must be able to be analyzed for the number of wins it contains. It is possible for multiple wins to result from a single ball being played. A win results from any of the following:
All spots in a row have been marked
All spots in a column have been marked
All spots in either diagonal have been marked
At the start of each new game, all the marks on the card from the previous game must be removed.
The third item that needs to be modeled is the bingo player. Each player has a name. A player can play any number of bingo cards. At the beginning of each new game, a player must clear the marks from all their bingo cards. Whenever a bingo ball is played, a player is informed of the column and number being played. The player plays that column and number on each of their bingo cards. The player checks all cards for the number of wins thus far in the game. Whenever a new game starts, the number of wins from the current game is added to the total wins the player already has.
The fourth item that needs to be modeled is the game manager. The game manager controls the bingo cage and manages all the players participating in a bingo session. The game manager plays a specified number of games in a bingo session. Each game is played until a specified minimum number of wins have been achieved. At the start of each game, the game manager makes sure the bingo cage contains all the bingo balls, and that each player has a chance to unmarked all their bingo cards. The game manager provides each player with column and number information for each bingo ball that is played. The game manager gets the number of wins in the current game per player as each bingo ball is played. The game manager stops a game when the total wins for the current game has equaled or exceeded the minimum wins required. After playing a bingo session, the game manager reports how many wins each player has achieved.
Class Design Details
BingoCage Class
Member Variables:
ArrayList of bingo ball values
Random number generator
constructor ( ): makes sure the list of bingo balls is complete
newGame ( ): makes sure the list of bingo balls is complete. Nothing returned.
nextBall ( ): removes a randomly selected ball from the list. Returns its value.
BingoCard Class
Member Variables:
A 2 dimensional array (5x5) of card values where a card value has a number and a mark
constructor: creates the 2 dimensional array of card values. Center spot mark is set.
newGame ( ): all marks in 2 dimensional array are cleared. Center spot mark is set.
playBall (column, number): checks card for specified column, number. If present,
that card value’s mark is set.
checkForWins ( ): returns the number of rows, columns, diagonals where all the card values in that row, column, or diagonal are marked.
displayCard ( ): prints the current card state, row by row, showing both the number and
the mark for each spot. This function is for debugging purposes only.
Player Class
Member Variables:
The player’s name
An array of BingoCards
The total number of wins
constructor (name, numCards): Store the name. Create the requested number of cards.
newGame ( ): add number of wins in current game to total wins. Reset each card.
playBall (column, number): play specified ball value on each card. Return the sum of
the number of wins on each card.
checkForWins ( ): return the sum of the number of wins on each card. (private method)
getName ( ): return the name of the player.
getTotalWins ( ): return the total number of wins.
GameManager Class
Member Variables:
A BingoCage.
An array of Players.
The game number.
constructor ( ): determine number of players and the number of cards per player (ask user). Get the names (from user) for each player. Add each player to the game. Example output shown below:
How many players are there?
How many cards per player?
Enter the names for 5 players.
Dracula Wolfman Frankenstein Mr.Hyde VanHelsing
The players are ready! Let the games begin!
play (numberOfGames, winsPerGame): plays the specified number of games. For each
game, gets ball from cage, distributes column, number to all players, displays
number of wins (>0) so far after each ball. At end of game, prepares cage and
players for a new game. Example partial output shown below:
Starting game 9!
Game 9: There are 1 wins after ball 28
Game 9: There are 2 wins after ball 29
Game 9: There are 2 wins after ball 30
Game 9: There are 3 wins after ball 31
Game 9: There are 3 wins after ball 32
Game 9: There are 4 wins after ball 33
Game 9: There are 4 wins after ball 34
Game 9: There are 4 wins after ball 35
Game 9: There are 6 wins after ball 36
Starting game 10!
Game 10: There are 1 wins after ball 20
Game 10: There are 1 wins after ball 21
Game 10: There are 2 wins after ball 22
Game 10: There are 2 wins after ball 23
Game 10: There are 2 wins after ball 24
Game 10: There are 2 wins after ball 25
Game 10: There are 2 wins after ball 26
Game 10: There are 2 wins after ball 27
Game 10: There are 2 wins after ball 28
Game 10: There are 3 wins after ball 29
Game 10: There are 3 wins after ball 30
Game 10: There are 4 wins after ball 31
Game 10: There are 4 wins after ball 32
Game 10: There are 5 wins after ball 33
displayResults ( ): display the name and total wins for each player as shown below:
Results after 10 games:
VanHelsing : 16 wins.
Dracula : 13 wins.
Wolfman : 11 wins.
Frankenstein : 9 wins.
Mr.Hyde : 4 wins.
Test Class
The main method in the test class will create a GameManager, then call the play method specifying the number of games to be played, and the number of wins per game. Finally the displayResults method is called to see who won!
Development Strategy
Develop the BingoCage class first. Then write a little test code to get all 75 values from the cage and display them. Then call the newGame method and try getting the 75 values again.
When the cage is working, implement the BingoCard class. Then write a little test code to create a card, play all 75 bingo ball values, check for 12 wins, and display the card. Then call the newGame method and display the card again to verify that all but the center spot is in an unmarked state.
Lastly implement the Player class and the GameManager class. The test program main function can simply create a GameManager object and have it create the desired number of players. Then have main call the game manager’s play method to play 10 games with 5 wins per game. Debug the Player and GameManager classes as needed.
An ArrayList will be needed for the BingoCage ONLY. This will make life simpler since you will need to remove random values from the bingo ball list. Doing this with a simple array would be possible, but require much more messy code. An ArrayList works as follows:
To create an ArrayList reference variable, do the following:
ArrayList<datatype> listname;
The datatype is whatever you decide you want to use to represent the possible values of the bingo balls. To create an actual ArrayList object, do the following:
listname = new ArrayList<> ( );
To add a value to the end of an ArrayList, do the following:
listname.add (value);
Note that the data type of the value must be the same as the data type you specified when you created the ArrayList object. To remove an item from the ArrayList at a random position, do the following:
value = listname.remove (position);
The item at the specified position is removed and returned from this function call. This statement stores the returned value into a variable named value that must be the same data type as that specified when the ArrayList was created. The number of items left in the ArrayList can be gotten anytime by doing the following:
numItems = listname.size ( );
An ArrayList can do many other things, but most likely these are the only operations you will need to do for this lab.
To display the final results in order such that the player with the most total wins is displayed first, and the player with the least wins is displayed last, it would be nice to simply use the Arrays.sort function on the array of players in the game manager. In order to do this, you will need to implement the Comparableinterface in the Player class. To do this you need to first add the following:
public class Player implements Comparable
Doing this requires that you also add the following function to the Player class:
public int compareTo (Object object) { …. }
This method will need to cast the object parameter to a Player in order to access the total number of wins for that object and compare that to the total wins on “this” object. Simply returning the difference between these two values will sort the list in ascending or descending order based on total wins. By doing the subtraction in the right order, you can have the Player array sorted in descending order so the winner of the most games is the first player in the array.
Documentation
Make sure you document all parts of this project. A description of each class should be included in a Javadoc comment before the start of the class. Every function in each class must include a Javadoc comment describing the purpose, parameters, and return value.
Submission
Create a zip file of the project and upload your zip file to the Pilot drop box before the project due date. Also, copy your source code into ONE text file (preferably Wordpad) and submit that separately to the drop box. The order of the classes in the text file should be BingoCage, BingoCard, Player, GameManager, followed by your test class. Ask your TA for help if you have any questions.
NOTE: If the submitted project does not compile, it will receive zero points.
Rubric (50 pts)
BingoCage works as described. (10 pts)
BingoCard works as described. (10 pts)
Player works as described. (10 pts)
GameManager works as described. (10 pts)
Test program works as described. (5 pts)
Documentation and programming style. (5 pts)
Explanation / Answer
ANS:-
Given that,
program:-
BingoBoard.java
package bingoboard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class BingoBoard
{
private String board[][];
private final int BOARD_DIM = 5;
private final int MAX_SIZE = BOARD_DIM * BOARD_DIM;
private HashMap<String,Boolean> eventCalledMap;
private ArrayList<String> events;
private ArrayList<String> selectedEvents;
private final String FREE = "FREE SPACE";
private final int player;
private boolean win;
BingoBoard()
{
board = new String[BOARD_DIM][BOARD_DIM];
selectedEvents = new ArrayList<>();
events = new ArrayList<>();
eventCalledMap = new HashMap<>();
eventCalledMap.put(FREE, true);
player = -1;
win = false;
}//end BingoBoard
BingoBoard(ArrayList<String> eventList)
{
board = new String[BOARD_DIM][BOARD_DIM];
selectedEvents = new ArrayList<>();
events = eventList;
eventCalledMap = new HashMap<>();
eventCalledMap.put(FREE, true);
player = -1;
win = false;
}//end BingoBoard
BingoBoard(ArrayList<String> eventList, int numb)
{
board = new String[BOARD_DIM][BOARD_DIM];
selectedEvents = new ArrayList<>();
events = eventList;
eventCalledMap = new HashMap<>();
eventCalledMap.put(FREE, true);
player = numb;
win = false;
}//end BingoBoard
//updates the event list.
public void updateEvents(ArrayList<String> eventList)
{
events.addAll(eventList);
}//end updateEvents
//Chooses events and adds them to the board.
public boolean randomizeEvents()
{
if(this.events.size() < MAX_SIZE - 1)
return false;
while(selectedEvents.size() < MAX_SIZE - 1)
{
Random rand = new Random();
int index = rand.nextInt(this.events.size());
String str = events.get(index);
selectedEvents.add(str);
events.remove(str);
}//end while
int count = 0;
for(String str:selectedEvents)
{
eventCalledMap.put(str,false);
if(count == MAX_SIZE/2)
{
board[count/BOARD_DIM][count%BOARD_DIM] = FREE;
count++;
}//end if
board[count/BOARD_DIM][count%BOARD_DIM] = str;
count++;
}//end for
return true;
}//end randomizeEvents
public void printBoard()
{
System.out.printf("Player %d ",this.player);
System.out.println("_____________________");
for(int i = 0; i < BOARD_DIM; i++)
{
System.out.println("|---|---|---|---|---|");
for(int j = 0; j < BOARD_DIM; j++)
if(eventCalledMap.get(board[i][j]) == true)
System.out.printf("|%3s", "X");
else
System.out.printf("|%3s",board[i][j]);
System.out.println("|");
}//end for
System.out.println("|---|---|---|---|---|");
System.out.println("_____________________ ");
}//end printBoard
//Puts maker on given value if it
public void putMarker(String value)
{
if(eventCalledMap.containsKey(value))
eventCalledMap.put(value, Boolean.TRUE);
}//end method putMarker
/*Checks board for a win and returns true if board won and false
otherwise. */
public boolean checkWin()
{
this.win = evalBoard();
return this.win;
}//end method putMarker
//Returns true if
public boolean won()
{
return this.win;
}//end method won
//returns player number
public int getPlayer()
{
return player;
}//end getPlayer
//Checks the board for win. Returns true if a win is found.
private boolean evalBoard()
{
int i, j, count;
for(i = 0; i < BOARD_DIM; i++)
{
j = 0;
count = 0;
//Checks horizontally for a win.
while(eventCalledMap.get(board[i][j]) != false)
{
count++;
j++;
if(count == BOARD_DIM)
return true;
}//end while
j = 0;
count = 0;
//Checks verically for a win.
while(eventCalledMap.get(board[j][i]) != false)
{
count++;
j++;
if(count == BOARD_DIM)
return true;
}//end while
}//end for
i = 0;
count = 0;
//Checks the top left to bottom right diagnal for a win.
while(eventCalledMap.get(board[i][i]) != false)
{
count++;
i++;
if(count == BOARD_DIM)
return true;
}//end while
i = BOARD_DIM -1;
j = 0;
count = 0;
//Checks the top left to bottom right diagnal for a win.
while(eventCalledMap.get(board[i][j]) != false)
{
count++;
i--;
j++;
if(count == BOARD_DIM)
return true;
}//end while
return false;
}//end evalBoard
}//end class
BingoGame.java
package bingoboard;
import java.util.ArrayList;
import java.util.Scanner;
public class BingoGame
{
private ArrayList<String> eventList;
private final int DEFAULT_PLAYER_COUNT = 2;
private int playerCount;
private boolean winnerDetermined;
private ArrayList<BingoBoard> boardList;
BingoGame()
{
this.eventList = new ArrayList<>();
this.playerCount = DEFAULT_PLAYER_COUNT;
this.winnerDetermined = false;
this.boardList = new ArrayList<>();
}//end default constructor
BingoGame(int players)
{
this.eventList = new ArrayList<>();
this.playerCount = players;
this.winnerDetermined = false;
boardList = new ArrayList<>();
}//end constructor
//adds events for game.
public void addEvent(String event)
{
this.eventList.add(event);
}//end method addEvent
//Main driver for the game.
public void startGame()
{
this.winnerDetermined = false;
for(int i = 1; i <= this.playerCount;i++)
{
ArrayList<String> events = (ArrayList<String>) eventList.clone();
BingoBoard board = new BingoBoard(events,i);
board.randomizeEvents();
this.boardList.add(board);
board.printBoard();
}//end for
Scanner in = new Scanner(System.in);
while(this.winnerDetermined == false)
{
System.out.println("Enter Event:");
String check = in.next();
for(BingoBoard boards:boardList)
{
boards.putMarker(check);
boards.printBoard();
if(winnerDetermined == false)
winnerDetermined = boards.checkWin();
else
boards.checkWin();
}//end for
}//end while
this.printWinner();
}//end startGame
//Prints out winning boards. More than one player may win.
private void printWinner()
{
//Prints out winning boards. More than one player may win.
for(BingoBoard boards:boardList)
{
if(boards.won())
System.out.printf("Player %d wins! ",boards.getPlayer());
}//end for
}//end printWinner
}//end class
BingoTester.java
package bingoboard;
public class BingoTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
BingoGame game = new BingoGame(4);
for(int i=1; i<=25; i++)
game.addEvent(Integer.toString(i));
game.startGame();
}//end main
}//end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.