In: Computer Science
JAVA!!!!
int[][] BingoCardArray = new int[5][5];
Use a nested for-loop, to populate the 2-D array representing 5
columns for B – I – N – G – O, where
row 1 =
col 1 = random # 1-
15
col 2 = random # 16
– 30
col 3 = random # 31
– 45
col 4 = random # 46
– 60
col 5 = random # 61
– 75
row 2 =
col 1 = random # 1-
15
col 2 = random # 16
– 30
col 3 = random # 31
– 45
col 4 = random # 46
– 60
col 5 = random # 61
- 75
row 3 =
col 1 = random # 1-
15
col 2 = random # 16
– 30
col 3 = random # 31
– 45
col 4 = random # 46
– 60
col 5 = random # 61
– 75
row 4 =
col 1 = random # 1-
15
col 2 = random # 16
– 30
col 3 = random # 31
– 45
col 4 = random # 46
– 60
col 5 = random # 61
- 75
row 5 =
col 1 = random # 1-
15
col 2 = random # 16
– 30
col 3 = random # 31
– 45
col 4 = random # 46
– 60
col 5 = random # 61
– 75
The playGame()method in the BingoGame (Driver) class will loop to generate 50 random numbers in the range of 1 to 75. This simulates drawing 50 numbers in a BINGO game. The method will pass the number to a method, checkBingo()in the domain class, BingoCard, which will check if the number received as a parameter is in the B range (1 – 15), I range (16 – 30), N range (31 – 45), G range (46 -60) or O range (61 – 75). According to the range it is in, the method will check each of the cells in designated column (col 0 = B; col 1 = I; col 2 = N; col 3 = G; col 4 = O). Example: randomNum 27 was randomly generated, which belongs to the I-range, column 1, checking rows 0 to 4.
for (int row = 0; row <= 4; row++)
{
//hard-code column 1 since checking
I-column:
if (randomNum ==
BingoCardArray[row][1])
{
BingoCardArray[row][1]
= 0;
}
}
etc. //the 0 being moved to the
number simulates putting a chip on the Bingo card, marking that
the
//
number was called.
The gotBingo()method in the Bingo (Domain) class will return TRUE if 5 numbers were set to 0 either horizontally, vertically, or diagonally. Otherwise, the method will return FALSE.
Each horizontal checkis a set of if-else if statements
for (int row = 0; row < 5; row++)
{
int rowTotal = 0;
for (int col = 0; col <
5; col++)
{
//add up all the column values in that row, and see if they add up to 0.
….
}
}
Each vertical checkis a set of if-else if statements
for (int col = 0; col < 5; col++)
{
int colTotal = 0;
for (int row = 0; row <
5; row++)
{
//add up all the row values in that column, and see if they add up to 0.
….
}
}
//for diagonals check….
for (int row = 0; row < 5; row++)
{
int diagonal1Total = 0;
int diagonal2Total = 0;
for (int col = 0; col <
5; col++)
{
//figure out if it is a diagonal going one way
//figure out if it is a diagonal going the other way
….
}
}
When gotBingo()ends, it will have either returned a TRUE or a FALSE.
In the driver class, the totalGamesWon is a global variable for keeping track of how many games the user wins instead of the computer. Before the
The determineWinner()method in the Bingo (Driver) class will call the gotBIngo() method in the domain class, and if it returns true, it will add 1 to the totalGamesWon, otherwise it will leave the totalGamesWon alone.
Finally, the mainmethod will contain a do-while loop that will execute at least once, and do the following:
//Java code
import java.util.Random; public class BingoCard { private int[][] BingoCardArray ; Random random= new Random(); private static int totalGameWon=0; //constructor public BingoCard() { BingoCardArray = new int[5][5]; /** * generate 50 random numbers in the range of 1 to 75. */ for (int i = 0; i <BingoCardArray.length ; i++) { /** * col 1 = random # 1- 15 * col 2 = random # 16 – 30 * col 3 = random # 31 – 45 * col 4 = random # 46 – 60 * col 5 = random # 61 – 75 */ //B BingoCardArray[i][0]=random.nextInt(15)+1; //I BingoCardArray[i][1]=random.nextInt(15)+16; //N BingoCardArray[i][2]=random.nextInt(15)+31; //G BingoCardArray[i][3]=random.nextInt(15)+46; //O BingoCardArray[i][4]=random.nextInt(15)+61; } } public int[][] getBingoCardArray() { return BingoCardArray; } public static int getTotalGameWon() { return totalGameWon; } /** * checkBingo()in the domain class, BingoCard, * which will check if the number received as a parameter * is in the B range (1 – 15), * I range (16 – 30), N range (31 – 45), G range (46 -60) or O range (61 – 75) * @param aNum */ public void checkBingo(int aNum) { for (int row = 0; row <= 4; row++) { //hard-code column 1 since checking B-column: if (aNum == BingoCardArray[row][0]) { BingoCardArray[row][0] = 0; } //I column else if(aNum == BingoCardArray[row][1]) { BingoCardArray[row][1]=0; } //N column else if(aNum == BingoCardArray[row][2]) { BingoCardArray[row][2]=0; } //G column else if(aNum == BingoCardArray[row][3]) { BingoCardArray[row][3]=0; } //O column else if(aNum == BingoCardArray[row][4]) { BingoCardArray[row][4]=0; } } } public boolean determineWinner() { if(gotBingo()) { totalGameWon++; return true; } else { return false; } } public boolean gotBingo() { boolean isBingo = false; //check horizontal int[] rowTotal = new int[BingoCardArray.length]; for (int i = 0; i <BingoCardArray.length ; i++) { int sum=0; for (int j = 0; j <BingoCardArray[0].length ; j++) { sum+=BingoCardArray[i][j]; } rowTotal[i]=sum; } //check vertical int[] colTotal=new int[BingoCardArray[0].length]; for (int i = 0; i <BingoCardArray.length ; i++) { int sum=0; for (int j = 0; j <BingoCardArray[0].length ; j++) { sum+=BingoCardArray[j][i]; } colTotal[i]= sum; } if(rowTotal[0]==0 || rowTotal[1]==0|| rowTotal[2]==0 || rowTotal[3]==0 || rowTotal[4]==0) isBingo= true; else if(colTotal[0]==0 || colTotal[1]==0||colTotal[2]==0||colTotal[3]==0||colTotal[4]==0) isBingo= true; //check diagonal if( (BingoCardArray[0][0]==0 && BingoCardArray[1][1]==0 && BingoCardArray[2][2]==0 && BingoCardArray[3][3]==0 && BingoCardArray[4][4]==0) || (BingoCardArray[4][4]==0 && BingoCardArray[3][3]==0 && BingoCardArray[2][2]==0 && BingoCardArray[1][1]==0 && BingoCardArray[0][0]==0) ) isBingo= true; return isBingo; } @Override public String toString() { String result=""; for (int i = 0; i <BingoCardArray.length ; i++) { for (int j = 0; j <BingoCardArray[0].length ; j++) { result+=String.format("%-5.30s",BingoCardArray[i][j]); } result+="\n"; } return result; } }
//===========================================
import java.util.Random; import java.util.Scanner; public class BingoGame { public static void main(String[] args) { BingoCard bingoCard; Scanner input = new Scanner(System.in); while (true) { bingoCard=new BingoCard(); while (true) { playGame(bingoCard); System.out.println(bingoCard); if (bingoCard.determineWinner()) { System.out.println("You got Bingo.."); break; } else { System.out.println("Ohhh.. You not got Bingo this time."); System.out.println("Retry (y/n): "); String choice = input.nextLine(); if (choice.charAt(0) == 'N' || choice.charAt(0) == 'n') break; } } System.out.println("Do you really wanna exit (y/n): "); String choice = input.nextLine(); if (choice.charAt(0) == 'Y' || choice.charAt(0) == 'y') break; } System.out.println("Total matches win by you: "+BingoCard.getTotalGameWon()); } public static void playGame(BingoCard bingoCard) { Random random = new Random(); for (int row = 0; row <= 4; row++) { int randomNum = random.nextInt(75)+1; bingoCard.checkBingo(randomNum); } } }
//Output
//If you need any help regarding this solution.......... please leave a comment ........ thanks