Random dice game












0














I was just wondering if someone could check my small "random dice game", which I programmed due to a "do it yourself" from my PHP book. I am happy that it is working and finished the DIY, but however, I want to know if the solution is okay or if there is a way better/easier way to do it.



Also please let me know if the way of adding comments is fine, if it is too much or not enough.



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Random Dice Game</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<table class="table">
<thead><h3>Lets go!</h3></thead>
<tbody>
<?php
// Defining variables
$sum1 = 0;
$sum2 = 0;

// while loop running until $sum1 or $sum2 >= 21
while($sum1 < 21 && $sum2 < 21) {
// random dice, safing result into variables
$dice1 = random_int(1, 6);
$dice2 = random_int(1, 6);
// $sum1 is increased by $dice1
$sum1 += $dice1;
// checking if game is over (winner player 1)
if($sum1 >= 21) {
echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Das Spiel ist beendet.</td></tr>";
break;
}
// $sum2 is increased by $dice2
$sum2 += $dice2;
// checking if game is over (winner player 2)
if($sum2 >= 21) {
echo "<tr><td>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br>Das Spiel ist beendet.</td></tr>";
break;
}
// printing results of both players, if no one won yet
echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br></td></tr>";
}
// if game is over, checking who won, printing win-message
if($sum1 >= 21 || $sum2 >= 21) {
if($sum1 >= 21) {
echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 1</b>!<br></td></tr></tbody></table>";
} else {
echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 2</b>!<br></td></tr></tbody></table>";
}
}
?>
</body>
</html>









share|improve this question















migrated from stackoverflow.com 3 hours ago


