概要
ファイルからビンゴカード情報と読み上げられた番号の情報を得てビンゴもしくはリーチ判定を下す。
条件
普通のビンゴゲームだと真ん中は初めから空いてるけどとりあえず今回は空いてないことにする。
入力ファイル
ビンゴカード
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.txt
1
7
13
19
2
3
4
ファイル読み出し
ファイル読み込みはこのページを参考
【Java】テキストファイル全体を読み込み文字列を返すメソッド
ビンゴ判定について
とりあえず思いついたあんまり賢くない方法
・ファイルの数値をsplitとかで切り出して二次元配列に突っ込む
        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]);
        }
・そこから縦の判定、横の判定、斜めの判定用に別の配列に取り出す
        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];
                }
            }
        }
・二つの配列(比較対象の配列と選択された配列)を引数として判定結果を返すメソッドを用意
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";
        }
}
・全パターンチェックして回る
        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++;
            }
        }
・結果を表示
        System.out.println("BINGO:"+bingoNum+"\nREACH:"+reachNum);
雑な感想
・配列の使い方が雑になってしまった
・もっと効率の良さそうなアルゴリズムがありそう(正規表現とか)
・こんな小規模なプログラムをJavaで書くのはなんか違う気がする
・違う言語とかアルゴリズムで実装してビンゴカードのサイズを10000くらいにして速度の比較したら面白そう
・乱数使ってシミュレーションしてリーチやビンゴの回数とかビンゴカードのサイズとかの関係とか調べるのも面白そう
Githubのリンク