 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
05-19-2009, 03:24 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Bingo game
PHP Code:
<?php
session_start(); // Previous entries are stored in SESSION variable so they last for each page
// Need to add a do while so it does not repeat the same number twice in one game
// Game is underway
if ($_POST['submitted']) {
$num = rand(1,90); // Get a random number within the Bingo limits
$_SESSION['prevNum'][$count] = $num; // Store $num in the array of previous numbers
$count = $_POST['count'] + 1; // Counter + 1 each new number (so array location changes)
// Display the previously used numbers on screen for reference
echo '<p>Previous numbers: ';
foreach ($_SESSION['prevNum'] as $v) {
echo $v . ', ';
}
echo '</p>';
// Display the current number in massive text
echo '<div align="center" style="font-size:20em;color:green;">' . $num . '</div>';
} else { // Game hasn't started yet so show different text instead of bingo number
echo '<div align="center" style="font-size:10em;color:red;">Click the button to start!</div>';
$_SESSION['prevNum'] = array();
}
?>
<div align="center">
<form action="bingo.php" method="post">
<input type="submit" name="submit" value="Get next number!" />
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="count" value="<?php if (isset($count)) { echo $count; } ?>" />
</form>
</div>
<?php
// If the game has started, display a link to start a new game
if ($_POST['submitted']) {
echo '<p><a href="bingo.php" alt="Start a new game">Click here to start<br />a new bingo game</a></p>';
}
?>
I'm writing a bingo machine for someone at school. This is my rough draft of the code. It's working pretty well apart from the previous numbers which should stack up along the top like:
Previous numbers: 4, 23, 54, 32
Whereas it's instead just doing:
Previous numbers: [put current number here (changes each go)]
Any help please?
|
|
|
05-19-2009, 03:28 PM
|
#2 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
You are calling the session value set before you increment its key, thus it overwrites everytime.
My I also suggest you wrap the post check in isset, to avoid the undefined key warnings.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
05-19-2009, 07:23 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
So obvious duh.
Which post set though? The one in the form is wrapped in them, which other ones might need it?
|
|
|
05-19-2009, 07:31 PM
|
#4 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Just flip it.
PHP Code:
$num = rand(1,90); // Get a random number within the Bingo limits
$count = $_POST['count'] + 1; // Counter + 1 each new number (so array location changes)
$_SESSION['prevNum'][$count] = $num; // Store $num in the array of previous numbers
|
|
|
|
05-19-2009, 08:27 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Quote:
|
Which post set though? The one in the form is wrapped in them, which other ones might need it?
|
I'd be tempted to do both:
PHP Code:
if ($_POST['submitted']) {
PHP Code:
if (isset($_POST['submitted'])) {
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
05-20-2009, 09:25 AM
|
#6 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
It's getting there
Now what I need to do is sort the array so instead of showing the previous numbers like:
3, 43, 12, 76
It will show them in order like:
3, 12, 43, 76
I know of the sort function but I'm having a little trouble using it with this 2d array. Can anybody help me with what the syntax should be using the above $_SESSION['prevNum'] array?
Thanks,
|
|
|
05-20-2009, 10:36 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
You don't need to pass the count POST variable, just change:
PHP Code:
$count = $_POST['count'] + 1; // Counter + 1 each new number (so array location changes) $_SESSION['prevNum'][$count] = $num; // Store $num in the array of previous numbers
to this (with sort):
PHP Code:
$_SESSION['prevNum'][] = $num; // Store $num in the array of previous numbers sort($_SESSION['prevNum']);
then you can remove the superfluous form field below.
also you could replace the foreach with implode:
PHP Code:
echo '<p>Previous numbers: ', implode(', ', $_SESSION['prevNum']), '</p>';
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
05-20-2009, 11:10 AM
|
#8 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
So what exactly does
PHP Code:
$_SESSION['prevNum'][] = $num
do?
Before it was doing:
$_SESSION['prevNum'][0] = $num
$_SESSION['prevNum'][1] = $num
$_SESSION['prevNum'][2] = $num
$_SESSION['prevNum'][3] = $num
$_SESSION['prevNum'][4] = $num
etc etc
What does it do now?
|
|
|
05-20-2009, 11:24 AM
|
#9 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Essentially the same, $array[] is the shortcut for array_push, all you are doing is pushing an element onto the end of the array (essentially using the array as a stack)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
05-20-2009, 11:27 AM
|
#10 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Brilliant - so you don't need the counter because the push method creates the next logical slot and puts the number into it? Is that right?
|
|
|
05-20-2009, 11:37 AM
|
#11 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Precisely 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
05-20-2009, 11:52 AM
|
#12 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Ok, one last thing now
The number generated is completely random, and therefore it can reproduce the same number during the length of the game - those bingo fans will know that this is no good.
I need a way to check the new number against the array, and if it has been used already to get a new random number.
This is what I had in mind:
PHP Code:
$match = TRUE; // Assume that the number has already been used
do {
$num = rand(1,90); // Get a random number
// Check to see if number has been used
foreach ($_SESSION['prevNum'] as $v) {
if ($v == $num) { $match = TRUE; }
else { $match = FALSE; }
}
} while ($match == TRUE);
Should this work?
|
|
|
05-20-2009, 11:53 AM
|
#13 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
UPDATE: It doesn't work - I get results like:
1, 1, 3, 4, 6, 8, 22, 25, 26, 27, 27, 32, 32, 37, 39, 42
What am I doing wrong here?
Thanks
|
|
|
05-20-2009, 11:59 AM
|
#14 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
In your snippet, you would need to break the foreach loop when it encounters a collision, otherwise the next check may not result in a collision, thus over writing $match.
A better solution would be to use in_array to check if a value exists.
PHP Code:
do{ $num = rand(1, 90); while(in_array($num, $_SESSION['prevNum']);
or similar
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
05-20-2009, 12:08 PM
|
#15 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
I would eliminate all the numbers from the array before you then pick a new random number. Otherwise, if you're performing a do while loop to find the numbers that haven't been used, then when that gap becomes very small, PHP will loop many times before it finds an unused number.
At the moment you're using rand, which may well be fine, but it's difficult to eliminate values from it as there is no blacklist argument for the rand function, which would be nice.
Hopefully that's a good concept to build upon using range. It gives you the same random numbers, but builds an array of values so we can then eliminate. To give an example of removing the numbers that have been used:
Edit: To visualise the problem of using rand, imagine, in all probability, how many loops PHP would have to perform to find the very last missing number in a range of 1 to 90.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
05-20-2009, 12:37 PM
|
#16 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Thanks sketchMedia - working a treat! I will post the full code up in a minute or two.
Wildhoney - I like the idea behind that, and I know it could loop hundreds of times in order to find the last number, so I might give that a go.
|
|
|
05-20-2009, 12:53 PM
|
#17 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Quote:
|
Thanks sketchMedia - working a treat! I will post the full code up in a minute or two.
|
No problem.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
05-20-2009, 01:08 PM
|
#18 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Here's my finished code:
PHP Code:
<?php
session_start(); // Previous entries are stored in SESSION variable so they last for each page
// Game is underway
if (isset($_POST['submitted'])) {
// Make sure the script breaks if every number has been used
if (sizeof($_SESSION['prevNum']) > 90) {
echo '<div align="center" style="font-size:10em;color:red;">Game finished!</div>';
echo '<p><a href="bingo.php" alt="Start a new game">Click here to start<br />a new bingo game</a></p>';
exit();
}
echo sizeof($_SESSION['prevNum']);
// Keep getting a random number until one that hasn't been used yet is got
do {
$num = rand(1,90); // Get a random number
} while (in_array($num, $_SESSION['prevNum'])); // If it's already in array, try again
$_SESSION['prevNum'][] = $num; // Store $num in the array of previous numbers
sort($_SESSION['prevNum']); // Sort the array to be ascending
// Display the previously used numbers on screen for reference
echo '<p>Previous numbers: ';
foreach ($_SESSION['prevNum'] as $v) {
echo $v . ', ';
}
echo '</p>';
// Display the current number in massive text
echo '<div align="center" style="font-size:20em;color:green;">' . $num . '</div>';
} else { // Game hasn't started yet so show different text instead of bingo number
echo '<div align="center" style="font-size:10em;color:red;">Click the button to start!</div>';
$_SESSION['prevNum'] = array(); // Reset the array for the new game
}
?>
<div align="center">
<form action="bingo.php" method="post">
<input type="submit" name="submit" value="Get next number!" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</div>
<?php
// If the game has started, display a link to start a new game
if (isset($_POST['submitted'])) {
echo '<p><a href="bingo.php" alt="Start a new game">Click here to start<br />a new bingo game</a></p>';
}
?>
Thanks guys 
|
|
|
05-20-2009, 01:08 PM
|
#19 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Looking at the following code which, I think at least, calculates the amount of times a loop has to occur before it finds a gap, it doesn't seem THAT bad. I'd have expected much worse.
I say I think because on several runs I've seen PHP finds some of the last few numbers in less than 10 loops, and I am sure probability tells us that's not right. I could be wrong. Perhaps my code is wrong.
php Code:
$iLooped = 0; $aUsed = array(); do{ do { $iNumber = rand(1, 90); $iLooped++; } while (in_array($iNumber, $aUsed)); $aUsed[] = $iNumber; printf("%d: %d<br />\n", count($aUsed), $iLooped); $iLooped = 0; }while (count($aUsed) < 90);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
10-18-2012, 10:11 AM
|
#20 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|