bingosample.java

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class BINGOSample { public static void main (String[] args) { /* * The BINGO card is a 5x5 array of Integer (wrapped ints). Only arrays of OBJECTS may be shuffled. * The called matrix keeps track of which numbers on the card are called. Remember [2][2] is TRUE. */ Integer[][] bingoCard = new Integer[5][5]; boolean[][] called = new boolean[5][5]; /* * Fill an array with 15 numbers (1-15) for the "B" column, put the numbers in random order, and * put the first 5 of those numbers in the "B" column of the bingo card. */ Integer[] BRow = new Integer[15]; fillRow(BRow, 1); printArray(BRow); // just for debugging and program development randomize (BRow); printArray (BRow); // just for debugging and program development putRowInBingoCard(bingoCard,0,BRow); /* * Fill an array with 15 numbers (16-29) for the "I" column, put the numbers in random order, and * put the first 5 of those numbers in the "I" column of the bingo card. */ Integer[] IRow = new Integer[15]; fillRow(IRow, 16); randomize (IRow); putRowInBingoCard(bingoCard,1,IRow); /* * ========================================================== * Repeat the above code for the "N", "G" and "O" columns. * ========================================================== */ /* * Print the bingo card to see if it is correct. */ printBingoCard(bingoCard); } /* * This method is just here for purposes of testing the program while it is being developed */ public static void printArray (Integer[] a) { for (int i=0; i<a.length; i++) System.out.print(" "+ a[i].intValue()); System.out.println(); } /* * Fill an array of Integers with 15 numbers with a given starting value. */ public static void fillRow(Integer[] theRow, int startValue){ for (int i = 0; i< 15; i++) theRow[i]= new Integer(startValue++); } /* * Take the array of sequential Integers and put them in random order. */ public static void randomize (Integer[] theArray) { Collections.shuffle(Arrays.asList(theArray)); } /* * The BINGO card needs only five of the shuffled numbers, so just put in the first five. */ public static void putRowInBingoCard(Integer[][]card, int column, Integer[] row){ for (int i=0; i<5; i++) card[i][column] = row[i]; } /* * Crude method to test how the BINGO card looks. */ public static void printBingoCard(Integer[][] bcard) { for (int i=0;i<5;i++) { for (int j=0; j<5;j++) System.out.print(bcard[i][j]+" "); System.out.println(); } } }