gpt4 book ai didi

java - 使用一些数组和循环的简化策划游戏

转载 作者:行者123 更新时间:2023-12-01 19:28:07 24 4
gpt4 key购买 nike

这是我为类(class)设计的一个项目。我仍然是一个初学者,所以这个主题只应该涵盖字符串、循环和数组。如果您不熟悉游戏,高级版本是this 。不过,我的任务是制作一个更简单的版本。以下是此版本的规则。

The program starts by asking the first player, the code maker, to enter a pattern to be used for the game. The pattern is 4 letters long and each letter represents a color (R is red, G is green).
To simplify the game, there are only two colors that can be used, red and green. So for example, the user may enter RRGR to represent Red Red Green Red, or they could enter GGGG to represent Green Green Green Green. To simplify, you can assume that the pattern will only consist of R’s and/or G’s, the code maker will not enter incorrect characters or symbols.However, you must check that the code maker entered exactly 4 character (that it is 4 characters long, not 5 and not 3). If they do not, you need to prompt until they do. The code maker might enter upper or lowercase letters and you should be able to handle it.

The program also asks for a maximum number of guesses that is allowed. Now the game can begin.The code breaker now guesses the pattern to try to win the game. If they cannot do so by the maximum number of guesses, they lose. To help the code breaker, your program will give feedback after each guess. Specifically,for each color that is in the exact correct location, it will show a black peg. For each color that is not in the correct location, but is in the pattern, it will show a white peg. See the sample interactions for examples of this. Detailed requirements and hints:

Here is a sample of how it should work:

Code maker, please enter a pattern of 4 colors (the possible colors are R=red and G=green):

GGGR

What is the maximum number of guesses allowed for this game?

4

Okay, now it's the code breaker's turn. You have 4 chances to guess the pattern. Enter a guess:

RRRR

The code maker sets out 1 black pegs and 0 white pegs.

Enter a guess:

GGRG

The code maker sets out 2 black pegs and 2 white pegs

Enter a guess:

GGGR

You got the pattern! The code breaker wins!

到目前为止我在哪里

首先我想计算黑钉。最好的方法是使用 FOR 循环将字符串输入转换为数组。有两个主要数组,需要解决的原始模式,以及用户所做的所有尝试的尝试数组。然后我们使用 FOR 循环来比较数组内容,并在每次尝试匹配时添加一个黑色的钉子。

这是我关注的代码(计算黑色引脚)。到目前为止,我的问题是我无法让引脚与每个数组内容相匹配。基本上,它总是产生 4 个引脚,即使有时应该是 3 个或 2 个引脚。如果需要,整个未完成的代码都在这部分下面。

String pattern = keys.nextLine().toUpperCase(); // this is used to enter the original pattern
String attempt = keys.nextLine().toUpperCase(); // this is to enter an attempt

String pattern_array [] = new String [5];
String attempt_array [] = new String [5];

int i;
int black_pegs = 0;
for (i=0; i<pattern.length(); i++) {
pattern_array[i] = pattern.substring(i,(i+1));
}
for ( i=0; i<attempt.length();i++) {
attempt_array[i] = attempt.substring(i,i+1);
}
for (int x=0; x<4; x++) {
if (pattern_array[i]==attempt_array[i]) {
black_pegs++;
}
}

这是我迄今为止所拥有的代码(如果您愿意,请随意查看并指出其他内容)

