2017-02-19 2 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

あなたは、この場合には、 "1" にされるであろう、あなたの最初の推測としてtokencolors[0]を使用しています。 説明したように、equalsを使用すると、何も削除されません(すべての可能性が「1 1 1」という形式なので)。

containsを使用している場合は、反復処理中にリストを変更している場合があります。これは悪い考えです。 がiの各値を分析:

I = 0

guesses.get(0)== "1"

を "1" 削除しように、 "1" を含みます。推測[1 2、2 1、2 2]今ある

I = 1

guesses.get(1)== "2 1"

"2 1" ので、 "1" を含みます削除します。推測私はこの時点では= 2

i >= guesses.size() [1 2、2 2]であるため、ループは終了します。

使用イテレータ

あなたがリストで物事を除去するためのイテレータを使用する必要があります。

for (Iterator<String> it = guesses.iterator(); it.hasNext();) { 
    String item = it.next(); 
    if (item.contains(guess)) { 
     iterator.remove(); 
    } 
} 
関連する問題