This question came from our site for professional and enthusiast programmers.




















    0














    I was just wondering if someone could check my small "random dice game", which I programmed due to a "do it yourself" from my PHP book. I am happy that it is working and finished the DIY, but however, I want to know if the solution is okay or if there is a way better/easier way to do it.



    Also please let me know if the way of adding comments is fine, if it is too much or not enough.



    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Random Dice Game</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    </head>
    <body>
    <table class="table">
    <thead><h3>Lets go!</h3></thead>
    <tbody>
    <?php
    // Defining variables
    $sum1 = 0;
    $sum2 = 0;

    // while loop running until $sum1 or $sum2 >= 21
    while($sum1 < 21 && $sum2 < 21) {
    // random dice, safing result into variables
    $dice1 = random_int(1, 6);
    $dice2 = random_int(1, 6);
    // $sum1 is increased by $dice1
    $sum1 += $dice1;
    // checking if game is over (winner player 1)
    if($sum1 >= 21) {
    echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Das Spiel ist beendet.</td></tr>";
    break;
    }
    // $sum2 is increased by $dice2
    $sum2 += $dice2;
    // checking if game is over (winner player 2)
    if($sum2 >= 21) {
    echo "<tr><td>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br>Das Spiel ist beendet.</td></tr>";
    break;
    }
    // printing results of both players, if no one won yet
    echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br></td></tr>";
    }
    // if game is over, checking who won, printing win-message
    if($sum1 >= 21 || $sum2 >= 21) {
    if($sum1 >= 21) {
    echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 1</b>!<br></td></tr></tbody></table>";
    } else {
    echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 2</b>!<br></td></tr></tbody></table>";
    }
    }
    ?>
    </body>
    </html>









    share|improve this question















    migrated from stackoverflow.com 3 hours ago


    This question came from our site for professional and enthusiast programmers.


















      0












      0








      0







      I was just wondering if someone could check my small "random dice game", which I programmed due to a "do it yourself" from my PHP book. I am happy that it is working and finished the DIY, but however, I want to know if the solution is okay or if there is a way better/easier way to do it.



      Also please let me know if the way of adding comments is fine, if it is too much or not enough.



      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>Random Dice Game</title>
      <link rel="stylesheet" href="css/bootstrap.min.css">
      </head>
      <body>
      <table class="table">
      <thead><h3>Lets go!</h3></thead>
      <tbody>
      <?php
      // Defining variables
      $sum1 = 0;
      $sum2 = 0;

      // while loop running until $sum1 or $sum2 >= 21
      while($sum1 < 21 && $sum2 < 21) {
      // random dice, safing result into variables
      $dice1 = random_int(1, 6);
      $dice2 = random_int(1, 6);
      // $sum1 is increased by $dice1
      $sum1 += $dice1;
      // checking if game is over (winner player 1)
      if($sum1 >= 21) {
      echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Das Spiel ist beendet.</td></tr>";
      break;
      }
      // $sum2 is increased by $dice2
      $sum2 += $dice2;
      // checking if game is over (winner player 2)
      if($sum2 >= 21) {
      echo "<tr><td>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br>Das Spiel ist beendet.</td></tr>";
      break;
      }
      // printing results of both players, if no one won yet
      echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br></td></tr>";
      }
      // if game is over, checking who won, printing win-message
      if($sum1 >= 21 || $sum2 >= 21) {
      if($sum1 >= 21) {
      echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 1</b>!<br></td></tr></tbody></table>";
      } else {
      echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 2</b>!<br></td></tr></tbody></table>";
      }
      }
      ?>
      </body>
      </html>









      share|improve this question















      I was just wondering if someone could check my small "random dice game", which I programmed due to a "do it yourself" from my PHP book. I am happy that it is working and finished the DIY, but however, I want to know if the solution is okay or if there is a way better/easier way to do it.



      Also please let me know if the way of adding comments is fine, if it is too much or not enough.



      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>Random Dice Game</title>
      <link rel="stylesheet" href="css/bootstrap.min.css">
      </head>
      <body>
      <table class="table">
      <thead><h3>Lets go!</h3></thead>
      <tbody>
      <?php
      // Defining variables
      $sum1 = 0;
      $sum2 = 0;

      // while loop running until $sum1 or $sum2 >= 21
      while($sum1 < 21 && $sum2 < 21) {
      // random dice, safing result into variables
      $dice1 = random_int(1, 6);
      $dice2 = random_int(1, 6);
      // $sum1 is increased by $dice1
      $sum1 += $dice1;
      // checking if game is over (winner player 1)
      if($sum1 >= 21) {
      echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Das Spiel ist beendet.</td></tr>";
      break;
      }
      // $sum2 is increased by $dice2
      $sum2 += $dice2;
      // checking if game is over (winner player 2)
      if($sum2 >= 21) {
      echo "<tr><td>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br>Das Spiel ist beendet.</td></tr>";
      break;
      }
      // printing results of both players, if no one won yet
      echo "<tr><td>Spieler 1 hat $dice1 gewürfelt. Summe: <b>$sum1</b><br>Spieler 2 hat $dice2 gewürfelt. Summe: <b>$sum2</b><br></td></tr>";
      }
      // if game is over, checking who won, printing win-message
      if($sum1 >= 21 || $sum2 >= 21) {
      if($sum1 >= 21) {
      echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 1</b>!<br></td></tr></tbody></table>";
      } else {
      echo "<tr><td>Wir haben einen Sieger! Glückwunsch, <b>Spieler 2</b>!<br></td></tr></tbody></table>";
      }
      }
      ?>
      </body>
      </html>






      beginner php






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      mdfst13

      17.4k52156




      17.4k52156










      asked 4 hours ago









      Ron

      1




      1




      migrated from stackoverflow.com 3 hours ago


      This question came from our site for professional and enthusiast programmers.






      migrated from stackoverflow.com 3 hours ago


      This question came from our site for professional and enthusiast programmers.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Some comments about the generated HTML:





          • <h3>: this element cannot be a child element of thead. Only <tr> elements can be children of <thead>. As a result the browser will in fact place the <h3> element outside of the table.


          • <br>: this element makes not a lot of sense just before a </td>. It it was to create extra vertical white-space, then it is better to use CSS styling on your <td> elements


          • <table>: as your table contains one column only, and the contents are in fact phrases, it is it a bit odd to use a <table> for that. It would make more sense to use <p> or <div> elements.

          • You include bootstrap CSS, which is OK, but for the little content you currently have it is probably overkill. It currently takes care of putting a horizontal border between <td> elements, but this you can manage with your own styles (if needed) on the <p> or <div> tags.


          Comments about the PHP code:





          • if($sum1 >= 21 || $sum2 >= 21): this condition will always be true since the while would have continued if this were not the case. You should just omit the if.

          • As the logic is the same for the two players it would be better not to have code repetition, but "toggle" the player between player 1 and 2.

          • It would be even better to put the die-rolling logic in a Player class: one instance per player

          • Separate the HTML output generation from the logic. It is better to keep a log of the game in variables and produce the output from that right at the end of your code.


          Here is how it could look:



          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8">
          <title>Random Dice Game</title>
          <link rel="stylesheet" href="css/bootstrap.min.css">
          <style>
          div.player0 {
          padding-top: 1rem;
          padding-left: 1rem;
          border-top: 1px solid #dee2e6;
          }
          div.player1 {
          padding-bottom: 1rem;
          padding-left: 1rem;
          }
          div.end {
          padding-bottom: 1rem;
          padding-left: 1rem;
          }
          </style>
          </head>

          <body>
          <h3>Lets go!</h3>
          <?php
          class Player {
          public $sum = 0;
          public $name;
          public $iswinner = false;

          public function __construct($name) {
          $this->name = $name;
          }
          public function rolldie() {
          // Throw die randomly, saving result into variable
          $die = random_int(1, 6);
          // $sum is increased by $die
          $this->sum += $die;
          // Check whether game is over
          $this->iswinner = $this->sum >= 21;
          }
          }
          // Defining variables
          $players = [new Player("Spieler 1"), new Player("Spieler 2")];
          $log = ;
          $playerid = 1;

          // Loop until a player wins
          while (!$players[$playerid]->iswinner) {
          // Switch player (toggle between 0 and 1)
          $playerid = 1 - $playerid;
          $player = $players[$playerid];
          // Roll the die
          $die = $player->rolldie();
          // Log the result
          $log = [$playerid, $player->name, $die, $player->sum];
          }
          $winner = $player->name;

          // Produce the HTML output
          foreach($log as list($playerid, $name, $die, $sum)) {
          ?>
          <div class="player<?=$playerid?>">$name hat <?=$die?> gewürfelt. Summe: <b><?=$sum?></b></div>
          <?php
          }
          ?>
          <div class="end">Das Spiel ist beendet.</div>
          <div class="winner">Wir haben einen Sieger! Glückwunsch, <b><?=$winner?></b>!</div>
          </body>
          </html>





          share|improve this answer























            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210552%2frandom-dice-game%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Some comments about the generated HTML:





            • <h3>: this element cannot be a child element of thead. Only <tr> elements can be children of <thead>. As a result the browser will in fact place the <h3> element outside of the table.


            • <br>: this element makes not a lot of sense just before a </td>. It it was to create extra vertical white-space, then it is better to use CSS styling on your <td> elements


            • <table>: as your table contains one column only, and the contents are in fact phrases, it is it a bit odd to use a <table> for that. It would make more sense to use <p> or <div> elements.

            • You include bootstrap CSS, which is OK, but for the little content you currently have it is probably overkill. It currently takes care of putting a horizontal border between <td> elements, but this you can manage with your own styles (if needed) on the <p> or <div> tags.


            Comments about the PHP code:





            • if($sum1 >= 21 || $sum2 >= 21): this condition will always be true since the while would have continued if this were not the case. You should just omit the if.

            • As the logic is the same for the two players it would be better not to have code repetition, but "toggle" the player between player 1 and 2.

            • It would be even better to put the die-rolling logic in a Player class: one instance per player

            • Separate the HTML output generation from the logic. It is better to keep a log of the game in variables and produce the output from that right at the end of your code.


            Here is how it could look:



            <!DOCTYPE html>
            <html>
            <head>
            <meta charset="utf-8">
            <title>Random Dice Game</title>
            <link rel="stylesheet" href="css/bootstrap.min.css">
            <style>
            div.player0 {
            padding-top: 1rem;
            padding-left: 1rem;
            border-top: 1px solid #dee2e6;
            }
            div.player1 {
            padding-bottom: 1rem;
            padding-left: 1rem;
            }
            div.end {
            padding-bottom: 1rem;
            padding-left: 1rem;
            }
            </style>
            </head>

            <body>
            <h3>Lets go!</h3>
            <?php
            class Player {
            public $sum = 0;
            public $name;
            public $iswinner = false;

            public function __construct($name) {
            $this->name = $name;
            }
            public function rolldie() {
            // Throw die randomly, saving result into variable
            $die = random_int(1, 6);
            // $sum is increased by $die
            $this->sum += $die;
            // Check whether game is over
            $this->iswinner = $this->sum >= 21;
            }
            }
            // Defining variables
            $players = [new Player("Spieler 1"), new Player("Spieler 2")];
            $log = ;
            $playerid = 1;

            // Loop until a player wins
            while (!$players[$playerid]->iswinner) {
            // Switch player (toggle between 0 and 1)
            $playerid = 1 - $playerid;
            $player = $players[$playerid];
            // Roll the die
            $die = $player->rolldie();
            // Log the result
            $log = [$playerid, $player->name, $die, $player->sum];
            }
            $winner = $player->name;

            // Produce the HTML output
            foreach($log as list($playerid, $name, $die, $sum)) {
            ?>
            <div class="player<?=$playerid?>">$name hat <?=$die?> gewürfelt. Summe: <b><?=$sum?></b></div>
            <?php
            }
            ?>
            <div class="end">Das Spiel ist beendet.</div>
            <div class="winner">Wir haben einen Sieger! Glückwunsch, <b><?=$winner?></b>!</div>
            </body>
            </html>





            share|improve this answer




























              0














              Some comments about the generated HTML:





              • <h3>: this element cannot be a child element of thead. Only <tr> elements can be children of <thead>. As a result the browser will in fact place the <h3> element outside of the table.


              • <br>: this element makes not a lot of sense just before a </td>. It it was to create extra vertical white-space, then it is better to use CSS styling on your <td> elements


              • <table>: as your table contains one column only, and the contents are in fact phrases, it is it a bit odd to use a <table> for that. It would make more sense to use <p> or <div> elements.

              • You include bootstrap CSS, which is OK, but for the little content you currently have it is probably overkill. It currently takes care of putting a horizontal border between <td> elements, but this you can manage with your own styles (if needed) on the <p> or <div> tags.


              Comments about the PHP code:





              • if($sum1 >= 21 || $sum2 >= 21): this condition will always be true since the while would have continued if this were not the case. You should just omit the if.

              • As the logic is the same for the two players it would be better not to have code repetition, but "toggle" the player between player 1 and 2.

              • It would be even better to put the die-rolling logic in a Player class: one instance per player

              • Separate the HTML output generation from the logic. It is better to keep a log of the game in variables and produce the output from that right at the end of your code.


              Here is how it could look:



              <!DOCTYPE html>
              <html>
              <head>
              <meta charset="utf-8">
              <title>Random Dice Game</title>
              <link rel="stylesheet" href="css/bootstrap.min.css">
              <style>
              div.player0 {
              padding-top: 1rem;
              padding-left: 1rem;
              border-top: 1px solid #dee2e6;
              }
              div.player1 {
              padding-bottom: 1rem;
              padding-left: 1rem;
              }
              div.end {
              padding-bottom: 1rem;
              padding-left: 1rem;
              }
              </style>
              </head>

              <body>
              <h3>Lets go!</h3>
              <?php
              class Player {
              public $sum = 0;
              public $name;
              public $iswinner = false;

              public function __construct($name) {
              $this->name = $name;
              }
              public function rolldie() {
              // Throw die randomly, saving result into variable
              $die = random_int(1, 6);
              // $sum is increased by $die
              $this->sum += $die;
              // Check whether game is over
              $this->iswinner = $this->sum >= 21;
              }
              }
              // Defining variables
              $players = [new Player("Spieler 1"), new Player("Spieler 2")];
              $log = ;
              $playerid = 1;

              // Loop until a player wins
              while (!$players[$playerid]->iswinner) {
              // Switch player (toggle between 0 and 1)
              $playerid = 1 - $playerid;
              $player = $players[$playerid];
              // Roll the die
              $die = $player->rolldie();
              // Log the result
              $log = [$playerid, $player->name, $die, $player->sum];
              }
              $winner = $player->name;

              // Produce the HTML output
              foreach($log as list($playerid, $name, $die, $sum)) {
              ?>
              <div class="player<?=$playerid?>">$name hat <?=$die?> gewürfelt. Summe: <b><?=$sum?></b></div>
              <?php
              }
              ?>
              <div class="end">Das Spiel ist beendet.</div>
              <div class="winner">Wir haben einen Sieger! Glückwunsch, <b><?=$winner?></b>!</div>
              </body>
              </html>





              share|improve this answer


























                0












                0








                0






                Some comments about the generated HTML:





                • <h3>: this element cannot be a child element of thead. Only <tr> elements can be children of <thead>. As a result the browser will in fact place the <h3> element outside of the table.


                • <br>: this element makes not a lot of sense just before a </td>. It it was to create extra vertical white-space, then it is better to use CSS styling on your <td> elements


                • <table>: as your table contains one column only, and the contents are in fact phrases, it is it a bit odd to use a <table> for that. It would make more sense to use <p> or <div> elements.

                • You include bootstrap CSS, which is OK, but for the little content you currently have it is probably overkill. It currently takes care of putting a horizontal border between <td> elements, but this you can manage with your own styles (if needed) on the <p> or <div> tags.


                Comments about the PHP code:





                • if($sum1 >= 21 || $sum2 >= 21): this condition will always be true since the while would have continued if this were not the case. You should just omit the if.

                • As the logic is the same for the two players it would be better not to have code repetition, but "toggle" the player between player 1 and 2.

                • It would be even better to put the die-rolling logic in a Player class: one instance per player

                • Separate the HTML output generation from the logic. It is better to keep a log of the game in variables and produce the output from that right at the end of your code.


                Here is how it could look:



                <!DOCTYPE html>
                <html>
                <head>
                <meta charset="utf-8">
                <title>Random Dice Game</title>
                <link rel="stylesheet" href="css/bootstrap.min.css">
                <style>
                div.player0 {
                padding-top: 1rem;
                padding-left: 1rem;
                border-top: 1px solid #dee2e6;
                }
                div.player1 {
                padding-bottom: 1rem;
                padding-left: 1rem;
                }
                div.end {
                padding-bottom: 1rem;
                padding-left: 1rem;
                }
                </style>
                </head>

                <body>
                <h3>Lets go!</h3>
                <?php
                class Player {
                public $sum = 0;
                public $name;
                public $iswinner = false;

                public function __construct($name) {
                $this->name = $name;
                }
                public function rolldie() {
                // Throw die randomly, saving result into variable
                $die = random_int(1, 6);
                // $sum is increased by $die
                $this->sum += $die;
                // Check whether game is over
                $this->iswinner = $this->sum >= 21;
                }
                }
                // Defining variables
                $players = [new Player("Spieler 1"), new Player("Spieler 2")];
                $log = ;
                $playerid = 1;

                // Loop until a player wins
                while (!$players[$playerid]->iswinner) {
                // Switch player (toggle between 0 and 1)
                $playerid = 1 - $playerid;
                $player = $players[$playerid];
                // Roll the die
                $die = $player->rolldie();
                // Log the result
                $log = [$playerid, $player->name, $die, $player->sum];
                }
                $winner = $player->name;

                // Produce the HTML output
                foreach($log as list($playerid, $name, $die, $sum)) {
                ?>
                <div class="player<?=$playerid?>">$name hat <?=$die?> gewürfelt. Summe: <b><?=$sum?></b></div>
                <?php
                }
                ?>
                <div class="end">Das Spiel ist beendet.</div>
                <div class="winner">Wir haben einen Sieger! Glückwunsch, <b><?=$winner?></b>!</div>
                </body>
                </html>





                share|improve this answer














                Some comments about the generated HTML:





                • <h3>: this element cannot be a child element of thead. Only <tr> elements can be children of <thead>. As a result the browser will in fact place the <h3> element outside of the table.


                • <br>: this element makes not a lot of sense just before a </td>. It it was to create extra vertical white-space, then it is better to use CSS styling on your <td> elements


                • <table>: as your table contains one column only, and the contents are in fact phrases, it is it a bit odd to use a <table> for that. It would make more sense to use <p> or <div> elements.

                • You include bootstrap CSS, which is OK, but for the little content you currently have it is probably overkill. It currently takes care of putting a horizontal border between <td> elements, but this you can manage with your own styles (if needed) on the <p> or <div> tags.


                Comments about the PHP code:





                • if($sum1 >= 21 || $sum2 >= 21): this condition will always be true since the while would have continued if this were not the case. You should just omit the if.

                • As the logic is the same for the two players it would be better not to have code repetition, but "toggle" the player between player 1 and 2.

                • It would be even better to put the die-rolling logic in a Player class: one instance per player

                • Separate the HTML output generation from the logic. It is better to keep a log of the game in variables and produce the output from that right at the end of your code.


                Here is how it could look:



                <!DOCTYPE html>
                <html>
                <head>
                <meta charset="utf-8">
                <title>Random Dice Game</title>
                <link rel="stylesheet" href="css/bootstrap.min.css">
                <style>
                div.player0 {
                padding-top: 1rem;
                padding-left: 1rem;
                border-top: 1px solid #dee2e6;
                }
                div.player1 {
                padding-bottom: 1rem;
                padding-left: 1rem;
                }
                div.end {
                padding-bottom: 1rem;
                padding-left: 1rem;
                }
                </style>
                </head>

                <body>
                <h3>Lets go!</h3>
                <?php
                class Player {
                public $sum = 0;
                public $name;
                public $iswinner = false;

                public function __construct($name) {
                $this->name = $name;
                }
                public function rolldie() {
                // Throw die randomly, saving result into variable
                $die = random_int(1, 6);
                // $sum is increased by $die
                $this->sum += $die;
                // Check whether game is over
                $this->iswinner = $this->sum >= 21;
                }
                }
                // Defining variables
                $players = [new Player("Spieler 1"), new Player("Spieler 2")];
                $log = ;
                $playerid = 1;

                // Loop until a player wins
                while (!$players[$playerid]->iswinner) {
                // Switch player (toggle between 0 and 1)
                $playerid = 1 - $playerid;
                $player = $players[$playerid];
                // Roll the die
                $die = $player->rolldie();
                // Log the result
                $log = [$playerid, $player->name, $die, $player->sum];
                }
                $winner = $player->name;

                // Produce the HTML output
                foreach($log as list($playerid, $name, $die, $sum)) {
                ?>
                <div class="player<?=$playerid?>">$name hat <?=$die?> gewürfelt. Summe: <b><?=$sum?></b></div>
                <?php
                }
                ?>
                <div class="end">Das Spiel ist beendet.</div>
                <div class="winner">Wir haben einen Sieger! Glückwunsch, <b><?=$winner?></b>!</div>
                </body>
                </html>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 1 hour ago









                trincot

                32627




                32627






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210552%2frandom-dice-game%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre