And Bingo was his Name-O

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theyapps
Forum Newbie
Posts: 8
Joined: Tue Sep 06, 2011 12:47 am

And Bingo was his Name-O

Post by theyapps »

Ok I am racking my head I am creating a bingo game for fun... I have function that serves as my hopper. It works pretty good, but sometimes it doesn't have a letter. I would like the result to be something like "G54". I usually get something like that, occasionally I will just get "54".

Code: Select all

function thehopper() {
    $length = 1;
    $characters = 'BINGO';
    $letter = $characters[mt_rand(1, strlen($characters))];
	$draw = $letter . rand(1,100);
    return $draw;
}
$draw = thehopper();
echo $draw;
EDIT: SOLVED: I changed the function up and this seems to work better!

Code: Select all

function thehopper() {
$alpha = str_split('BINGO');
shuffle($alpha);
$draw = implode('', array_slice($alpha, 0, 1)); 
$draw = $draw . rand(1,100);
return $draw;
}
Dorin85
Forum Newbie
Posts: 20
Joined: Tue Aug 16, 2011 3:16 pm

Re: And Bingo was his Name-O

Post by Dorin85 »

Simplified:

Code: Select all

function thehopper() {
$alpha = str_split('BINGO');
shuffle($alpha);
return $alpha[0] . rand(1,100);
}
You'll probably want to store the previously called letter/number combinations so that duplicates aren't called.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: And Bingo was his Name-O

Post by twinedev »

Not sure where you are from and how bingo may be played there, but here is code specific to the US normal Bingo, where you have 15 possible numbers under each letter:

Code: Select all

function thehopper() {
	$alpha = str_split('BINGO');
	$intNumber = rand(1,75);
	$draw = $alpha[floor(($intNumber-1)/15)].'-'.$intNumber;
	return $draw;
}
This makes sure that for B you only get 1-15, I you get 16-30, etc. The letters are never randomized, just the actual number, and the appropriate letter for it's column is calculated.

-Greg
theyapps
Forum Newbie
Posts: 8
Joined: Tue Sep 06, 2011 12:47 am

Re: And Bingo was his Name-O

Post by theyapps »

Yah i just found this out my self lol i fixed this but not as gracefully as you haha
Post Reply