import java.util.Scanner;
public class CopyOfAssignment5 {
public static void main(String[] args) {
Scanner keys = new Scanner(System.in);
System.out.println("Codemaker, please enter a pattern of 4 colors (the possible colors are R=red and G=green");
String pattern = keys.nextLine().toUpperCase();
System.out.println("What is the maximum number of guesses for this game?");
int number_of_guesses = keys.nextInt();
int turns = number_of_guesses + 1;
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("Okay, now its the codebreaker's turn. You have " + number_of_guesses
+ " chances to guess the pattern");
System.out.println("Enter a Guess:");

int white_pegs = 0, black_pegs = 0;
String pattern_array[] = new String[5];
String attempt_array[] = new String[5];
String attempt = null;

while (turns > 0) { // while turns are not zero
attempt = keys.nextLine().toUpperCase(); // then keep displaying the scanner input for an attempt.
turns--;
}
if (attempt.equals(pattern)) { // if you get the correct patter, then you win. Loops stops.
System.out.println("You got the pattern! The codebreaker wins!");
}
if (turns == 0 && !(attempt.equals(pattern))) {
System.out
.println("The codemaker sets out " + black_pegs + " black pegs and " + white_pegs + " white pegs.");
System.out.println("Sorry, that was your last chance. You lost.");
} else if (turns < turns) {
System.out
.println("The codemaker sets out " + black_pegs + " black pegs and " + white_pegs + " white pegs.");
System.out.println("Enter a Guess:");
}

int i;
for (i = 0; i < pattern.length(); i++) {
pattern_array[i] = pattern.substring(i, (i + 1));
}
for (i = 0; i < attempt.length(); i++) {
attempt_array[i] = attempt.substring(i, i + 1);
}
for (int x = 0; x < 4; x++) {
if (pattern_array[i] == attempt_array[i]) {
black_pegs++;
}
}
}
}

最佳答案

这是我的实现。我不会解释代码,因为您已经接受了答案。也许您仍然想将您的代码与我的进行比较。也许其他人会解决这个问题并从下面的代码中获得一些好处。

public class MstrMind {
private static final char BLACK = 'B';
private static final char WHITE = 'W';
private static final int CODE_LENGTH = 4;

private static String getCode(Scanner stdin) {
System.out.println("Code maker, please enter a pattern of " + CODE_LENGTH + " colors (the possible colors are R=red and G=green):");
String code = stdin.nextLine();
while (!isCodeValid(code)) {
System.out.println("Entered code does not contain " + CODE_LENGTH + " colors.");
System.out.println("Code maker, please enter a pattern of " + CODE_LENGTH + " colors (the possible colors are R=red and G=green):");
code = stdin.nextLine();
}
return code.toUpperCase();
}

private static int getGuesses(Scanner stdin) {
System.out.print("What is the maximum number of guesses allowed for this game? ");
int guesses = stdin.nextInt();
stdin.nextLine();
return guesses;
}

private static boolean guess(Scanner stdin, String code) {
System.out.print("Enter a guess: ");
String guess = stdin.nextLine();
char[] flags = new char[CODE_LENGTH];
int blackCount = 0;
int whiteCount = 0;
for (int i = 0; i < CODE_LENGTH; i++) {
if (guess.charAt(i) == code.charAt(i)) {
flags[i] = BLACK;
blackCount++;
}
else {
for (int j = 0; j < CODE_LENGTH; j++) {
if (guess.charAt(j) == code.charAt(i) && flags[j] == 0 && i != j) {
flags[j] = WHITE;
whiteCount++;
}
}
}
}
boolean guessed = blackCount == CODE_LENGTH;
if (!guessed) {
System.out.printf("The code maker sets out %d black pegs and %d white pegs.%n",
blackCount,
whiteCount);
}
else {
System.out.println("You got the pattern! The code breaker wins!");
}
return guessed;
}

private static boolean isCodeValid(String code) {
return code != null && code.length() == CODE_LENGTH;
}

private static void playGame(Scanner stdin, String code, int guesses) {
int guess = guesses;
boolean guessed = false;
while (guess > 0 && !guessed) {
guessed = guess(stdin, code);
guess--;
}
if (!guessed) {
System.out.println("Code breaker failed to break the code. Code maker wins.");
}
}

public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
String code = getCode(stdin);
int guesses = getGuesses(stdin);
System.out.printf("Okay, now it's the code breaker's turn. You have %d chances to guess " +
"the pattern.%n",
guesses);
playGame(stdin, code, guesses);
}
}

关于java - 使用一些数组和循环的简化策划游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com