Page 1 of 1

PostPosted: Wed May 17, 2006 6:20 am
by winhax
Hi all,
I hope someone can shed some light on my possible dilemma. At the end of chapter 4 of the <u>Palm Programming for the Absolute Beginner</u> book, The last two challenges are for making the classic "I am thinking of a number" game. Now I had no problems creating it from the programs perspective. What I'm having trouble with is creating it from the user's view. In other words, the user thinks of a number between 1 and 100 and then the computer "guesses" what it is. Maybe I'm doing too much work at this point but is the computer (PDA) supposed to know and remember what guesses it made or is it supposed to just keep guessing until it guesses correctly? I can't seem to get a simple algorithim for storing values that it already guessed and also not to guess a lower (high) value than it's last guess. I'd really appreciate anyones' help on this because I can't figure it out for the life of me.



PostPosted: Wed May 17, 2006 11:29 am
by mgreen
You could do it either way (remember past guesses or not). There are lots of ways for the program to remember a series of values. What have you learned so far from the book along these lines? In terms of the computer guessing ... You can use a "pseudo-random" number generator or you can use the user interaction. If the user has to tap the screen to get the computer to provide a number, the amount of time waiting for the user can be a nearly random number, particularly if you use the remainder from a division by 100. You can also multiply the time by a moderately large prime number, then take the remainder

PostPosted: Wed May 17, 2006 6:03 pm
by winhax
OK here is what I have so far...

<b><font color="blue">//iatoanu
Classic I am thinking of a number game
Jason Puehler, 05/13/06

@cid "User";
@name "UserG";
@dbname "RandomG";
@ver "0.1";
@category "Games";

//main variables
string Fname;
int PalmG;
int PalmH;
int PalmL;
string UserA;

gn() {
//start gn()
UserA = gets("Correct(C)/Higher(H)/Lower(L)?");
UserA = strupr(UserA);
if (UserA == "C") {
alert("I knew it!");
} else if (UserA == "H") {
PalmH = 100 - random(PalmG) > PalmG;
return gn();
} else if (UserA == "L") {
PalmL = random(PalmG) + 1 < PalmG;
return gn();
} else {
alert("Incorrect Entry, Please try again.");
return gn();
} //end if
} //end gn

splash() {
//start splash() function
title("I am Thinking of a number...");
puts(" Hi, Welcome to my game.");
puts(" Think of a number between 1 and 100.");
puts(" I bet my program can guess your number");
puts(" Tap the screen to continue...");
Fname = getsd("What's your first name?","Jason");
}//end splash

main() {
PalmG = random(100) + 1;
} //end main</font id="blue"></b>

PostPosted: Wed May 17, 2006 6:27 pm
by sangahm
Your way of having the PDA guess the number is very inefficient. If you want to go that way, you may want to create an array of past guesses so that a guess is not repeated.

Otherwise, think about a binary approach where the PDA guesses by dividing the field of guesses in half each time. This would be most efficient.

In other words, if the field of possibilities starts at 0 to 100 and the number is "x", the first guess would be 50 (half of the field). The next guess would be either 50 to 100, or 0 to 50 depending on if the guess was high or low. If the guess was low, then you know the answer is between 50 to 100, so the guess would be 75. If the guess was high, then the guess would be between 0 to 50, so the guess would be 25. One thing to think about is after the first guess, the "new max" or high boundary to guess keeps changing after each guess. (In the above example, the new max would be either 50 or 100 depending on whether the guess was high or low). Keep that guess going until you home in on the answer.

You can add the variability I think you are looking for by making the next guess be a random number *between* the last guess and the new_max.

That way the only things that you would have to keep track of was the current_guess, next_guess, new_max and result_of_guess (correct, higher, lower).

The function would keep looping until the correct answer was guessed.

Now all you have to do is put the above into code. ;)