import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class BingoMain {
public static void main(String[] args) {
boolean[] bingoCheck = new boolean[3];
boolean playerWin = false;
boolean computerWin = false;
int playerHit = 0;
int computerHit = 0;
int bingoNumber = 0;
int rowColumn = 5;
int fields = rowColumn * rowColumn;
int listSize = fields * 4;
ArrayList<Integer> playerNumbers = new ArrayList<Integer>(listSize);
ArrayList<Integer> computerNumbers = new ArrayList<Integer>(listSize);
ArrayList<Integer> picker = new ArrayList<Integer>(listSize);
// Generate 3 shuffled lists with integers from 1 to listSize
// Refers to method 1: generateArray
playerNumbers = generateArray(listSize);
computerNumbers = generateArray(listSize);
picker = generateArray(listSize);
// truncate the bingo cards to the size of the bingo card (listSize)
playerNumbers.subList(fields, listSize).clear();
computerNumbers.subList(fields, listSize).clear();
// Format playerNumbers and computerNumbers in a 2 dimensional array
// Refers to method 2: printCard
System.out.println("This is your Bingo card: ");
System.out.println();
printCard(playerNumbers, rowColumn);
System.out.println();
System.out.println("This is the computer's Bingo card: ");
System.out.println();
printCard(computerNumbers, rowColumn);
System.out.println();
// Go through the picker list and replace numbers with 0 in playerNumbers and computerNumbers if they match
for (int i = 0; i < listSize; i++) {
bingoNumber = picker.get(i);
if (playerNumbers.contains(bingoNumber)) {
playerHit = playerNumbers.indexOf(bingoNumber);
playerNumbers.set(playerHit, 0);
}
if (computerNumbers.contains(bingoNumber)) {
computerHit = computerNumbers.indexOf(bingoNumber);
computerNumbers.set(computerHit, 0);
}
// Check if row, column, diagonal 1 and/or diagonal 2 is full for player and computer
// Refers to method 3: checkArray
if (Collections.frequency(playerNumbers, 0) >= rowColumn) {
bingoCheck = checkArray(playerNumbers, rowColumn);
if ((bingoCheck[0] == true) | (bingoCheck[1] == true) | (bingoCheck[2] == true)
| (bingoCheck[3] == true)) {
playerWin = true;
}
}
if (Collections.frequency(computerNumbers, 0) >= rowColumn) {
bingoCheck = checkArray(computerNumbers, rowColumn);
if ((bingoCheck[0] == true) | (bingoCheck[1] == true) | (bingoCheck[2] == true) | (bingoCheck[3] == true)) {
computerWin = true;
}
}
// Determine who won or if there was a draw, then print the cards
if ((playerWin == true) && (computerWin && true)) {
System.out.println("It's a draw!");
System.out.println();
System.out.println("Your card: ");
printCard(playerNumbers, rowColumn);
System.out.println();
System.out.println("The computer card: ");
printCard(computerNumbers, rowColumn);
break;
} else if (playerWin == true) {
System.out.println("Congratulations, you won!");
System.out.println();
System.out.println("Your card: ");
printCard(playerNumbers, rowColumn);
System.out.println();
System.out.println("The computer card: ");
printCard(computerNumbers, rowColumn);
break;
} else if (computerWin == true) {
System.out.println("Too bad... You lost!");
System.out.println();
System.out.println("Your card: ");
printCard(playerNumbers, rowColumn);
System.out.println();
System.out.println("The computer card: ");
printCard(computerNumbers, rowColumn);
break;
}
}
}
// Method 1: generateArray
// Generate populated and shuffled arrays of the appropriate list size
public static ArrayList<Integer> generateArray(int listSize) {
ArrayList<Integer> generatedArray = new ArrayList<Integer>(listSize);
for (int i = 1; i <= listSize; i++) {
generatedArray.add(i);
}
Collections.shuffle(generatedArray);
return generatedArray;
}
// Method 2: printCard
// method to print the arrays in rows and columns
public static void printCard(ArrayList<Integer> array, int rowColumn) {
int i = 0;
Object[][] bingoCard = new Object[rowColumn][rowColumn];
for (int j = 0; j < rowColumn; j++) {
for (int k = 0; k < rowColumn; k++) {
bingoCard[j][k] = array.get(i);
i++;
if (i > rowColumn * rowColumn) {
break;
}
}
}
System.out.println(Arrays.deepToString(bingoCard).replace("[[", "[").replace("]]", "]").replace("], ", "]\n"));
}
// Method 3: checkArray
// Method to return boolean array if there's a bingo, where row is index 0, column on 1, diagonal 1 on 2 and diagonal 2 on 3
public static boolean[] checkArray(ArrayList<Integer> array, int rowColumn) {
boolean row = false;
boolean column = false;
boolean diagonal1 = false;
boolean diagonal2 = false;
boolean[] bingoReturn = new boolean[4];
ArrayList<Integer> dummy = new ArrayList<Integer>(rowColumn);
ArrayList<Integer> placeHolder = new ArrayList<Integer>(rowColumn);
// Populate dummy list with 0's to compare with
for (int i = 0; i < rowColumn; i++) {
dummy.add(0);
}
// Check rows for bingo
for (int i = 0; i <= rowColumn * rowColumn-1; i += rowColumn) {
if ((array.get(i) == 0) && (row == false)) {
if (array.subList(i, i + rowColumn).equals(dummy)) {
row = true;
bingoReturn[0] = row;
System.out.println("Bingo! Row " + ((i / rowColumn) + 1));
break;
}
}
}
// Check columns for bingo
for (int i = 0; i < rowColumn; i++) {
if (array.get(i) == 0) {
for (int j = 0; j < rowColumn * rowColumn - 1; j += rowColumn) {
if ((array.get(i + j) != 0) | (row == true)) {
placeHolder.clear();
break;
} else {
placeHolder.add(array.get(i + j));
}
if (placeHolder.equals(dummy)) {
column = true;
bingoReturn[1] = column;
System.out.println("Bingo! Column " + (i + 1));
break;
}
}
placeHolder.clear();
}
}
// Check diagonal 1 for bingo
for (int i = 0; i <= rowColumn * rowColumn - 1; i += rowColumn + 1) {
if (array.get(i) == 0) {
placeHolder.add(0);
} else {
placeHolder.clear();
break;
}
if (placeHolder.equals(dummy)) {
diagonal1 = true;
bingoReturn[2] = diagonal1;
System.out.println("Bingo! Diagonal 1");
break;
}
}
placeHolder.clear();
// Check diagonal 2 for bingo
for (int i = rowColumn - 1; i <= rowColumn * rowColumn - 1; i += rowColumn - 1) {
if (array.get(i) == 0) {
placeHolder.add(0);
} else {
placeHolder.clear();
break;
}
if (placeHolder.equals(dummy)) {
diagonal2 = true;
bingoReturn[3] = diagonal2;
System.out.println("Bingo! Diagonal 2");
break;
}
}
placeHolder.clear();
dummy.clear();
return bingoReturn;
}
}