January 31st, 2013, 03:34 AM

Eliminating numbers in Arraylist
Basically this is our prompt:
Write a Java program that implements a numberguessing game as follows:
1. You think of a 4digit secret integer between the range of 1000 and 9999 (e.g. 5432).
2. The program takes a guess (e.g. 1234).
3. You tell the program how many digits in the guess match your secret number. A match must have the same digit at the same position.
4. The program performs some updates, and takes another guess.
5. You tell the program that the guess has 2 matching digits
6. The program repeats step 4, until it guesses the correct secrete number, in which case you will tell the program that all 4 digits match.
So I understand that we are supposed to make a loop where the program gives a random number between 1000 and 9999 but say the first guess is 1234 while your number is 5678. The program would then return 0 matches. This means any number between 1000 and 1999, or with x2xx, xx3x, xxx4 cannot be a match and should be eliminated. Likewise with if your next guess is 2478, then there's 2 matching your number. This thus eliminates the number 5478 from the pool of numbers to guess from because the 478 part match 2478 and that's too many matching numbers. So basically if the program says 2 matching numbers, then the leftover pool can only have no more than 2 numbers the same (eg 24xx or x47x or xx78 etc).
I was wondering how exactly do I go about eliminating the numbers from my arraylist that started out as 10009999? Technically the available pool of numbers can be reduced by a lot after the first few rounds but i'm not sure how to remove all those elements.
January 31st, 2013, 06:39 AM

What is the arraylist used for?
January 31st, 2013, 06:56 AM

You could just simply replace the elements of the array that you want to eliminate with 999 or any other number that is not in the range (1000 9999), and just do a simple check for the length of the array element. in case if you need to free some memory I think you can use null as a value.
January 31st, 2013, 06:58 AM

Originally Posted by Gemoroyshik
You could just simply replace the elements of the array that you want to eliminate with 999 or any other number that is not in the range (1000 9999), and just do a simple check for the length of the array element.
Let's say this problem is not for "words" with length 4, but for words with length n (for example n=100). You would not save all "words" with length 100 would you?
January 31st, 2013, 07:43 AM

if you would need for your program the exact same sequence of the chars you would need to save all 100 chars.
2 dimensional arrays may help you store 100 chars line.
January 31st, 2013, 09:09 AM

i was thinking that you start off with a super big array (10009999) so like 8999 elements. Then with the first guess, you can start removing elements as you scan through the arraylist and comparing the matches. What I'm really lost on is how exactly to go about the scanning process and the removing.
this is the hint we got
prepare an ArrayList that contains all available guesses. Initially this ArrayList will contain all integers between 1000 to 9999 inclusive. The algorithm then randomly picks a number from the ArrayList as the first guess, let's call it myguess. The user then compares it with her secret number, and provides the number of matches, let's call it nmatches. Next, the program will scan through all numbers currently in the ArrayList, compare each of them with myguess, and remove those that do not result in the same number of matches as nmatches. Intuitively, the program tries to eliminate numbers that cannot possibly be the correct answer. After this round, the ArrayList contains all remaining possible numbers but will become shorter, and the program will repeat the same steps as above, until it either succeeds in guessing, or the ArrayList becomes empty, indicating that the user must have miscalculated the number of matches at some point.
So i was thinking maybe use an if statement that start off with if matches=0 then you'd remove so and so elements. But then, I have no idea what parameters to set for the remove method
January 31st, 2013, 09:20 AM

The hint you got is a perfect start for pseudocode. As for removing the item(s) from the arraylist safely, use an iterator:
Code:
Iterator<int> it = possibleNumbers.iterator();
while(it.hasNext())
{
// do fancy stuff here
}
January 31st, 2013, 09:34 AM

i didn't program in java for a long time now, but might wanna look into java api, you can find something like this.
int array[9000] ={};// you can populate that array with a for loop.
for(i=0;i<9000;i++){
array[i]=i+1000;
}
to remove you need to look through api i don't remember what was the way of doing it. i just remember of using one of java's methods that could tell you each char in the each array instance. Then you would probably need to use a double loop to check through each int of the array an replace those that you need to be removed with null. don't forget to check if the array[n] is empty or not. I miss Java. When is it due to?
Maybe I could look through api on the weekends. But you should really do it yourself.