[JAVA] Bingo judgment program

Overview

Bingo or reach judgment is made by obtaining the bingo card information and the information of the number read out from the file.

conditions

In a normal bingo game, the center is vacant from the beginning, but for the time being, I will not vacate this time.

Input file

Bingo card

board.txt


1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Selected number

selected.txt


1
7
13
19
2
3
4

Read file

Refer to this page for reading files [Java] Method that reads the entire text file and returns a string

About bingo judgment

The unwise way I came up with for the time being

-Cut out the numerical value of the file with split etc. and insert it into a two-dimensional array

        String str = readAll("board.txt");
        String[] str2 = str.split("\n");
        int N = str2.length;
        String[][] str4 = new String[N][N];
        int[][] board = new int[N][N];

        for (int i = 0; i < str2.length; i++){
            str4[i] = str2[i].split(" ");
        }

        for (int i = 0; i < str4.length; i++){
            for (int j = 0; j < str4.length; j++){
                board[i][j] = Integer.parseInt(str4[i][j]);
            }
        }

        String str3 = readAll("selected.txt");
        String[] str5 = str3.split("\n");
        int[] selected = new int[str5.length];

        for (int i = 0; i < str5.length; i++){
            selected[i] = Integer.parseInt(str5[i]);
        }

・ From there, take out to another array for vertical judgment, horizontal judgment, and diagonal judgment.

        int[][] row = new int[N][N];
        int[][] col = new int[N][N];
        int[][] naname = new int[2][N];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                col[i][j] = board[j][i];
                row[i][j] = board[i][j];

                if (i == j) {
                    naname[0][i] = board[i][j];
                }
                if (i + j == N - 1) {
                    naname[1][i] = board[i][j];
                }
            }
        }

-Prepare a method that returns the judgment result with two arrays (the array to be compared and the selected array) as arguments.

public static String board(int[] board, int[] selected){
        int count = 0;

        for (int i = 0; i < board.length; i++){
            for (int j = 0; j < selected.length; j++){
                if(board[i] == selected[j]){
                    count++;
                    break;
                }
            }
        }
        if (count == board.length) {
            return "Bingo";
        } else if (count == board.length - 1) {
            return "Reach";
        } else {
            return "NOT";
        }
}

・ Check all patterns and go around

        int bingoNum = 0, reachNum = 0;

        for (int[] i : col) {
            if (check(i, selected).equals("Bingo")){
                bingoNum++;
            } else if (check(i, selected).equals("Reach")){
                reachNum++;
            }
        }

        for (int[] i : row) {
            if (check(i, selected).equals("Bingo")){
                bingoNum++;
            } else if (check(i, selected).equals("Reach")){
                reachNum++;
            }
        }

        for (int[] i : naname) {
            if (check(i, selected).equals("Bingo")){
                bingoNum++;
            } else if (check(i, selected).equals("Reach")){
                reachNum++;
            }
        }

・ Display results

        System.out.println("BINGO:"+bingoNum+"\nREACH:"+reachNum);

Miscellaneous impressions

・ The usage of arrays has become complicated. ・ There seems to be an algorithm that seems to be more efficient (regular expressions, etc.) ・ I feel that writing such a small program in Java is something different. ・ It would be interesting to implement it in a different language or algorithm, set the bingo card size to about 10000, and compare the speeds. ・ It would be interesting to simulate using random numbers to find out the relationship between the number of reach and bingo and the size of the bingo card.

Github link

https://github.com/johejo/Bingo

Recommended Posts

Bingo judgment program
We have refactored the "Bingo Judgment Program"
[Ruby] Leap year judgment program
Calculator program
Calculator program
Ruby payroll program
FizzBuzz program (2 types)
Bingo judgment program