Hello, this is for programming 2 with the java language. Below is my first proje
ID: 3744503 • Letter: H
Question
Hello, this is for programming 2 with the java language. Below is my first project assignment. For this assignment we are supposed to make a bingo game. The professor gave us a series of hints and I will try my hardest to convey them correctly. The Professor told us to use BitSet cuz in the game of bingo when a number has been selected you exclude that number from the random generator. He also told use to use the StringBuffer in order to get an X on the users bingo card to show what numbers have been selected. Um he also said to of course use the (int)(Math.random()*74+1) thingy to generate the random numbers. He says to use a while(true) loop until a winner is announced. He said that we should use return; at the end of a method instead of a break;, I’m not sure why... uh he said to keep track of numbers picked and that checkforWin method is the most important method because it does all the real work and if win is found that it checks the horizontal first then vertical then diagonals and the way to do that is if any of them sum up to 0 then that is a win and to announce the winner. He also gave us the main tricks saying 1) Declare our card matrix 2)Fill it up 3)print out card 4) play the game until win found [which is where all the above stuff that I mentioned fits in I think] 5)print out card that wins, the numbers called, type of bingo win? Something like that... Please include comments if you can, at least with difficult parts, thank you so much.
So please keep the program simple utilizing what the professor asked for if possible. I thank you so very much for your help.
Ps. There is extra credit if we write the program to where it have multiple users. Like instead of just one bingo card, have two bingo cards. (No user input is in this program I don’t think) again you don’t have to do this, it would just be awesome. Thank you no matter what though.
Below is the sample of what the output of the program should look like. The professor said he wants the bingo card to come out in rows of five but you don’t have to do the dashs and line things.
This is the StringBuffer and BitSet thingy he gave us as examples
Computer Science 205 Project #1 The BINGO! Game Due Date : Friday, September 21st, 11:59 PM 50 Points Objective The purpose of this assignment is to acquaint ourselves with processing multi-dimensional ar- rays. This program will utilize loops, external file i/o, random numbers, and methods with array parameters The Game of BINGO! Bingo is a popular game played on a 5 row by 5 column grid, called a card. There is one letter of the word B-I-N-G-O at the top of each column. In each space under each letter are randomly . under B, 1-15 under I, 16-30 under N, 31-45 . under G, 46-60 under O, 61-75. As numbers are selected, they are marked off the card. When a line of five squares, horizontal, vertical, or diagonal, is marked out, the card is a winner. A sample bingo card is shown below 12 28 31 49 66 3 | 26 | 45 | 53 | 75 10 17 33 59 67 7 19 42 55 74 46 If the numbers 3, 45, 53, 75, and 26 are picked at random, then there will be a horizontal bingo on the second row. Diagonal wins may be from top left corner down to bottom right corner or top right corner down to bottom left corner. No free space will be utilized.Explanation / Answer
Solution:
BingoBoard.java
Code:
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
Code:
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
Code:
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.