flashbangpro Posted October 20, 2011 Share Posted October 20, 2011 Hi I am still learning PHP and have a project due. I haven't had any PHP classes so I am struggling with this project. It is suppose to be a text only output pinball simulation. It calculates the players skill level randomly, there are 4 bumpers and for banks, it calculates how many times each bumper and bank is hit and calculates the score outputting all info in text no formatting. My code looks like it should work but doesn't output correctly and I can't figure out way. Here is the output : Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 39 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 39 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 42 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 42 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 45 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 45 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 48 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 48 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 51 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 51 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 54 Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 54 Welcome To God Mode! Ball Launched! Notice: Undefined variable: skillLevel in C:\xampp\htdocs\pinball.php on line 64 Bumper 4 Hit. Bumper 2 Hit. Bank 2 Hit! Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 441 Damn You Suck!!!! Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank1Pegs in C:\xampp\htdocs\pinball.php on line 907 Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank1 in C:\xampp\htdocs\pinball.php on line 905 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank2Pegs in C:\xampp\htdocs\pinball.php on line 915 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank2 in C:\xampp\htdocs\pinball.php on line 913 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank3Pegs in C:\xampp\htdocs\pinball.php on line 923 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank3 in C:\xampp\htdocs\pinball.php on line 921 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: bank4Pegs in C:\xampp\htdocs\pinball.php on line 931 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: bank4 in C:\xampp\htdocs\pinball.php on line 929 Notice: Undefined variable: score in C:\xampp\htdocs\pinball.php on line 946 Notice: Undefined variable: bumper1Hits in C:\xampp\htdocs\pinball.php on line 951 Notice: Undefined variable: bumper2Hits in C:\xampp\htdocs\pinball.php on line 952 Notice: Undefined variable: bumper3Hits in C:\xampp\htdocs\pinball.php on line 953 Notice: Undefined variable: bumper4Hits in C:\xampp\htdocs\pinball.php on line 955 GAME OVER! Your Score: Number Of Pegs Left In Bank 1: 6; Number Of Pegs Left In Bank 2: ; Number Of Pegs Left In Bank 3: 6; Number Of Pegs Left In Bank 4: 6; Number Bumper 1 Hit: Number Bumper 2 Hit: Number Bumper 3 Hit: Number Bumper 4 Hit: Refresh Page To Play Again! Here is the code: <?php $score; $bank1PegsNotHit; $bank2PegsNotHit; $bank3PegsNotHit; $bank4PegsNotHit; $bumper1Hits; $bumper2Hits; $bumper3Hits; $bumper4Hits; $skillLevel = 2 - (getRandom() * 0.01); $bank1[0] = true; $bank1[1] = true; $bank1[2] = true; $bank1[3] = true; $bank1[4] = true; $bank1[5] = true; $bank2[0] = true; $bank2[1] = true; $bank2[2] = true; $bank2[3] = true; $bank2[4] = true; $bank2[5] = true; $bank3[0] = true; $bank3[1] = true; $bank3[2] = true; $bank3[3] = true; $bank3[4] = true; $bank3[5] = true; function launch() { $player; switch ($skillLevel) { case $skillLevel < 1.1: $player = " First Timer"; break; case $skillLevel < 1.2: $player = " Beginer"; break; case $skillLevel < 1.3: $player = " Regular"; break; case $skillLevel < 1.4: $player = " Advanced"; break; case $skillLevel < 1.5: $player = " Pro"; break; case $skillLevel < 1.6: $player = " Olympic "; break; default: $player = " God "; break; } echo "Welcome To<strong>$player</strong>Mode!<br />Ball Launched!<br /><br />"; switch ($i = $skillLevel * getRandom()) { case $i <= 5: hole(); break; case $i <= 20: flipper(); break; case $i <= 25: bumper4(); break; case $i <= 30: bumper3(); break; case $i <= 65: bumper1(); break; default: bumper4(); break; } } function bumper1() { global $score; $score+= 100; global $bumper1Hits; $bumper1Hits += 1; echo "<br />Bumper 1 Hit."; switch ($i = getRandom()) { case $i <= 25: bumper2(); break; case $i <= 50: bumper3(); break; case $i <= 60: bumper4(); break; case $i <= 70: bank1(); break; case $i <= 75: bank2(); break; case $i <= 80: bank3(); break; case $i <= 95: flipper(); break; default: hole(); break; } } function bumper2() { global $score; $score+= 100; global $bumper1Hits; $bumper1Hits += 1; echo "<br />Bumper 2 Hit."; switch ($i = getRandom()) { case $i <= 25: bumper1(); break; case $i <= 35: bumper3(); break; case $i <= 60: bumper4(); break; case $i <= 65: bank1(); break; case $i <= 75: bank2(); break; case $i <= 80: bank4(); break; case $i <= 95: flipper(); break; default: hole(); break; } } function bumper3() { global $score; $score+= 500; global $bumper1Hits; $bumper1Hits += 1; echo "<br />Bumper 3 Hit."; switch ($i = getRandom()) { case $i <= 25: bumper1(); break; case $i <= 35: bumper2(); break; case $i <= 60: bumper4(); break; case $i <= 65: bank1(); break; case $i <= 90: bank3(); break; case $i <= 95: flipper(); break; default: hole(); break; } } function bumper4() { global $score; $score+= 500; global $bumper1Hits; $bumper1Hits += 1; echo "<br />Bumper 4 Hit."; switch ($i = getRandom()) { case $i <= 10: bumper1(); break; case $i <= 35: bumper2(); break; case $i <= 60: bumper3(); break; case $i <= 65: bank2(); break; case $i <= 90: bank4(); break; case $i <= 95: flipper(); break; default: hole(); break; } } function bank1() { echo "<br />Bank 1 Hit!"; switch ($i = getRandom()) { case $i <= 16: if ($bank1[0]) { global $score; $score+= 10; global $bank1; $bank1[0] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 32: if ($bank1[1]) { global $score; $score+= 10; global $bank1; $bank1[1] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 48: if ($bank1[2]) { global $score; $score+= 10; global $bank1; $bank1[2] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 64: if ($bank1[3]) { global $score; $score+= 10; global $bank1; $bank1[3] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 80: if ($bank1[4]) { global $score; $score+= 10; global $bank1; $bank1[4] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 96: if ($bank1[5]) { global $score; $score+= 10; global $bank1; $bank1[5] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 99; flipper(); break; default: hole(); break; } } function bank2() { echo "<br />Bank 2 Hit!"; switch ($i = getRandom()) { case $i <= 16: if ($bank2[0]) { global $score; $score+= 10; global $bank2; $bank2[0] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 32: if ($bank2[1]) { global $score; $score+= 10; global $bank2; $bank2[1] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 48: if ($bank2[2]) { global $score; $score+= 10; global $bank2; $bank2[2] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 64: if ($bank2[3]) { global $score; $score+= 10; global $bank2; $bank2[3] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 80: if ($bank2[4]) { global $score; $score+= 10; global $bank2; $bank2[4] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 96: if ($bank2[5]) { global $score; $score+= 10; global $bank2; $bank2[5] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 99; flipper(); break; default: hole(); break; } } function bank3() { echo "<br />Bank 3 Hit!"; switch ($i = getRandom()) { case $i <= 16: if ($bank3[0]) { global $score; $score+= 10; global $bank3; $bank3[0] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 32: if ($bank3[1]) { global $score; $score+= 10; global $bank3; $bank3[1] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 48: if ($bank3[2]) { global $score; $score+= 10; global $bank3; $bank3[2] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 64: if ($bank3[3]) { global $score; $score+= 10; global $bank3; $bank3[3] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 80: if ($bank3[4]) { global $score; $score+= 10; global $bank3; $bank3[4] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 96: if ($bank3[5]) { global $score; $score+= 10; global $bank3; $bank3[5] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 99; flipper(); break; default: hole(); break; } } function bank4() { echo "<br />Bank 4 Hit!"; switch ($i = getRandom()) { case $i <= 16: if ($bank4[0]) { global $score; $score+= 10; global $bank4; $bank4[0] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 32: if ($bank4[1]) { global $score; $score+= 10; global $bank4; $bank4[1] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 48: if ($bank4[2]) { global $score; $score+= 10; global $bank4; $bank4[2] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 64: if ($bank4[3]) { global $score; $score+= 10; global $bank4; $bank4[3] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 80: if ($bank4[4]) { global $score; $score+= 10; global $bank4; $bank4[4] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 96: if ($bank4[5]) { global $score; $score+= 10; global $bank4; $bank4[5] = false; } switch ($i = getRandom()) { case $i <= 40; bumper1(); break; case $i <= 80: bumper2(); break; case $i <= 95: flipper(); break; default: hole(); break; } break; case $i <= 99; flipper(); break; default: hole(); break; } } function flipper() { switch ($i = $skillLevel * getRandom()) { case $i <= 10: hole(); break; case $i <= 15: bank3(); break; case $i <= 20: bank4(); break; case $i <= 25: bank1(); break; case $i <= 30: bank2(); break; case $i <= 47: bumper1(); break; case $i <= 64: bumper2(); break; case $i <= 81: bumper3(); break; default: bumper4(); break; } } function hole() { echo "<br />Damn You Suck!!!!<br />"; $bank1Pegs; $bank2Pegs; $bank3Pegs; $bank4Pegs; for ($i = 0; $i <= 5; $i++) { if (!$bank1[$i]) { $bank1Pegs += 1; } } for ($i = 0; $i <= 5; $i++) { if (!$bank2[$i]) { $bank2Pegs += 1; } } for ($i = 0; $i <= 5; $i++) { if (!$bank3[$i]) { $bank3Pegs += 1; } } for ($i = 0; $i <= 5; $i++) { if (!$bank4[$i]) { $bank4Pegs += 1; } } global $bank1PegsNotHit; $bank1PegsNotHit = $bank1Pegs; global $bank2PegsNotHit; $bank1PegsNotHit = $bank2Pegs; global $bank3PegsNotHit; $bank3PegsNotHit = $bank3Pegs; global $bank4PegsNotHit; $bank4PegsNotHit = $bank4Pegs; echo <<< EOD <br /><h4>GAME OVER!</h4> Your Score: <strong>$score</strong> Number Of Pegs Left In Bank 1: <strong>$bank1PegsNotHit;</strong> Number Of Pegs Left In Bank 2: <strong>$bank2PegsNotHit;</strong> Number Of Pegs Left In Bank 3: <strong>$bank3PegsNotHit;</strong> Number Of Pegs Left In Bank 4: <strong>$bank4PegsNotHit;</strong> Number Bumper 1 Hit: <strong>$bumper1Hits</strong> Number Bumper 2 Hit: <strong>$bumper2Hits</strong> Number Bumper 3 Hit: <strong>$bumper3Hits</strong> Number Bumper 4 Hit: <strong>$bumper4Hits</strong> Refresh Page To Play Again! EOD; } function getRandom() { return rand(1, 100); } launch(); ?> Any help fixing it would be greatly appreciated. Thank You All Quote Link to comment https://forums.phpfreaks.com/topic/249491-pinball-simulation/ Share on other sites More sharing options...
KevinM1 Posted October 20, 2011 Share Posted October 20, 2011 You need to pass the appropriate values into your functions via their argument lists. In other words: function launch($skillLevel) { // rest of function } // AND, when you want to invoke it: launch($skillLevel); Also, NEVER use 'global'. Pass parameters through a function's argument list. That's why it's there. In fact, stop using whatever source you're using to learn PHP. 'Global' is a clear sign of doing it wrong, which calls into doubt everything else your resource is trying to instruct. Regarding functions (and just about everything else), go to the manual for more information: functions Quote Link to comment https://forums.phpfreaks.com/topic/249491-pinball-simulation/#findComment-1280957 Share on other sites More sharing options...
flashbangpro Posted October 20, 2011 Author Share Posted October 20, 2011 Thank you I'll try and see if i can get that to work. I have just been trying to learn from online tutorials. Quote Link to comment https://forums.phpfreaks.com/topic/249491-pinball-simulation/#findComment-1280960 Share on other sites More sharing options...
flashbangpro Posted October 21, 2011 Author Share Posted October 21, 2011 Its all working now except the calculation of the totaling of the accumulated score. I'm not sure if I was suppose to pas $score through something specific or what, But thank you very much for the help you have provided and I will look for better learning resources. Quote Link to comment https://forums.phpfreaks.com/topic/249491-pinball-simulation/#findComment-1280994 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.