You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现宾果Bingo游戏获胜判定优化与功能完善咨询

Java 5x5宾果游戏问题修复方案

核心修改点

  • 拆分玩家胜负判定逻辑,独立计算两位玩家的获胜状态,支持同时宣布双方获胜
  • 抽取单张棋盘的Bingo判定通用方法,消除重复代码,逻辑更易维护
  • 新增已呼叫号码存储集合,实现重复输入校验
  • 修复原代码中字符串用==比较的隐患,统一改为equals方法判断内容

修正后代码

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class BingoGame {
    private static final Scanner sc = new Scanner(System.in);
    // 存储已经呼叫过的号码
    private static final Set<Integer> calledNumbers = new HashSet<>();
    private static final String PLAYER1_NAME = "玩家1";
    private static final String PLAYER2_NAME = "玩家2";
    private static int choice;
    private static int i,j;

    // 新增:单张棋盘获胜判定通用方法
    private static boolean isBingo(String[][] card) {
        // 判断行
        for (int row = 0; row < 5; row++) {
            boolean rowWin = true;
            for (int col = 0; col < 5; col++) {
                if (!"XX".equals(card[row][col])) {
                    rowWin = false;
                    break;
                }
            }
            if (rowWin) return true;
        }
        // 判断列
        for (int col = 0; col < 5; col++) {
            boolean colWin = true;
            for (int row = 0; row < 5; row++) {
                if (!"XX".equals(card[row][col])) {
                    colWin = false;
                    break;
                }
            }
            if (colWin) return true;
        }
        // 判断主对角线
        boolean diag1Win = true;
        for (int idx = 0; idx <5; idx++) {
            if (!"XX".equals(card[idx][idx])) {
                diag1Win = false;
                break;
            }
        }
        if (diag1Win) return true;
        // 判断副对角线
        boolean diag2Win = true;
        for (int idx =0; idx <5; idx++) {
            if (!"XX".equals(card[idx][4-idx])) {
                diag2Win = false;
                break;
            }
        }
        return diag2Win;
    }

    // 重写后的bingoCheck方法
    public static void bingoCheck(String[][] card1, String[][] card2) {
        boolean p1Win = isBingo(card1);
        boolean p2Win = isBingo(card2);

        if (p1Win && p2Win) {
            System.out.print("Bingo! 两位玩家同时获胜!");
            return;
        }
        if (p1Win) {
            System.out.print("Bingo! 玩家1获胜!");
            return;
        }
        if (p2Win) {
            System.out.print("Bingo! 玩家2获胜!");
            return;
        }
        // 无人获胜继续输入
        userIn(card1, card2);
    }

    // 重写后的userIn方法
    public static void userIn(String[][] card1, String[][] card2) {
        String str = "";
        System.out.print("裁判呼叫号码(输入0退出游戏):");
        choice = sc.nextInt();

        // 退出逻辑
        if (choice == 0) {
            System.exit(0);
        }

        // 号码范围校验
        while (choice < 0 || choice > 25) {
            System.out.println("号码必须在1-25之间,请重新呼叫!");
            System.out.print("裁判呼叫号码(输入0退出游戏):");
            choice = sc.nextInt();
        }

        // 新增:重复号码校验
        while (calledNumbers.contains(choice)) {
            System.out.println("该号码已经呼叫过,请重新输入!");
            System.out.print("裁判呼叫号码(输入0退出游戏):");
            choice = sc.nextInt();
            // 重复输入时也要校验范围和退出逻辑
            if (choice == 0) {
                System.exit(0);
            }
            while (choice < 0 || choice > 25) {
                System.out.println("号码必须在1-25之间,请重新呼叫!");
                System.out.print("裁判呼叫号码(输入0退出游戏):");
                choice = sc.nextInt();
            }
        }
        // 合法号码存入已呼叫集合
        calledNumbers.add(choice);
        str = Integer.toString(choice);

        // 更新棋盘标记
        for (i = 0;i <=4; i++) {
            for (j = 0;j <=4; j++) {
                if (str.equals(card1[i][j])) {
                    card1[i][j] = "XX";
                }
                if (str.equals(card2[i][j])) {
                    card2[i][j] = "XX";
                }
            }
        }

        // 打印玩家1棋盘
        System.out.println(PLAYER1_NAME);
        for (i = 0; i < card1.length; i++) {
            for (j = 0; j < card1.length; j++) {
                System.out.printf(" %2s ", card1[i][j]);
            }
            System.out.print("\n");
        }

        System.out.print("\n");

        // 打印玩家2棋盘
        System.out.println(PLAYER2_NAME);
        for (i = 0; i < card2.length; i++) {
            for (j = 0; j < card2.length; j++) {
                System.out.printf(" %2s ", card2[i][j]);
            }
            System.out.print("\n");
        }

        // 判定胜负
        bingoCheck(card1, card2);
    }
}

内容的提问来源于stack exchange,提问作者Anas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:36:11