Bingo!

 

Modules - CSE2130 Data Structures, CSE 2120 Procedural Programming

 

Teacher’s Notes:

=======================================

I found it was helpful to include a picture of a BINGO card for students who did not know what this game was.

I have included a BINGO number generator for this program that will generate the 25 numbers for the BINGO card as well as simulate the sample number calls in case you wanted to make test cases.
 
I brute forced the diagonal win checking but for such a small card, I couldn't be bothered to set up a more elegant solution.
 

Assignment:

=======================================

In the game of BINGO, a player is given a 5 x 5 card with integer numbers in each cell on the card. Each number on the card only appears once.
 
As the game is played, another person draws numbers and calls them out. If the number that is drawn appears on the card, the player makes a mark in the corresponding spot where the number is located. The center spot is considered to be a free spot - the player can automatically mark this spot as being called at the beginning of the game.

In this version of the game, there are a total of 50 numbers drawn.
 
A player wins the game if:
( a ) all five of the numbers in any row are called.
( b ) all five of the numbers in any column are called.
( c ) all five of the numbers in the diagonal from the top left to the bottom right are called.
( d ) all five of the numbers in the diagonal from the top right to the bottom left are called.
 
Write a program that creates a 2 dimensional array to represent the bingo card. The program should then read 5 sets of 5 integers from the keyboard, representing the numbers in each row. A number will be given for the center number even though it is a free spot.
 
Following this, the program will read 50 integer numbers from the keyboard - representing the numbers being drawn. After these numbers have been read, the program will output the number of draws that were made before the player won the game. If the player did not win the game, the program should output a -1.
 
THERE ARE NO PROMPTS, ERROR TRAPS OR RANDOM NUMBER GENERATION REQUIRED FOR THIS PROBLEM!!

Sample Input 1:

4 27 32 55 73
15 25 41 58 75
8 26 55 59 70
7 22 33 54 62
13 17 43 48 67
4 25 54 59 38 36 67 5 …(42 more numbers after this)

Sample Output 1:

7

Explanation of Sample Output 1:

The first set of numbers creates the same board as the one in the picture above. On the seventh number, all of the numbers on the diagonal line from top left corner to bottom right corner will have been called except the 55. However, the 55 is the center square which is the free square. This means that the person has won on the seventh call with the chain 4-25-55-54-67

 

 

 

Solution (JAVA):

=======================================

import java.util.Scanner;
public class Main {
    public static boolean hasWon(boolean[][] marks)
    {
        int countX = 0, countY = 0;
        for(int x = 0; x < 5; x++)
        {
            countX = 0;
            countY = 0;
        
            for(int y = 0; y < 5; y++)
            {
                if(marks[x][y])
                    countX++;
                if(marks[y][x])
                    countY++;
            }
            if(countX == 5 || countY == 5)
                return true;
        }
        
        
        if(marks[0][0] && marks[1][1] && marks[2][2] && marks[3][3] && marks[4][4])
            return true;
        if(marks[4][0] && marks[3][1] && marks[2][2] && marks[1][3] && marks[0][4])
            return true;
    
        return false;
    }
    
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[][] card = new int[5][5];
        boolean[][] marks = new boolean[5][5];
        boolean wonGame = false;
        int number = 0, winningDraw = -1;
        
        for(int x = 0; x < 5; x++)
            for(int y = 0; y < 5; y++)
                card[x][y] = input.nextInt();
        marks[2][2] = true;
        for(int a = 1; a <= 50; a++) {
      
            number = input.nextInt();
            
            for(int x = 0; x < 5; x++)
                for(int y = 0; y < 5; y++)
                    if(!wonGame && card[x][y] == number)
                    {
                        marks[x][y] = true;
                        if(hasWon(marks))
            {
                            winningDraw = a;
              wonGame = true;
            }
                    }
        }
        
        System.out.println("won on turn: " + winningDraw);
            
    }
}
 

 

 

 

Card and Call Generator (JAVA):

=======================================


public class RNG {
    public static void main(String[] args) {
        boolean[] numberList = new boolean[101];
        int count = 0;
        int sum = 0;
        int rawSum = 0;
         
        while(count < 25)
        {
            int number = (int)(Math.random()*100+1);
            if(!numberList[number]) {
                System.out.println(number);
                numberList[number] = true;
                count++;
            }    
        }
        
        System.out.println();
        
        count = 0;
        for(int x =0; x < 50; x++)
            numberList[x] = false;
        while(count < 50)
        {
            int number = (int)(Math.random()*100+1);
            if(!numberList[number]) {
                System.out.println(number);
                numberList[number] = true;
                count++;
            }    
        }
    }
}