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.
Result of php executing
Full code of samu.php
- <?php
- $m = 4; //dadu
- $n = 3; //pemain
- $point = 6;
- $switch = 1;
- $dadu = array("1", "2", "3", "4", "5", "6");
- $nilaiPoint = 1;
- // var_dump($lemparanDadu);
- // $_GET["debug"] ? print_r($game) : '';
- $game = null;
- $property["dadu"] = $dadu;
- $property["round"] = 1;
- $property["n"] = $n;
- $property["m"] = $m;
- $property["nilaiPoint"] = $nilaiPoint;
- echo "Pemain = " . $property["n"] . ", Dadu = " . $property["m"];
- echo "<br>==================<br>";
- if (is_null($game)) {
- $jmlDaduKu = $property["m"];
- // echo "jmlDaduKu" . $jmlDaduKu . "<br>";
- for ($x = 1; $x <= $jmlDaduKu; $x++) {
- $jmlDaduke = 1;
- do {
- // echo "The number is: $jmlDaduke <br>";
- $suhefl[$jmlDaduke] = $property["dadu"][array_rand($property["dadu"], 1)];
- $jmlDaduke++;
- } while ($jmlDaduke <= $property["m"]);
- // print_r($suhefl);
- // exit();
- // echo "The number is: $x <br>";
- $game["Pemain"]["#" . $x]["dadu"] = $suhefl;
- $game["Pemain"]["#" . $x]["life"] = 1;
- $game["Pemain"]["#" . $x]["point"] = 0;
- }
- }
- PlayGame($game, $property);
- function PlayGame($game, $property)
- {
- // var_dump($game);
- // var_dump($property);
- // exit();
- // var_dump($game);
- // exit();
- echo "Giliran <b>" . $property["round"] . "</b> lempar dadu:<br>";
- foreach ($game["Pemain"] as $key => $value) {
- // var_dump($key);
- /*Cek Point*/
- echo "<ul>";
- echo "<li> Pemain " . $key . " (" . $game["Pemain"][$key]["point"] . ") : " . implode(" ", $game["Pemain"][$key]["dadu"]) . "</li>";
- echo "</ul>";
- }
- // print_r($game["Pemain"]);
- echo "<br>Setelah evaluasi:<br>";
- foreach ($game["Pemain"] as $key => $value) {
- // var_dump($key);
- /*Cek Point*/
- // var_dump($game["Pemain"][$key]["dadu"]);
- foreach ($game["Pemain"][$key]["dadu"] as $kDadu => $vDadu) {
- // $game["Pemain"][$key]["point"] = cekPoint($vDadu,$game["Pemain"][$key]["point"],$nilaiPoint);
- if ("6" === $vDadu) {
- // echo "Point bertambah";
- $game["Pemain"][$key]["point"] = $game["Pemain"][$key]["point"] + $property["nilaiPoint"];
- // echo "Point dadu di buang";
- /*Cek Buang dadu*/
- if (($key6 = array_search("6", $game["Pemain"][$key]["dadu"])) !== false) {
- unset($game["Pemain"][$key]["dadu"][$key6]);
- }
- }
- //pindah dadu
- if ("1" === $vDadu) {
- // echo "Dadu berpindah";
- // $game["Pemain"][$key]["dadu"] = $game["Pemain"][$key]["dadu"] + $property["nilaiPoint"];
- // echo "Point dadu di pindah";
- /*Cek Pindah dadu*/
- if (($key1 = array_search("1", $game["Pemain"][$key]["dadu"])) !== false) {
- // array_push($game["Pemain"][$key]["dadu"],"1");
- unset($game["Pemain"][$key]["dadu"][$key1]);
- }
- }
- /*Cek Buang life*/
- $jumlahDadu = count($game["Pemain"][$key]["dadu"]);
- if ($jumlahDadu <= 0) {
- $game["Pemain"][$key]["life"] = 0;
- }
- }
- if ($game["Pemain"][$key]["life"] === 0) {
- //kurangi jumlah pemain
- $color = 'red';
- } else {
- $color = 'black';
- }
- if (empty($game["Pemain"][$key]["dadu"])) {
- $labelDadu = '_ (Berhenti bermain karena tidak memiliki dadu)';
- } else {
- $labelDadu = implode(" ", $game["Pemain"][$key]["dadu"]);
- }
- echo "<ul>";
- echo "<li style='color:" . $color . "'>Pemain " . $key . " (" . $game["Pemain"][$key]["point"] . ") : " . $labelDadu . "</li>";
- echo "</ul>";
- }
- echo "==================<br>";
- // print_r($game["Pemain"]);
- // exit();
- $pemain = $property["m"];
- foreach ($game["Pemain"] as $key => $value) {
- // var_dump($key);
- $jmlDaduKu = count($game["Pemain"][$key]["dadu"]); // == 0 ? $property["m"] : count($game["Pemain"][$key]["dadu"]);
- // echo "jmlDaduKu" . $jmlDaduKu . "<br>";
- // exit();
- for ($sufelKey = 1; $sufelKey <= $jmlDaduKu; $sufelKey++) {
- $jmlDaduke = 1;
- do {
- // echo "The number is: $jmlDaduke <br>";
- $suhefl[$jmlDaduke] = $property["dadu"][array_rand($property["dadu"], 1)];
- $jmlDaduke++;
- } while ($jmlDaduke <= $jmlDaduKu);
- // print_r($suhefl);
- // exit();
- // echo "The number is: $x <br>";
- $game["Pemain"][$key]["dadu"] = $suhefl;
- // $game["Pemain"][$key]["life"] = 1;
- // $game["Pemain"][$key]["point"] = 0;
- }
- //hitung jumlah pemain
- if ($game["Pemain"][$key]["life"] == 0) {
- $pemain--;
- }
- }
- // exit();
- $jumlah_pemain = $pemain;
- // $jumlah_pemain=$property["n"];
- // echo "jumlah pemain".$jumlah_pemain;
- $property["round"]++;
- // $property["n"] = count($game["Pemain"][$key]["pemain"]);
- // $property["m"] = count($game["Pemain"][$key]["dadu"]);
- // $property["nilaiPoint"] = $nilaiPoint;
- $gameR = $game;
- if ($jumlah_pemain > 1) {
- PlayGame($gameR, $property);
- // exit();
- }
- }
- ?>
File Description
- samu
- PHP Code
- 22 Oct-2020
- 5.28 Kb
You can Share it:
Latest PHP Pastes