samu - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of samu.php

  1. <?php
  2. $m = 4; //dadu
  3. $n = 3; //pemain
  4. $point = 6;
  5. $switch = 1;
  6. $dadu = array("1", "2", "3", "4", "5", "6");
  7. $nilaiPoint = 1;
  8.  
  9. // var_dump($lemparanDadu);
  10.  
  11.  
  12.  
  13. // $_GET["debug"] ? print_r($game) : '';
  14. $game = null;
  15. $property["dadu"] = $dadu;
  16. $property["round"] = 1;
  17. $property["n"] = $n;
  18. $property["m"] = $m;
  19. $property["nilaiPoint"] = $nilaiPoint;
  20.  
  21. echo "Pemain = " . $property["n"] . ", Dadu = " . $property["m"];
  22. echo "<br>==================<br>";
  23.  
  24. if (is_null($game)) {
  25.     $jmlDaduKu = $property["m"];
  26.     // echo "jmlDaduKu" . $jmlDaduKu . "<br>";
  27.     for ($x = 1; $x <= $jmlDaduKu; $x++) {
  28.         $jmlDaduke = 1;
  29.         do {
  30.             // echo "The number is: $jmlDaduke <br>";
  31.             $suhefl[$jmlDaduke] = $property["dadu"][array_rand($property["dadu"], 1)];
  32.             $jmlDaduke++;
  33.         } while ($jmlDaduke <= $property["m"]);
  34.         //   print_r($suhefl);
  35.         //   exit();
  36.  
  37.         // echo "The number is: $x <br>";
  38.         $game["Pemain"]["#" . $x]["dadu"] = $suhefl;
  39.         $game["Pemain"]["#" . $x]["life"] = 1;
  40.         $game["Pemain"]["#" . $x]["point"] = 0;
  41.     }
  42. }
  43.  
  44. PlayGame($game, $property);
  45.  
  46.  
  47. function PlayGame($game, $property)
  48. {
  49.  
  50.  
  51.     // var_dump($game);
  52.     // var_dump($property);
  53.     // exit();
  54.  
  55.     // var_dump($game);
  56.     // exit();
  57.  
  58.     echo "Giliran <b>" . $property["round"] . "</b> lempar dadu:<br>";
  59.  
  60.     foreach ($game["Pemain"] as $key => $value) {
  61.  
  62.         // var_dump($key);
  63.  
  64.         /*Cek Point*/
  65.  
  66.         echo "<ul>";
  67.         echo "<li> Pemain " . $key . " (" . $game["Pemain"][$key]["point"] . ") : " . implode(" ", $game["Pemain"][$key]["dadu"]) . "</li>";
  68.         echo "</ul>";
  69.     }
  70.  
  71.  
  72.     // print_r($game["Pemain"]);
  73.  
  74.     echo "<br>Setelah evaluasi:<br>";
  75.     foreach ($game["Pemain"] as $key => $value) {
  76.         // var_dump($key);
  77.  
  78.         /*Cek Point*/
  79.         // var_dump($game["Pemain"][$key]["dadu"]);
  80.         foreach ($game["Pemain"][$key]["dadu"] as $kDadu => $vDadu) {
  81.             // $game["Pemain"][$key]["point"] = cekPoint($vDadu,$game["Pemain"][$key]["point"],$nilaiPoint);
  82.             if ("6" === $vDadu) {
  83.                 // echo "Point bertambah";
  84.                 $game["Pemain"][$key]["point"] = $game["Pemain"][$key]["point"] + $property["nilaiPoint"];
  85.                 // echo "Point dadu di buang";
  86.  
  87.  
  88.                 /*Cek Buang dadu*/
  89.                 if (($key6 = array_search("6", $game["Pemain"][$key]["dadu"])) !== false) {
  90.                     unset($game["Pemain"][$key]["dadu"][$key6]);
  91.                 }
  92.             }
  93.             //pindah dadu
  94.             if ("1" === $vDadu) {
  95.                 // echo "Dadu berpindah";
  96.                 // $game["Pemain"][$key]["dadu"] = $game["Pemain"][$key]["dadu"] + $property["nilaiPoint"];
  97.                 // echo "Point dadu di pindah";
  98.  
  99.  
  100.                 /*Cek Pindah dadu*/
  101.                 if (($key1 = array_search("1", $game["Pemain"][$key]["dadu"])) !== false) {
  102.                     // array_push($game["Pemain"][$key]["dadu"],"1");
  103.                     unset($game["Pemain"][$key]["dadu"][$key1]);
  104.                 }
  105.             }
  106.  
  107.             /*Cek Buang life*/
  108.             $jumlahDadu = count($game["Pemain"][$key]["dadu"]);
  109.             if ($jumlahDadu <= 0) {
  110.                 $game["Pemain"][$key]["life"] = 0;
  111.             }
  112.         }
  113.  
  114.         if ($game["Pemain"][$key]["life"] === 0) {
  115.             //kurangi jumlah pemain
  116.             $color = 'red';
  117.         } else {
  118.             $color = 'black';
  119.         }
  120.  
  121.         if (empty($game["Pemain"][$key]["dadu"])) {
  122.             $labelDadu = '_ (Berhenti bermain karena tidak memiliki dadu)';
  123.         } else {
  124.             $labelDadu = implode(" ", $game["Pemain"][$key]["dadu"]);
  125.         }
  126.  
  127.         echo "<ul>";
  128.         echo "<li style='color:" . $color . "'>Pemain " . $key . " (" . $game["Pemain"][$key]["point"] . ") : " . $labelDadu . "</li>";
  129.         echo "</ul>";
  130.     }
  131.     echo "==================<br>";
  132.  
  133.     // print_r($game["Pemain"]);
  134.     // exit();
  135.     $pemain = $property["m"];
  136.     foreach ($game["Pemain"] as $key => $value) {
  137.         // var_dump($key);
  138.         $jmlDaduKu = count($game["Pemain"][$key]["dadu"]); // == 0 ? $property["m"] : count($game["Pemain"][$key]["dadu"]);
  139.         // echo "jmlDaduKu" . $jmlDaduKu . "<br>";
  140.         // exit();
  141.  
  142.         for ($sufelKey = 1; $sufelKey <= $jmlDaduKu; $sufelKey++) {
  143.             $jmlDaduke = 1;
  144.             do {
  145.                 // echo "The number is: $jmlDaduke <br>";
  146.                 $suhefl[$jmlDaduke] = $property["dadu"][array_rand($property["dadu"], 1)];
  147.                 $jmlDaduke++;
  148.             } while ($jmlDaduke <= $jmlDaduKu);
  149.             //   print_r($suhefl);
  150.             //   exit();
  151.  
  152.             // echo "The number is: $x <br>";
  153.             $game["Pemain"][$key]["dadu"] = $suhefl;
  154.             // $game["Pemain"][$key]["life"] = 1;
  155.             // $game["Pemain"][$key]["point"] = 0;
  156.         }
  157.  
  158.         //hitung jumlah pemain
  159.         if ($game["Pemain"][$key]["life"] == 0) {
  160.             $pemain--;
  161.         }
  162.     }
  163.     // exit();
  164.     $jumlah_pemain = $pemain;
  165.     // $jumlah_pemain=$property["n"];
  166.     // echo "jumlah pemain".$jumlah_pemain;
  167.  
  168.     $property["round"]++;
  169.     // $property["n"] = count($game["Pemain"][$key]["pemain"]);
  170.     // $property["m"] = count($game["Pemain"][$key]["dadu"]);
  171.     // $property["nilaiPoint"] = $nilaiPoint;
  172.     $gameR = $game;
  173.     if ($jumlah_pemain > 1) {
  174.         PlayGame($gameR, $property);
  175.         // exit();
  176.     }
  177. }
  178.  
  179. ?>
  180.  
File Description
  • samu
  • PHP Code
  • 22 Oct-2020
  • 5.28 Kb
You can Share it: