PHP: Newbie is searching for improvement, random dice game
good morning!
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.
Thanks in advance and happy new year!
<!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>
php
migrated from stackoverflow.com 2 mins ago
This question came from our site for professional and enthusiast programmers.
add a comment |
good morning!
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.
Thanks in advance and happy new year!
<!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>
php
migrated from stackoverflow.com 2 mins ago
This question came from our site for professional and enthusiast programmers.
add a comment |
good morning!
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.
Thanks in advance and happy new year!
<!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>
php
good morning!
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.
Thanks in advance and happy new year!
<!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>
php
php
asked 47 mins ago
Ron
migrated from stackoverflow.com 2 mins ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 2 mins ago
This question came from our site for professional and enthusiast programmers.
add a comment |
add a comment |
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210552%2fphp-newbie-is-searching-for-improvement-random-dice-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210552%2fphp-newbie-is-searching-for-improvement-random-dice-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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