/** ****************************************************************************** * HOMEWORK 1, 15-111 ****************************************************************************** * BINGO ****************************************************************************** * * The standard bingo game. * * * * * Name: Brandon Sherman * * AndrewID: bsherman * ******************************************************************************/import java.util.*;import java.io.*;public class Bingo{ private Random rand = new Random(); private int[][] card; //Bingo card configuration private int[] stream; //list of 75 integers private boolean[][] marks; //simulates placing chips on a Bingo card public Bingo() { card = new int[5][5]; stream = new int[75]; marks = new boolean[5][5]; ArrayList<Integer> streamBuffer = new ArrayList<Integer>(); } /** * This method writes a random Bingo card configuration and a stream of random * number between 1 and 75 to the output file. * * The first column in the table contains only integers between 1 and 15, * the second column numbers are all between 16 and 30, the third are 31 to 45, * the fourth 46-60, and the fifth 61-75. * * There are no duplicate numbers on a Bingo card. */ public void write(String outputFile) throws IOException { FileWriter ad = new FileWriter(outputFile); randCard();//create card from helper method randStream();//create stream //Create output string and write to file String out = ""; for (int row = 0; row<5; row++ ) { for (int column = 0; column<5; column++) { out=out+" "+card[row][column]; } out = out+"\n"; } for (int streamNum=0; streamNum<75; streamNum++) { out = out+" "+stream[streamNum]; } ad.write(out); ad.close();//close file to make it readable } private void randCard() { for (int column = 0; column<5; column++) { boolean[] columnUse = new boolean[15];//create number tracker for (int row = 0; row<5; row++ ) { //create random numbers int setNumber = rand.nextInt(15); if (columnUse[setNumber]) { setNumber = rand.nextInt(15); row--; } else { card[row][column] = setNumber+1+column*15; //remove number from set columnUse[setNumber] = true; } } } } private void randStream() { for (int number = 1; number<76;number++) { streamBuffer.add(number); } shuffle(streamBuffer); for (int number = 0; number<75;number++) { stream[number] = (int)(streamBuffer.get(number)); } } /** * Shuffles the list of numbers */ public void shuffle(ArrayList<Integer> list) { Random rand = new Random(); for (int i=0; i< list.size(); i++) { int temp = 0; int r = rand.nextInt(74); //range is 0-74 r++; //range is now 1-75 /* ensure all array elements get shuffled */ temp = list.get(r); //random spot stored list.set(r,list.get(i)); //random spot gets array element list.set(i,temp); //array element gets the value of the random spot } } /** * This method reads a given inputFile that contains a Bingo card configuration and * a stream of numbers between 1 and 75. * . * A Bingo card configuration is stored in the card array. * A list of 75 integers is stored in the stream array. */ public void read(String inputFile) throws IOException { //open file Scanner sc = new Scanner(new File(inputFile)); // for (int i = 0; i<5; i++) { for (int j = 0; j<5; j++) { card[i][j] = sc.nextInt(); } sc.nextLine(); } for (int i = 0; i<75; i++) { stream[i]=sc.nextInt(); } sc.close(); } /** * This method returns the first integer from the stream array that * gives you the earliest winning condition. * * - all the spots in a column are marked * - all the spots in a row are marked * - all the spots in either of the two diagonals are marked * - all four corner squares are marked X */ public int playGame() { for (int a=0; a< 75; a++) { //search card for the "called" number for (int i=0; i< 5; i++) { for (int j=0; j< 5; j++) { //if it is present, place a marker in that space if (card[i][j] == stream[a]) { marks[i][j] = true; } } } if (gameWon()) return stream[a]; } return -1; //if you get this, you're doing it wrong } public boolean gameWon() //tests to see if the game has been won or not { //testing for four cornered win if ((marks[0][0]==true) && (marks[0][4]==true) && (marks[4][4]==true) && (marks[4][0]==true)) return true;//loop through and determine if an entire row or collumn has been marked for (int i=0; i< 5; i++) { int row = 0; int col = 0; for (int j=0; j< 5; j++) { if (marks[i][j] == true) row++; //totals the number of marks in a line if (marks[j][i] == true) //totals the number of marks in a column col++; if ((row==5) || (col == 5)) //once we have 5 in a row, win { return true; } } } for (int i=0; i< 5; i++) //first diagonal { int diag = 0; if (marks[i][i] == true) diag++; if (diag == 4) return true; } for (int i=4; i>=0; i--) //second diagonal { int diag = 0; if (marks[i][i] == true) diag++; if (diag == 4) return true; } //nobody wins return false; }}