programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Number Guessing Game based on Matching Positions? o_O

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
Hey all, I've been posed a pretty difficult variation on the typical Number Guessing Game, where you have to think up a secret number, then have the computer guess your number. If the computer's number is greater than yours, then you tell it so, and if it is less than yours, you tell it so, usually via user input. Typically, to optimize guessing, you program it so that it picks the midrange number. In this case, we are using four digits.

However, instead of user input based on less than/greater than, the computer tries to guess a (4-digit) number (between 1000 and 9999), and then you state how many digits in the guess match your secret number. A match must have the same digit at the same position. Notice that the only information the computer receives is whether there is a match or not, and how many matches there are. It does NOT know WHAT digit is matching. I am pretty baffled by this program, but I have quite a few ideas.

There are 2 methods I am using:
public int myGuessIs() - returns a guess, and
public void updateMyGuess(int nmatches) - updates the previous guess based on the number of matches and prepares for the next guess

So I am thinking we should use an ArrayList to construct the numbers from 1000 to 9999, first, using a for loop, and we'll call the variable "range" of type int.
Then, I presume we'll need indices for the ArrayList at some point to refer to the numbers. This gets the typical "i" and it is an int also.
Assuming this construction of the list works, I'd want to update the guess based on the user-input number of matches (nmatches). If it is zero, than for a four digit number abcd, I cannot have "a" in the thousands place, "b" in the hundreds place, "c" in the tens place, or "d" in the ones place. I would then somehow have to come up with a way to eliminate all of those possibilities from my ArrayList. In retrospect, the "int" type may be totally useless to tell me about decimal places. Maybe if I somehow converted the int to an int[], or a String, and then used some String-breaking methods, and somehow identified the "places"...I really haven't the faintest inkling of where to go with this. Some inspiration would be AWESOME. Don't give it all away though :P

So far, I've got what appears below. You'll notice I've slashed in some questions and comments; I'm pretty noob so answers to those would also be MUCH appreciated

THANK YOU VERY MUCH!

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:

Can I tell you one more algorithm or you want to update yours?

Ranch Hand
Posts: 479
• Number of slices to send:
Optional 'thank-you' note:
I think you've started out in the wrong direction here.

The problem states that these are four-digit numbers, but in fact they are being treated like strings -- if the problem said you were going to use combinations of A, B, C, and D, or 4 pictures, or the four seasons, it would not change the game significantly.

So a list of 9,000 numbers is not necessary -- i.e., it is not necessary to store all the possible combinations of numbers for the game.

Try thinking of item being guessed as a string. The target string and the guess string must be 4 characters long, and evaluating a game guess involves looking at each specific position of the the target and guess to see if they match. I invite you to look at the method substring() of the java class String as a way to determine what character is in a specific position.

One other suggestion: you already have myGuessIs() to return a guess; make the job of returning a guess from the user a separate mini-project from other logic. You can write myGuessIs() to return a guess that you just write into the code, and then use that to work on your logic for evaluating the guess, without having to futz with user input. Separately, you can write code to return user input without bothering to evaluate that input as a guess. I'm suggesting you do one part of that first and get that code running, before you attempt the other. Pick either one you like. But separating the two tasks will make it less frustrating than trying to do them both together.

rc

Marshal
Posts: 76845
366
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Please avoid long lines in code (you could miss out a lot of those //comments) and use the code button. Since you are new, I have edited your post and you can see how much better it looks.

 A wop bop a lu bob a womp bam boom. Tutti frutti ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising