Continuing with Bingo Now that we have our Bingo Cards we should play Bingo!!! C
ID: 3606352 • Letter: C
Question
Continuing with Bingo
Now that we have our Bingo Cards we should play Bingo!!!
Create a program that will:
do everything from program 2 (create bingo cards), if you didn't get this working just right go see TAs/professor As Soon As Possible
Add an option 4 to your menu for Play Bingo
read in a bingo call (e,g, B6, I17, G57, G65)
checks to see if the bingo call read in is valid (i.e., G65 is not valid)
marks all the boards that have the bingo call
checks to see if there is a winner, for our purposes winning means
5 tokens in a row or column
5 tokens in a diagonal
1 token in each of the 4 corners of the game board
Loop accepting bingo calls until there is a winner, and display an appropriate message specifying which user won, and how they won (5 in a row with calls B6, I20, ...; 5 in a column with calls ...; 5 on a diagonal with calls ... ; 4 corners with calls ...)
Explanation / Answer
Here we need 2 classes
1. Board class
2. Game class
so we do make a seperate java files for both of them and then we will make a driver program (main method) to call the game
1. Board
public class BingoBoard
{
private final static int BOARD_SIZE = 5;
private String board[BOARD_SIZE][BOARD_SIZE];
private final static int MAX_SIZE = BOARD_SIZE * BOARD_SIZE;
private HashMap<String,Boolean> eventCalledMap;
private ArrayList<String> events;
private ArrayList<String> selectedEvents;
private final String EMPTY = "EMPTY SPACE";
private final int player;
private boolean win;
BingoBoard()
{
board = new String[BOARD_SIZE][BOARD_SIZE];
selectedEvents = new ArrayList<>();
events = new ArrayList<>();
eventCalledMap = new HashMap<>();
eventCalledMap.put(EMPTY, true);
player = -1;
win = false;
}//end BingoBoard
BingoBoard(ArrayList<String> eventList)
{
board = new String[BOARD_SIZE][BOARD_SIZE];
selectedEvents = new ArrayList<>();
events = eventList;
eventCalledMap = new HashMap<>();
eventCalledMap.put(EMPTY, true);
player = -1;
win = false;
}//end BingoBoard
BingoBoard(ArrayList<String> eventList, int numb)
{
board = new String[BOARD_SIZE][BOARD_SIZE];
selectedEvents = new ArrayList<>();
events = eventList;
eventCalledMap = new HashMap<>();
eventCalledMap.put(EMPTY, 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_SIZE][count%BOARD_SIZE] = EMPTY;
count++;
}//end if
board[count/BOARD_SIZE][count%BOARD_SIZE] = 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_SIZE; i++)
{
System.out.println("|---|---|---|---|---|");
for(int j = 0; j < BOARD_SIZE; 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_SIZE; i++)
{
j = 0;
count = 0;
//Checks horizontally for a win.
while(eventCalledMap.get(board[i][j]) != false)
{
count++;
j++;
if(count == BOARD_SIZE)
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_SIZE)
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_SIZE)
return true;
}//end while
i = BOARD_SIZE -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_SIZE)
return true;
}//end while
return false;
}//end evalBoard
}//end class
2. Game
public class BingoCardsGame
{
private ArrayList<String> eventList;
private final int no_of_Players = 2;
private int playerCount;
private boolean is_game_won;
private ArrayList<BingoBoard> boardList;
BingoCardsGame(int players)
{
this.eventList = new ArrayList<>();
this.playerCount = players;
this.is_game_won = false;
boardList = new ArrayList<>();
}//end constructor
BingoCardsGame()
{
this.eventList = new ArrayList<>();
this.playerCount = no_of_Players;
this.is_game_won = false;
this.boardList = new ArrayList<>();
}//end default 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.is_game_won = 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.is_game_won == false)
{
System.out.println("Enter Event:");
String check = in.next();
for(BingoBoard boards:boardList)
{
boards.putMarker(check);
boards.printBoard();
if(is_game_won == false)
is_game_won = 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
MAIN method
Hope this will help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.