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

时间:2016-12-30 00:37:14

标签: java class-variables

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

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

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

Main.java

root.

Code.java

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

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

2 个答案:

答案 0 :(得分:3)

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

另一个提示是让一个方法返回一个值,或者改变状态(其输入或它自己的实例,this),但避免同时执行这两个操作。

答案 1 :(得分:1)

  

&#34;为什么我的类变量会在不相关的方法运行后重写自己?&#34;

因为,实际上,它并不是无关的。 &#34;混乱&#34;通过将变量和方法声明为static而创建的,会导致代码的不同部分之间出现不必要的耦合。

很难说正确的解决方案在这里是什么,因为你的代码因重写而变得如此困惑,以至于难以辨别出原始的&#34;设计意图&#34;。

我的建议是重新开始。您现在应该更清楚地了解所需的功能。您需要做的是重做对象设计,以便每个类都有明确的目的。 (MainGame类是有意义的,但Code似乎是功能和状态的混搭,没有连贯的目的。)