Archive Forums (lite version).  Gaming, Game Development, and Anime community!


PDA

View Full Version : PHP Help.


neozf
09-05-2007, 04:22 PM
Scraped the original post since a few changes were made.
Source code for the game.
/*HIGHER OR LOWER*/
if (preg_match("/^\:\^HoL$/i",$command)){
$playing=true;$currentnumber=rand(1,100);
$point=0;
$turn=1;
fputs($socket, "PRIVMSG $ex[2] :The current number is $currentnumber, do you think the next one would be Higher or Lower?($turn/10)\n");
}
if ($playing==true&&$turn<10){
$nextnumber=rand(1,100);
if (preg_match("/Higher/i",$text) && $nextnumber > $currentnumber){
$turn=$turn+1;
fputs($socket, "PRIVMSG $ex[2] :Correct, you get one point, the next number is $nextnumber, Higher or Lower?($turn/10)\n");
$point = $point+1;
$currentnumber = $nextnumber;
} elseif (preg_match("/Higher/i",$text) && $nextnumber < $currentnumber) {
$turn=$turn+1;
fputs($socket, "PRIVMSG $ex[2] :Incorrect, you don't get a point, the next number was $nextnumber, Higher or Lower?($turn/10)\n");
$currentnumber = $nextnumber;
} elseif (preg_match("/Lower/i",$text) && $nextnumber < $currentnumber){
$turn=$turn+1;
fputs($socket, "PRIVMSG $ex[2] :Corrent, you get one point, the next number is $nextnumber, Higher or Lower?($turn/10)\n");
$point = $point+1;
$currentnumber = $nextnumber;
} elseif (preg_match("/Lower/i",$text) && $nextnumber > $currentnumber) {
$turn=$turn+1;
fputs($socket, "PRIVMSG $ex[2] :Incorrect, you don't get a point, the next number was $nextnumber, Higher or Lower?($turn/10)\n");
$currentnumber = $nextnumber;
}
}
if ($turn==10){
if (preg_match("/Higher/i",$text) && $nextnumber > $currentnumber){
fputs($socket, "PRIVMSG $ex[2] :LAWLCorrect, you get one point, the next number was $nextnumber.\n");
$point = $point+1;
fputs($socket, "PRIVMSG $ex[2] :Game ended, you got $point Points.\n");
$playing=false;
} elseif (preg_match("/Higher/i",$text) && $nextnumber < $currentnumber) {
fputs($socket, "PRIVMSG $ex[2] :LAWLIncorrect, you don't get a point, the next number was $nextnumber. \n");
fputs($socket, "PRIVMSG $ex[2] :Game ended, you got $point Points.\n");
$playing=false;
} elseif (preg_match("/Lower/i",$text) && $nextnumber < $currentnumber){
fputs($socket, "PRIVMSG $ex[2] :LAWLCorrent, you get one point, the next number was $nextnumber.\n");
$point = $point+1;
fputs($socket, "PRIVMSG $ex[2] :Game ended, you got $point Points.\n");
$playing=false;
} elseif (preg_match("/Lower/i",$text) && $nextnumber > $currentnumber) {
fputs($socket, "PRIVMSG $ex[2] :LAWLIncorrect, you don't get a point, the next number was $nextnumber.\n");
fputs($socket, "PRIVMSG $ex[2] :Game ended, you got $point Points.\n");
$playing=false;
}
}

I also added a piece of code which will show the current turn and how many points you have.
What happens when I test it.
[21:43] <AppleBot> Hey guys!
[21:43] > ^hol
[21:43] <AppleBot> The current number is 64, do you think the next one would be Higher or Lower?(1/10)
[21:43] > ^turn lower
[21:43] <AppleBot> Corrent, you get one point, the next number is 39, Higher or Lower?(2/10)
[21:43] <AppleBot> 2 , 1
[21:43] > ^turn lower
[21:43] <AppleBot> Corrent, you get one point, the next number is 13, Higher or Lower?(3/10)
[21:43] <AppleBot> 3 , 2
[21:43] > ^turn lower
[21:43] <AppleBot> Incorrect, you don't get a point, the next number was 49, Higher or Lower?(4/10)
[21:43] <AppleBot> 4 , 2
[21:43] > ^turn lower
[21:43] <AppleBot> Incorrect, you don't get a point, the next number was 84, Higher or Lower?(5/10)
[21:43] <AppleBot> 5 , 2
[21:43] > ^turn lower
[21:43] <AppleBot> Corrent, you get one point, the next number is 38, Higher or Lower?(6/10)
[21:43] <AppleBot> 6 , 3
[21:43] > ^turn lower
[21:43] <AppleBot> Corrent, you get one point, the next number is 19, Higher or Lower?(7/10)
[21:43] <AppleBot> 7 , 4
[21:43] > ^turn lower
[21:43] <AppleBot> Incorrect, you don't get a point, the next number was 23, Higher or Lower?(8/10)
[21:43] <AppleBot> 8 , 4
[21:43] > ^turn lower
[21:43] <AppleBot> Incorrect, you don't get a point, the next number was 47, Higher or Lower?(9/10)
[21:43] <AppleBot> 9 , 4
[21:43] > ^turn lower
[21:43] <AppleBot> Incorrect, you don't get a point, the next number was 67, Higher or Lower?(10/10)
[21:43] <AppleBot> 10 , 4
[21:43] > ^turn lower
[21:43] <AppleBot> 10 , 4
^hol starts the game, ^turn checks the current turn and how many points you have. It stops when $turn gets to 10 since the second part of the source says to only do that bit if $turn<10, but it does not do the third part which it is meant to do once $turn==10. ^turn shows that the current turn is 10, which would mean $turn is 10, but for some reason it still doesn't work.

Anyone got an idea why this is?

Google
--

Walrii
09-07-2007, 08:19 PM
Maybe it is because php is goofy and the type of a variable can change willy nilly. :)

You could try
intval($turn, 10)==10
;the first 10 means that $turn is written in base 10
to force the $turn variable into the number? I dunno, I'm not a php expert. :)

Dazed_n_Confused
09-09-2007, 11:13 AM
In php, if $a="10", then $a == 10 is true. You can also do math with $a and it works fine.

You just need a "$nextnumber=rand(1,100);" after if ($turn==10){.
You set $nextnumber and $currentnumber equal, so none of the statements there are true.

Also, you can shorten up your code in a couple of ways. First, you only need to check for two winning conditions. You could then default to a third loosing condition. Second, $turn = $turn + 1 can be replaced with $turn++. You could also move $currentnumber = $nextnumber; to either above or below the "if ($playing==true&&$turn<10){ /*code*/ }" block instead of repeating it inside the if statement.

Theres a spelling error here:
fputs($socket, "PRIVMSG $ex[2] :LAWLCorrent, you get one point, the next

neozf
09-09-2007, 11:17 AM
Oh right, thanks Dazed, also, I didn't notice that spelling mistake lol.

Update: I just tested it and it works :D
Thanks Dazed +10 :D

Sim9
09-10-2007, 12:20 AM
Thanks for the advice, Dazed. +10