[Homework] Creating Game Program

This program is supposed to function as a "guessing game". A user guesses a number between 1000 and 9999 and the program guesses a number, the user then tells how many digits in the program's "guess" match the number of digits the user was thinking of. Then it guesses again with a modified result, until it guesses the correct answer or determines the number can't exist. My program is built to have all possible numbers (1000 through 9999) in an arraylist and, upon getting the number of matches, to delete any numbers that don't match it. I go about this by putting the number that the computer guessed along with the first possible number into two arrays of length four (so each digit has its own index), then counting the number of matches and deleting the current number if the matches aren't the same as what the user input. However, it simply doesn't go about the process like I believe it should. I would appreciate any help you could give in hinting to me where the problem is.

The second half of the program (separated by a line break) was given to us and shouldn't be changed, it is what we work off of to create the methods and functions.

My question is why my program isn't running as intended. I've gone through it multiple times on paper and cannot find what the problem is, however I think I've found where it is.

In the updateMyGuess method, the program looks at a possible number (the first available in the arrayList of possible numbers) and compares it with the number it "guessed". It's supposed to look at each individual digit, and if they match increment the variable "matches". updateMyGuess was passed a variable nmatches, which the user provided (telling how many digits matched, both in number and position), and at the end it checks if nmatches and matches are the same. If they are not, that means the number couldn't be one that the user guessed so it deletes it from the array. Then it continues with the next possible number.

However, I've found that it deletes all but the thousands (2000, 3000, 4000, etc), note that this doesn't include 1000. For example:

I'm thinking of the number 9876
The computer "guesses" 8888.
I tell it there's one match.
It should delete all numbers that don't have exactly one match, meaning it will not delete 7841 (one match) but will delete 7848 (two matches).
However, as I said it deletes all but the thousands, not including 1000.

You have a lot of moding going on that probably is excluding these numbers.

I would add println statements throughout the method to get your head around what is really happening.

But looking closer, you have a lot of complex logic of comparisons and code that extracts the digits. I would pull the digit extraction out into a separate method that you can verify that that piece operations correctly. Doing this will also make the code more readable and reduce the complexity.

Since you are not doing any math with the numbers except to find the digits, why don't you just keep the input as a string and compare it character by character? This eliminates the most confusing and error-prone section of the code, as well as improving readability.

Comments on this post

jzd agrees
: I almost mentioned that as well, but I didn't want to bring up to many points at once and loose the OP.

Thanks for your suggestions, guys. I actually followed Spyder's advice and it works almost perfectly. The one strange thing is, though, that at 10 guesses (I've checked multiple times), it gives me an IllegalArgumentException as follows:

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at Assignment2.myGuessIs(Assignment2.java:25)
at Assignment2.main(Assignment2.java:70)

I'm currently checking, but from what I'm reading in the exception it seems like it might be a problem in my understanding of how the Random method I'm using works, and not just what I typed in. So if you could offer any information while I'm looking my program over I would appreciate it. Here is my updated and almost working code.

EDIT: Soon after posting this, I found what I think to be the problem. In the myGuessIs method, at the point where there was only one possible number left, it was choosing a random number with zero as the parameter for rand.NextInt. I changed it to the following and so far everything is working. I'll keep checking it but I believe it should be all set. Thanks so much! I can't believe I spent hours trying to make it work the way with the mod when I could have just changed it to a string. Any other suggestions for simply making my code "cleaner"?