Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 4 This project provides an opportunity to practice object oriented progr

ID: 3708382 • Letter: P

Question

Project 4 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, N, G, O. Each column contains random values in the following ranges: . Column 0: B thru 15 . Column1: 16 thru 30 . Column 2: N 31 thru 45 . Column 3: G 46 thru 60 . Column 4: O61 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

Explanation / Answer

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