I got homework about making bingo game in Jgrasp but can\'t do anything... 45 pt
ID: 3583030 • Letter: I
Question
I got homework about making bingo game in Jgrasp but can't do anything...
45 pts: The GUI is operational; looks good and works
5 pts: Tell the player how to start the game
5 pts: Some mechanism to control game (button, menu,…)
5 pts: NO duplicates if it is applicable to your display (no duplicate numbers for a Bingo board or no duplicate cards for a card game)
10 pts: You can actually PLAY the game (everything works)
10 pts: NEW game feature to start over after win or in middle of game
20 pts: Check if user wins
here is my starting code for bingo
///lots of comments
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class BingoGame extends JFrame
{
//some panels
JPanel CardPanel;
// create 5 BCells
JLabel BCell1;
// create 5 ICells
JLabel ICell1;
///do the rest
public BingoGame()
{
buildBingoCardPanel();
//buildButtonPanel();
add(CardPanel);
setSize(500,500);
setVisible(true);
}
public void buildBingoCardPanel()
{
CardPanel = new JPanel();
BCell1 = new JLabel("");
ICell1 = new JLabel("");
fillCells();
CardPanel.add(BCell1);
CardPanel.add(ICell1);
}
public void fillCells()
{
//WITH FIRST TWO METHODS, NEED TO MAKE SURE YOU
//DON'T HAVE DUPLICATES
//randomize some numbers to fill Bingo Board
//1st way -- use Math.random()
//rand num between 1-15
int randNum = (int)(Math.random()*15) + 1;
//add +15 for Is, add +30 for Ns, add +45 for Gs, add +60 for Os
BCell1.setText(String.valueOf(randNum));
//2nd way -- use Random class
//give it the upper bounds of range of random nums
Random myRandom = new Random(15);
randNum = myRandom.nextInt();
BCell1.setText(String.valueOf(randNum));
//3rd way -- use the ArrayList & the Collections class
//put 15 numbers into array & then shuffle them
//put this UP TOP!!
ArrayList<Integer> Bnumbers = new ArrayList<Integer>();
for (int i=1; i<=15; i++)
{
Bnumbers.add(i);
//Inumbers.add(i+15);
//Nnumbers.add(i+30);
//Gnumbers.add(i+45);
//Onumbers.add(i+60);
}
Collections.shuffle(Bnumbers);
System.out.println(Bnumbers);
//Collections.shuffle(Inumbers);
//etc...
BCell1.setText((Bnumbers.get(0)).toString());
//BCell2.setText((Bnumbers.get(1).toString());
//BCell3.setText((Bnumbers.get(2).toString());
//BCell4.setText((Bnumbers.get(3).toString());
//BCell5.setText((Bnumbers.get(4).toString());
}
public static void main(String[] args)
{
BingoGame myGame = new BingoGame();
}
}
Explanation / Answer
Please find the code below which would help you to modify your code and try on UI.
// Bingo Code
// Import the jars required.
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Random;
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
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
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
Output:
Player 1
_____________________
|---|---|---|---|---|
| 14| 9| 6| 18| 19|
|---|---|---|---|---|
| 11| 4| 1| 10| 16|
|---|---|---|---|---|
| 24| 8| X| 21| 22|
|---|---|---|---|---|
| 20| 17| 12| 13| 2|
|---|---|---|---|---|
| 15| 7| 23| 3| 5|
|---|---|---|---|---|
_____________________
Player 2
_____________________
|---|---|---|---|---|
| 5| 14| 18| 11| 12|
|---|---|---|---|---|
| 10| 2| 20| 21| 22|
|---|---|---|---|---|
| 16| 3| X| 15| 23|
|---|---|---|---|---|
| 25| 8| 6| 19| 13|
|---|---|---|---|---|
| 9| 7| 4| 17| 1|
|---|---|---|---|---|
_____________________
Player 3
_____________________
|---|---|---|---|---|
| 2| 14| 9| 23| 12|
|---|---|---|---|---|
| 17| 24| 11| 19| 18|
|---|---|---|---|---|
| 5| 22| X| 15| 8|
|---|---|---|---|---|
| 10| 4| 1| 13| 20|
|---|---|---|---|---|
| 6| 25| 7| 3| 16|
|---|---|---|---|---|
_____________________
Player 4
_____________________
|---|---|---|---|---|
| 25| 24| 4| 2| 7|
|---|---|---|---|---|
| 13| 8| 14| 20| 11|
|---|---|---|---|---|
| 21| 18| X| 17| 5|
|---|---|---|---|---|
| 22| 19| 3| 23| 1|
|---|---|---|---|---|
| 6| 12| 15| 9| 10|
|---|---|---|---|---|
_____________________
Enter Event:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.