2017-02-19 166 views
0

我試圖編寫Mastermind遊戲,但不是用戶破解計算機的代碼,計算機必須破解用戶的代碼。我在響應方法中遇到了問題,我嘗試刪除可能不正確的代碼。我測試的例子是密碼是223,令牌是1,2和3,位置是3.計算機首先猜測1 1 1,我說這是不正確的。然後,計算機應該從ArrayList中刪除所有包含「1」的字符串,但它不會刪除任何內容。我做錯了什麼?從ArrayList刪除字符串

import java.util.Scanner; 
import java.util.ArrayList; 

public class MMplayer { 
    static Scanner scan = new Scanner (System.in); 
    String[] tokencolors; //the tokens the user enters 
    ArrayList <String> possibleGuesses = new ArrayList <String>(); 
    String [] remainingGuess; //the number of remaining possible guesses 
    String lastGuess; //the last guess that was made 
    int guessNum = 0; //guess count 
    int positions = 0; 
    int ccpw; //"color correct, position wrong" 
    int ccpc; //"color correct, position correct" 

public static void main (String[] args){ 

    //This sets up the introduction so the player knows how to play 
    System.out.println("Hello, and welcome to Mastermind!"); 
    System.out.println(""); 
    System.out.println("You will choose a certain number of tokens to play with (these can be anything, like colors, names, or fruit).\n" 
      + "Then you will choose the number of those tokens that you'd like to use in the game (called the position)." 
      + "\nThe computer will then try to guess the correct name and location of your tokens. "); 
    System.out.println("\nPlease enter the number of tokens and their names (up to six tokens), and the number of positions (up to four)." 
      + "\nJust remember that to make it harder for the computer, pick more tokens than you actually want to use. "); 

    System.out.println(""); 
    System.out.println("Enter number of tokens now: "); 
    //number of tokens, makes sure it is at most 6 
    int aryLength = scan.nextInt(); 
    if (aryLength > 6){ 
     System.out.println("Please enter at most six tokens."); 
     System.exit(0); 
    } 

    System.out.println("Enter token names now, pressing enter after each: "); 
    String [] tokencolors = new String[aryLength]; 
    for (int i = 0; i < aryLength; i++){ 
     tokencolors[i] = scan.next(); 
    } 

    System.out.println("Enter number of positions now: "); 
    int position = scan.nextInt(); 
    if (position > 4){ 
     System.out.println("Please enter at most 4 positions."); 
     System.exit(0); 
    } 


    MMplayer player = new MMplayer(tokencolors, position); 
} 

public MMplayer(String[] cTokencolors, int cPositions) { 
    tokencolors = cTokencolors; 
    positions = cPositions; 

    holder(); 

} 
public void holder(){ 

    if (guessNum == 0){ //if this is the very first guess 
     if (positions == 1){ //if there is one position 
      for (int i = 0; i < tokencolors.length; i++){ 
       possibleGuesses.add(tokencolors[i]); 
      } 
     } 
     else if (positions == 2){ //if there are two positions 
      for (int i = 0; i < tokencolors.length; i++){ 
       for (int j = 0; j < tokencolors.length; j++){ 
        possibleGuesses.add(tokencolors[i] + " " + tokencolors[j]); 
       } 
      } 
     } 

     else if (positions == 3){ //if there are three positions 
      for (int i = 0; i < tokencolors.length; i++){ 
       for (int j = 0; j < tokencolors.length; j++){ 
        for (int k = 0; k < tokencolors.length; k++){ 
         possibleGuesses.add(tokencolors[i] + " " + tokencolors[j] + " " + tokencolors[k]); 

        } 
       } 
      } 

     } 
     else if (positions == 4){ //if there are four positions 
      for (int i = 0; i < tokencolors.length; i++){ 
       for (int j = 0; j < tokencolors.length; j++){ 
        for (int k = 0; k < tokencolors.length; k++){ 
         for (int l = 0; l < tokencolors.length; l++){ 
          possibleGuesses.add(tokencolors[i] + " " + tokencolors[j] + " " + tokencolors[k] + " " + tokencolors[l]); 
         } 
        } 
       } 
      } 
     } 
     else { 
      System.out.println("Number of positions is invalid."); 
      newGame(); 
     } 

     System.out.println("Guess: " + possibleGuesses.get(0)); 
     lastGuess = possibleGuesses.get(0); 
     guessNum++; 

     System.out.println("First enter how many tokens were right, but had the wrong position: "); 
     ccpw = scan.nextInt(); 
     System.out.println("Next, enter how many tokens were right and had the right position: "); 
     ccpc = scan.nextInt(); 

     response(ccpw, ccpc); 
    } 


} 

// public String[] nextMove() {// return the next guess 
// 
// } 


public void response(int colorsRightPositionWrong, int positionsAndColorRight) { 
    ccpw = colorsRightPositionWrong; 
    ccpc = positionsAndColorRight; 




    if (ccpc == positions){ 
     System.out.println("The computer has won!"); 
    } 
    else { 
     if (guessNum == 1){ 
      String guess = tokencolors[0]; 
      for (int i = 0; i < possibleGuesses.size(); i++){ 
       if (possibleGuesses.get(i).equals(guess)){ 
        possibleGuesses.remove(i); 
       } 
      } 
     } 
     System.out.println("Remaining guesses: " + possibleGuesses); 
    } 



} 

} 

回答

0
Based on my understanding of your description it appears that you have issue on the line shown below where you should check for contains instead instead of equals: 
if (guessNum == 1){ 
      String guess = tokencolors[0]; 
      for (int i = 0; i < possibleGuesses.size(); i++){ 
       //check for contains below? 
       if (possibleGuesses.get(i).equals(guess)){ 
        possibleGuesses.remove(i); 
       } 
      } 
     } 
.. 
} 
+0

有趣。我只是嘗試了包含,它刪除了大約一半的字符串。如果我使用標記1和2並使用位置2,則試圖刪除包含1的字符串將使我留下[1 2,2 2]。 –

0

您使用tokencolors[0]作爲你的第一個猜測,這將是 「1」,在這種情況下。 如您所描述的那樣,如果您使用equals(因爲所有可能性都具有格式「1 1 1」),則不會導致任何內容被刪除。

如果使用contains,會發生什麼情況是您在迭代時修改列表,這是一個糟糕的主意。 分析的i每個值:

I = 0

guesses.get(0)== 「1 1」

「11」 包含 「1」,所以排除。猜測現在[1 2,2 1,2 2]

I = 1

guesses.get(1)== 「1 2」

「2 1」 中包含 「1」,所以刪除。猜測現在[1 2,2 2]

I = 2

此時i >= guesses.size(),所以循環結束。

使用迭代

您應該使用迭代器在列表中刪除的東西:

for (Iterator<String> it = guesses.iterator(); it.hasNext();) { 
    String item = it.next(); 
    if (item.contains(guess)) { 
     iterator.remove(); 
    } 
}