gpt4 book ai didi

java - 为什么在不相关的方法运行后我的类变量会重写自身?

转载 作者:行者123 更新时间:2023-11-29 08:39:27 25 4
gpt4 key购买 nike

所以我正在编写一个基本的 MasterMind 游戏,它...主要是功能性的。但是,它表现出奇怪的行为,我不确定为什么。

这个想法是,定义代码及其行为的是一个文件,游戏玩法是另一个文件,Main 只是创建一个新游戏并开始玩。当我初始化游戏时,计算机按预期创建了一个新的随机字符串 4(“密码”);但是一旦我得到用户猜测的输入,它似乎将密码重写为我输入的任何内容。此外,我评估匹配的方法根本不起作用,但考虑到密码不断变化意味着它没有被设置为开始,我不确定为什么。

以下所有三个类。为什么我在 Game 中的类变量设置不正确并且无法被其他方法访问?

主.java

class Main { 
public static void main(String[] args) {
Game newGame = new Game();
newGame.play();
}
}

代码.java

import java.util.Random;
import java.util.HashMap;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Set;

import java.lang.Math;
import java.lang.StringBuilder;

class Code {
private static HashMap<String,String> PEGS;
private static ArrayList<String> pegStrings;
protected static String secretCodeString;


public static void main(String[] args) {

}

public Code(String input){
this.secretCodeString = input;
}

public Code(){
randomize();
}


//literally just creates the peghash
public static void setPegs(){
PEGS = new HashMap<String,String>();

PEGS.put("C","c");
PEGS.put("Y","y");
PEGS.put("R","r");
PEGS.put("P","p");
PEGS.put("O","o");
PEGS.put("G","g");
}

//turns the pegs ito something randomize can use
public static ArrayList<String> makePegArray(){
setPegs();

pegStrings = new ArrayList<String>();

Collection<String> pegValues = PEGS.values();
Object[] pegObjects = pegValues.toArray();

for (int i = 0; i < pegObjects.length; i++){
pegStrings.add(pegObjects[i].toString());
}

return pegStrings;
}

// sets Class Variable secretCode to a four letter combination
public static Code randomize(){
secretCodeString = new String();

Random rand = new Random();
int randIndex = rand.nextInt(makePegArray().size());

for (int i = 0; i < 4; i++){
randIndex = rand.nextInt(makePegArray().size());
secretCodeString = secretCodeString.concat(makePegArray().get(randIndex));
}

Code secretCode = parse(secretCodeString);
return secretCode;
}

public static Code parse(String input) {
setPegs();
makePegArray();

String[] letters = input.split("");
StringBuilder sb = new StringBuilder();

for (String letter : letters) {
if (pegStrings.contains(letter)) {
sb.append(letter);
} else {
System.out.println(letter);
throw new RuntimeException();

}
}

String pegListString = sb.toString();
Code parsedCode = new Code(pegListString);
//System.out.println(parsedCode);
return parsedCode;

}

public int countExactMatches(Code guess){
String guessString = guess.secretCodeString;

int exactMatches = 0;

String[] guessArray = guessString.split("");

String[] winningCodeArray = (this.secretCodeString).split("");

for(int i = 0; i < 4; i++){

if(guessArray[i] == winningCodeArray[i]){
exactMatches++;
}
}
return exactMatches;
}

public int countNearMatches(Code guess) {

String guessString= guess.secretCodeString;

HashMap<String,Integer> guessCount = new HashMap<String,Integer>();
HashMap<String,Integer> secretCodeCount = new HashMap<String,Integer>();

Set<String> codeKeys = guessCount.keySet();

int matches = 0;
int keys = guessCount.keySet().size();


String[] keyArray = new String[keys];



for(int i = 0; i < guessString.length(); i++) {
//removes character from string
String codeCharacter = String.valueOf(guessString.charAt(i));
String guessShort = guessString.replace(codeCharacter,"");

//counts instances of said character
int count = guessString.length() - guessShort.length();

guessCount.put(codeCharacter, count);
}

for(int i = 0; i < secretCodeString.length(); i++) {
//removes character from string
String winningString = this.secretCodeString;

String winningCodeCharacter = String.valueOf(winningString.charAt(i));
String winningCodeShort = guessString.replace(winningCodeCharacter,"");

//counts instances of said character
int count = winningString.length() - winningCodeShort.length();

secretCodeCount.put(winningCodeCharacter, count);
}

for (int i = 0; i < keys; i++) {
codeKeys.toArray(keyArray);
String keyString = keyArray[i];

if (secretCodeCount.containsKey(keyString)) {
matches += Math.min(secretCodeCount.get(keyString), guessCount.get(keyString));
}
}

int nearMatches = matches - countExactMatches(guess);

return nearMatches;
}
}

游戏.java

import java.util.Scanner;

class Game {

protected static Code winningCode;

public static void main(String[] args){

}

public Game(){
winningCode = new Code();
}

protected static Code getGuess() {

Scanner userInput = new Scanner(System.in);

int count = 0;
int maxTries = 5;
while(true){
try {
String codeToParse = userInput.next();
Code guess = Code.parse(codeToParse);
return guess;

} catch(RuntimeException notACode) {
System.out.println("That's not a valid peg. You have " + (maxTries - count) + " tries left.");
if (++count == maxTries) throw notACode;
}
}


}

protected static void displayMatches(Code guess){

int nearMatches = winningCode.countNearMatches(guess);
int exactMatches = winningCode.countExactMatches(guess);

System.out.println("You have " + exactMatches + " exact matches and " + nearMatches + " near matches.");
}

protected static void play(){
int turnCount = 0;
int maxTurns = 10;

System.out.println("Greetings. Pick your code of four from Y,O,G,P,C,R.");

while(true){
Code guess = getGuess();
displayMatches(guess);

if (guess == winningCode) {
System.out.print("You win!!");
break;
} else if (++turnCount == maxTurns) {
System.out.print("You lose!!");
break;
}
}
}
}

最佳答案

在每次猜测时,您调用 Code.parseCode.parse 创建一个新的 Code (new Code(pegListString) ;) 并且该构造函数设置了 secretCodeString,因为它是静态的,所以 Code 的所有实例都共享同一个变量。您需要避免使用可变的 static 成员。

另一个技巧是要么让方法返回一个值,要么改变状态(它的输入或它自己的实例,this),但要避免两者都做。

关于java - 为什么在不相关的方法运行后我的类变量会重写自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389118/

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