gpt4 book ai didi

java - java中的Mastermind游戏

转载 作者:行者123 更新时间:2023-12-01 08:05:00 25 4
gpt4 key购买 nike

package edu.blastermind.model;

import java.util.Random;

/**
* Text-based version of the Mastermind game. The hints are simpler in that you
* know the position of correct pegs (but you know nothing at all about
* correctly-colored pegs in wrong positions).
*
* @author
*
*/
public class BlasterMindGame {

private static final int NUM_PEGS = 5;
private String secret;

/**
* Creates a new game with a randomized secret consisting of some
* combination of the letters: A, B, C, D, and/or E
*/
public BlasterMindGame() {
Random rng = new Random();

this.secret = "ABCDE";
// TODO 7: for-loop to create the secret string
StringBuffer guess = new StringBuffer("");
for (int i = 0; i < NUM_PEGS - 1; i++) {
char currentChar = guess.charAt(i);
String cs = currentChar + "";
currentChar = (char) ((char) (currentChar));
Character A = guess.charAt(0);
Character B = guess.charAt(1);
Character C = guess.charAt(2);
Character D = guess.charAt(3);
Character E = guess.charAt(4);
if (cs.matches("0")) {
guess.append(secret.charAt(A));
}else if (cs.matches("1")) {
guess.append(secret.charAt(B));
}else if (cs.matches("2")) {
guess.append(secret.charAt(C));
}else if (cs.matches("3")) {
guess.append(secret.charAt(D));
}else if (cs.matches("4")) {
guess.append(secret.charAt(E));
}
}
}

/**
* Tries to guess the secret.
*
* @param guess
* a 5-character string made up of only A, B, C, D, or E. Must be
* exactly 5 characters long.
*
* @return a 5-character string made up of the characters - and O,
* indicating: '-' for an incorrect match in that position 'O' for a
* correct match in that position
*/
public String getEasyHint(String guess) {

String hint = "";

return hint;
}

/**
* Lets us know if we've guessed the secret or not.
*
* @param guess
* our guess (must be exactly 5 characters long)
* @return true if correct, false otherwise.
*/
public boolean isGuessCorrect(String guess) {

return this.secret.equals(guess);
}
}
package edu.westga.blastermind.controllers;

import java.util.Scanner;

import edu.westga.blastermind.model.BlasterMindGame;

public class BlasterMind {

public static void main(String[] args) {

BlasterMindGame game = new BlasterMindGame();
Scanner kb = new Scanner(System.in);
int turns = 1;

while(true){
System.out.println("Enter a guess:");
String guess = kb.next();
if (!game.isGuessCorrect(guess)){
System.out.println("You guessed incorrect!");
String input = kb.next();
turns++;
} else if (game.isGuessCorrect(guess)) break;
}
// TODO 6: write the game simulation loop


System.out.printf("You won in %d turns\n", turns);
}

}

大家好,我正在用java编写mastermind游戏,这是一个猜弦游戏。这个游戏中的 secret 不是数字,而是由大写字母 A、B、C、D 和 E 组成的 5 个字符的字符串。当我运行 main 方法时,我得到:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.charAt(Unknown Source)
at edu.westga.blastermind.model.BlasterMindGame.<init>(BlasterMindGame.java:29)
at edu.westga.blastermind.controllers.BlasterMind.main(BlasterMind.java:11)

while 循环应该: •只要猜测不正确就会运行在体内:

  • 告诉用户猜测不正确
  • 从扫描仪获取下一个猜测
  • 增加圈数我认为我有这个权利(如果我没有,请告诉我)并且 for 循环应该是:

  • 从 0 迭代到钉子数量(注意静态常量 NUM_PEGS)

  • 对于每次迭代:• 获取 0 到 NUM_PEGS-1 之间的随机数(请参阅 Random 的 nextInt(int n) 方法以获取帮助)
    • 如果数字为 0,则将“A”附加到 this.secret
    • 如果数字为 1,则将“B”附加到 this.secret
    • 等等。分别为 2、3、4 和 C、D、E

出于某种原因,当我运行该程序时,我收到上述错误,并且我不知道如何更改它以使其正常工作。任何帮助表示赞赏。提前致谢。

最佳答案

您需要将字符串传递到字符串缓冲区。

 StringBuffer guess = new StringBuffer("BCDEA");//Exception Won't occur

关于java - java中的Mastermind游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447926/

25 4 0
文章推荐: java - 如何从 Map 中提取整数数组的长度?
文章推荐: python - 从 Sphinx 中的 .rst 格式的文件导入多个表
文章推荐: jquery - 尝试使用 jQuery 在
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com