chris57828 Posted May 14, 2011 Share Posted May 14, 2011 Hi everyone, this is quite a big one, and I am new to programming. In the code below you will see I have created a 'Lucky Lotto Dip'. The display function allows the user to choose how many draws they want then by way of the shuffle() 6 numbers are generated randomly for each line. I have two questions. (1) I have managed by way of an if statement to automatically check the first number in the array $draw, how do I get it to check for 2 match's, 3 match's and so on up to 6 match's. And secondly if the first number is matched I have managed to get it to repeat itself and if to show a match, but how do I get it to repeat itself on the line below the selected numbers? I know this is difficult but if anyone can help I will be very grateful! <?php class Game { public $draw = array(10,25,36,3,12,13); public $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49); public $age; public $count; function __construct($sub1, $sub2) { $this->name = $sub1; $this->age = $sub2; return $this->name;age; } function checkAge() { if ($this->age <18) { return "Sorry $this->name, you are too young to gamble"; } else { return "Hi $this->name welcome to Lucky Lotto Dip, how many draws would you like?"; } } function display($sub3) { shuffle ($this->numbers); $this->count = $sub3; echo "<pre>"; echo "*********************"; echo "<br />"; echo "* Lucky Lotto *"; echo "<br />"; echo "*********************"; while ($this->count >0) { echo "<br />"; echo "* "; for ($j =0; $j <6; $j++) // For loop if ($this->numbers[$j] <10) { echo " " . $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0]) { echo $this->numbers[$j] = $this->draw[0]; } } else { echo $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0]) { echo $this->numbers[$j] = $this->draw[0]; } } echo "* "; echo "<br />"; echo "*********************"; echo "<br />"; echo " "; echo "<br />"; echo "*********************"; $this->count --; shuffle ($this->numbers); } } } $object2 = new Game("Chris", 36); echo $object2->checkAge(); echo $object2->display(4); ?> Quote Link to comment https://forums.phpfreaks.com/topic/236391-arrays-and-formatting/ Share on other sites More sharing options...
wildteen88 Posted May 14, 2011 Share Posted May 14, 2011 Try <?php class Game { public $draw = array(10,25,36,3,12,13); public $numbers = array(); public $age; public $count; public $draws = array(); function __construct($sub1, $sub2) { $this->name = $sub1; $this->age = $sub2; $this->numbers = range(1, 49); return $this->name;age; } function checkAge() { if ($this->age <18) { return "Sorry $this->name, you are too young to gamble"; } else { return "Hi $this->name welcome to Lucky Lotto Dip, how many draws would you like?"; } } function genDrawLines($lines) { // loop for as many draws required for($i = 0; $i < $lines; $i++) { $this->draws[$i] = array(); $nKeys = array_rand($this->numbers, 6); // pick random numbers foreach($nKeys as $key) $this->draws[$i][] .= $this->numbers[$key]; // add numbers to draw } } function display($sub3) { // generate the numbers for each draw $this->genDrawLines($sub3); echo "<pre>"; echo "*********************"; echo "<br />"; echo "* Lucky Lotto *"; echo "<br />"; echo "*********************"; // loop through the draws foreach($this->draws as $key => $drawNumbers) { $matches = 0; echo "<br />"; echo "* "; for ($j =0; $j <6; $j++) { if ($drawNumbers[$j] < 10) echo " " . $drawNumbers[$j] . " "; else echo $drawNumbers[$j] . " "; // increment matches when the number within the current draw exists within the draw array if (in_array($drawNumbers[$j], $this->draw)) $matches++; } echo "* "; echo "<br />"; echo "*********************"; echo "<br />"; echo "\t\t$matches matches"; // display matchers echo "<br />"; echo "*********************"; } } } $object2 = new Game("Chris", 36); echo $object2->checkAge(); echo $object2->display(4); ?> Quote Link to comment https://forums.phpfreaks.com/topic/236391-arrays-and-formatting/#findComment-1215339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.