Я пытаюсь закодировать игру Mastermind, но вместо того, чтобы пользователь взломал код компьютера, компьютер должен взломать код пользователя. У меня возникают проблемы с методом ответа, когда я пытаюсь удалить возможные коды, которые не могут быть правильными. Пример, который я тестировал, заключался в том, что секретный код - 223, а токены - 1,2 и 3, а позиция - 3. Компьютер сначала догадывается 1 1 1, что, я говорю, неверно. Затем компьютер должен удалить все строки, содержащие «1» из ArrayList, но он вообще ничего не удаляет. Что я сделал не так?Удаление строк из 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);
}
}
}
Интересно. Я просто пытался содержать, и он удалил примерно половину строк, которые он должен был использовать. Если я использую токены 1 и 2 и использую позиции 2, попытка удалить строки, содержащие 1, оставляет меня с [1 2, 2 2]. –