我正在尝试对Mastermind游戏进行编码,但计算机必须破解用户的代码,而不是用户破解计算机代码。我在响应方法中遇到问题,我试图删除可能无法正确的代码。我测试的例子是密码是223,代码是1,2和3,位置是3.计算机首先猜测1 1 1,我说这是不正确的。然后,计算机应删除包含" 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 :(得分: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);
}
}
}
..
}
答案 1 :(得分:0)
您使用tokencolors[0]
作为第一个猜测,在这种情况下,它将是“1”。
如果您使用equals
(因为所有可能性都具有格式“1 1 1”),这将导致无法删除任何内容,正如您所描述的那样。
如果使用contains
,那么在迭代时你会修改列表,这是一个坏主意。
分析i
的每个值:
guesses.get(0)==“1 1”
“1 1”包含“1”,因此删除。猜测现在是[1 2,2 1,2 2]
guesses.get(1)==“2 1”
“2 1”包含“1”,因此删除。猜测现在是[1 2,2 2]
此时i >= guesses.size()
,循环结束。
您应该使用迭代器来删除列表中的内容:
for (Iterator<String> it = guesses.iterator(); it.hasNext(); ) {
String item = it.next();
if (item.contains(guess)) {
iterator.remove();
}
}