Hey there. I'm almost finished with this assignment which basically asks for the following. The user of the program thinks of a number from 1000 to 9999. The program makes an array of Strings with integers 1000-9999(which are converted to Strings), and randomnly picks a number from the range. The program then asks how many "matches" there are in the number, not worrying about the position of the number. Then what the program does is compares each number in the array with the computer's guess, and compares each char of every number to the guess. If there is a match, the match variable is incremented. If the "guessMatch" does not equal "nmatches", then that element in the array is removed.
So basically, the arraylist after each pass is reduced in size, until there is only 1 element left. Here is the code:
Code:
// fill in your information in the line below, and comment it out in order to compile
// Name: ??? Student ID: ???;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
public class Assignment2 {
private int totalGuess;
private ArrayList<String> guess;
private String myGuess;
private Random random;
public Assignment2 ( ){
totalGuess = 0;
myGuess = "";
guess = new ArrayList<String>();
random = new Random();
for(int i = 1000; i <= 9999 ; i++){ // Adds numbers 1000 to 9999 initially.
guess.add(Integer.toString(i));
}
}
public int myGuessIs() {
int index = random.nextInt(guess.size());
myGuess = guess.get((random.nextInt(guess.size()-1)));
totalGuess++;
if(guess.size() == 0)
myGuess = "-1";
return Integer.parseInt(myGuess);
}
public int totalNumGuesses() {
return totalGuess;
}
public void updateMyGuess(int nmatches) {
int guessMatches; // fill in code here
// update the guess based on the number of matches claimed by the user
for(int i =0 ; i < guess.size(); i++){
guessMatches = 0;
for(int j =0; j <= 3 ; j++){
if(myGuess.charAt(i) == guess.get(j).charAt(i))
guessMatches++;
}
if(nmatches != guessMatches)
guess.remove(i);
i+=-1;
}
}
// you shouldn't need to change the main function
public static void main(String[] args) {
Assignment2 gamer = new Assignment2( );
JOptionPane.showMessageDialog(null, "Think of a number between 1000 and 9999.\n Click OK when you are ready...", "Let's play a game", JOptionPane.INFORMATION_MESSAGE);
int numMatches = 0;
int myguess = 0;
do {
myguess = gamer.myGuessIs();
if (myguess == -1) {
JOptionPane.showMessageDialog(null, "I don't think your number exists.\n I could be wrong though...", "Mistake", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
String userInput = JOptionPane.showInputDialog("I guess your number is " + myguess + ". How many digits did I guess correctly?");
// quit if the user input nothing (such as pressed ESC)
if (userInput == null)
System.exit(0);
// parse user input, pop up a warning message if the input is invalid
try {
numMatches = Integer.parseInt(userInput.trim());
}
catch(Exception exception) {
JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
numMatches = 0;
}
// the number of matches must be between 0 and 4
if (numMatches < 0 || numMatches > 4) {
JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE);
numMatches = 0;
}
if (numMatches == 4)
break;
// update based on user input
gamer.updateMyGuess(numMatches);
} while (true);
// the game ends when the user says all 4 digits are correct
System.out.println("Aha, I got it, your number is " + myguess + ".");
System.out.println("I did it in " + gamer.totalNumGuesses() + " turns.");
}
}
The output is not what I want it to be, and I cannot figure out why. Can anyone provide some help?
Thanks!
-Bori
P.S - the main function is not to be changed.