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

Why is my class variable rewriting itself after an unrelated method runs?

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

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

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

Main.java

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

Code.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;
  }
}

Game.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 因为那是static,Code 的所有实例共享同一个变量。您需要避免可变 static 成员。

另一个提示是要么有一个方法return一个值,要么改变状态(它的输入,或者它自己的实例,this),但要避免两者都做。

"Why is my class variable rewriting itself after an unrelated method runs?"

因为,其实不无关系。您通过将变量和方法声明为 static 创建的 "mess" 导致代码不同部分之间不必要的耦合。

很难说这里的正确解决方案是什么,因为您的代码因重写而变得如此混乱,以至于很难辨别原始 "design intent"。

我的建议是重新开始。您现在应该更清楚地了解需要什么功能。你需要做的是重做对象设计,让每个class都有一个明确的目的。 (MainGame class 是有道理的,但 Code 似乎是功能和状态的混搭,没有一致的目的。)