猜32位纯数字Java

2.猜数字规则:系统生成随机选32位数字(例如859632587459632587412589658558745),参加游戏的人员同时猜测当前随机串对应位数上的数值。若某一人员猜测位数和对应位数的数字匹配,则表示此人此次此位猜测正确,此时平台随机数会重新生成,所有人重新猜新生成的随机数,各选手已经猜对的位次无需重新猜。若参加游戏的人员将1-32位所有位次都猜测正确过,此局游戏结束,此选手为获胜选手。

该回答引用ChatGPT

import java.util.*;

public class GuessNumberGame {
    private static final int NUMBER_LENGTH = 32;
    private static final int MAX_TRIES = 100;
    private static final int MAX_PLAYERS = 10;
    private static final Random random = new Random();

    private static String generateNumber() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER_LENGTH; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        // Generate the random number
        String numberToGuess = generateNumber();
        System.out.println("Number to guess: " + numberToGuess);

        // Initialize the game state
        boolean[] guessed = new boolean[NUMBER_LENGTH];
        int[] numTries = new int[MAX_PLAYERS];
        String[] playerNames = new String[MAX_PLAYERS];
        int numPlayers = 0;

        // Get player names
        Scanner scanner = new Scanner(System.in);
        while (numPlayers < MAX_PLAYERS) {
            System.out.print("Enter player name (or 'quit' to start the game): ");
            String name = scanner.nextLine();
            if (name.equalsIgnoreCase("quit")) {
                break;
            }
            playerNames[numPlayers++] = name;
        }

        // Start the game
        int numGuessed = 0;
        int numPlayersRemaining = numPlayers;
        while (numGuessed < NUMBER_LENGTH && numPlayersRemaining > 0) {
            // Get the next guess from each player
            for (int i = 0; i < numPlayers; i++) {
                if (numTries[i] < MAX_TRIES) {
                    System.out.print(playerNames[i] + ", enter your guess (32 digits): ");
                    String guess = scanner.nextLine();
                    if (guess.length() != NUMBER_LENGTH) {
                        System.out.println("Your guess must have 32 digits!");
                        continue;
                    }
                    // Check the guess
                    for (int j = 0; j < NUMBER_LENGTH; j++) {
                        if (guess.charAt(j) == numberToGuess.charAt(j) && !guessed[j]) {
                            guessed[j] = true;
                            numGuessed++;
                            numTries[i]++;
                        }
                    }
                    // Check if the player has won
                    if (numGuessed == NUMBER_LENGTH) {
                        System.out.println(playerNames[i] + " wins!");
                        return;
                    }
                    // Check if the player has used up all their tries
                    if (numTries[i] == MAX_TRIES) {
                        System.out.println(playerNames[i] + " is out!");
                        numPlayersRemaining--;
                    }
                }
            }
        }
        // Game over
        System.out.println("Nobody wins!");
    }
}


不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^