php


PHP While Loop Debugging with Case


i got a bit further into my own little trial and error and wanted to create a while loop that ends if $monster or $du are below "0". I fixed the first problem which was that the loop was permanently in Loop. But now it just ends the moment $du is at or below "0" and not if "monster" is below "0".
What do i have to change?
to make it clear: I want the script to stop if $du is at zero or if $monster is at zero. Only one thing hast to be "true" to stop it. Because wether the hero kills the monster or the monster kills the hero - the fight is over.
right now it does only stop when $du is at zero and still goes on and on even if $monster is below zero when at the same time hero is above zero.
<?php
if(isset($_POST['gesendet'])){
// Variablen deklararien und Werte zuweisen.
$waffe = $_POST['waffe'];
$monster = 100;
$du = 70;
$i = 0;
$ko = 0;
switch($waffe){
case "1":
while ( $du > 0 OR $monster > 0) {
$schaden2 = rand(1,6);
$dolch = rand(1,4) + 4;
$du = ($du - $schaden2);
$monster = ($monster - $dolch);
$i = $i + 1;
echo "Runde: " . $i . "<br>" ;
echo "Du triffst das Monster mit einem Dolch für " . $dolch . " Schaden.<br>" ;
echo "Es hat noch " . $monster . " HP<br>" ;
echo "Das Monster trifft dich für " . $schaden2 . " Schaden.<br>" ;
echo "Du hast noch " . $du . " HP<br>" ;
}
break;
case "2":
while ( $du > 0 OR $monster > 0) {
$schaden2 = rand(1,6);
$schwert = rand(1,6) + 2;
$du = ($du - $schaden2);
$monster = ($monster - $schwert);
$i = $i + 1;
echo "Runde: " . $i . "<br>" ;
echo "Du triffst das Monster mit einem Schwert für " . $schwert . " Schaden.<br>" ;
echo "Es hat noch " . $monster . " HP<br>" ;
echo "Das Monster trifft dich für " . $schaden2 . " Schaden.<br>" ;
echo "Du hast noch " . $du . " HP<br>" ;
}
break;
case "3":
while ( $du > 0 OR $monster > 0) {
$schaden2 = rand(1,6);
$zweihaender = rand(1,6) * 2;
$du = ($du - $schaden2);
$monster = ($monster - $zweihaender);
$i = $i + 1;
echo "Runde: " . $i . "<br>" ;
echo "Du triffst das Monster mit einem Zweihänder für " . $zweihaender . " Schaden.<br>" ;
echo "Es hat noch " . $monster . " HP<br>" ;
echo "Das Monster trifft dich für " . $schaden2 . " Schaden.<br>" ;
echo "Du hast noch " . $du . " HP<br>" ;
}
break;
}
}
?>
Firstly I'd change
while ( $du > 0 OR $monster > 0) {
To
while ( $du > 0 || $monster > 0) {
This is because of operator precedence regarding "or" and "||".
Then look at the logic. You are telling the script to stop when either $du or $monster is below zero. If you want the loop to exit when both are below zero then change the code to.
while ( $du > 0 && $monster > 0) {
Some links:
PHP documentation for operator precedence
Stack Overflow question about precedence
PHP doc if statement
Update
Change your switch statements to this:
switch($waffe) {
case "1":
while ( $du > 0 && $monster > 0) {
$schaden2 = rand(1,6);
$dolch = rand(1,4) + 4;
$du = ($du - $schaden2);
$monster = ($monster - $dolch);
$i = $i + 1;
echo "Runde: " . $i . "<br>" ;
echo "Du triffst das Monster mit einem Dolch für " . $dolch . " Schaden.<br>" ;
echo "Es hat noch " . $monster . " HP<br>" ;
echo "Das Monster trifft dich für " . $schaden2 . " Schaden.<br>" ;
echo "Du hast noch " . $du . " HP<br>" ;
}
break;
Repeat that for all cases in the switch statement (if add them all but then my answer will be long and I'm on my phone).
The logic behind this is that you are getting the script to use this logic:
If $monster and $du are positive then keep running the while loop.
Otherwise someone had died.
So, these are the cases:
If $monster is positive and $du is positive then keep fighting.
If $monster is negative and $du is positive then monster is slain.
If $monster is positive and $du is negative then player is slain.
(This one is unlikely but) If $monster is negative and $du is negative then both have died.
Your previous code meant that if $monster was below zero then the code would carry on running because $du was still greater than zero which meant that the whole loop was true.
Consider this example:
$a = 5;
$b = 2;
if ($a < 4 || $b < 4)
{
echo "This will be true.";
}
In this example $a is not less than 4 but $b is which means that the result is true. The way an or statement works is that one of the two must be true.
With an and statement both must be true. This means that if you did:
$a = 5;
$b = 2;
if ($a < 4 && $b < 4)
{
echo "This will be false and won't run.";
}
The above code won't run because $a is greater than 4. This will cancel the If statement.
Using and for your while loop means that you will get the desired result.

Related Links

error when saving php sessions in project's directory
SoapFault Integrator username in header and body does not match
What does assigning a null value to a variable in a function parameter mean?
Doctrine, Symfony3, Query entities from other entity array
Order by attribute value
Where, best, should I place my add_action and function codes in the function.php file?
MySQL script retrieves Steam ID and nothing else
How to load wordpress child theme css after parent theme css
Why I'm getting this error “SQLSTATE[HY093]: Invalid parameter number: parameter was not defined”?
Does PHP mysqli query and fetch_(row/assoc) use real time data?
PHP function suggestion need
Design pattern for one controller and many services
Wrong php email form
Elequoent relationship is not selecting releated rows
Laravel routes not working on production
Update with wp_update_post in WordPress

Categories

HOME
freeradius
hdfs
websocket
consul
jersey-2.0
qooxdoo
phpmqtt
pyresttest
phpseclib
fireloop
csrf
tiff
cname
synthesis
google-sites
gravity
mod-wsgi
cellular-network
background-color
cube
ninject
audit
python-2.x
ms-media-foundation
cmis
ftp-server
python-sounddevice
spring-restcontroller
yoast
elasticsearch-aggregation
spooler
transparency
linq-to-excel
renderman
visual-format-language
algebra
dblink
nrf51
allegro
researchkit
nclam
conan
dpkg
qtcpsocket
piping
perforce-integrate
wicked-pdf
exists
outlook.com
deeplink
http.client
asp.net-web-api-helppages
domino-designer-eclipse
ipod
sql-processor
comctl32
inputbox
lumia-imaging-sdk
squeezebox
commenting
zend-search-lucene
sql-function
spring-social-linkedin
code39
web-frameworks
word-frequency
uploading
motodev-studio
nsrangeexception
winsxs
nosetests
asp.net-mvc-views
qcar-sdk
hungarian-notation
resharper-5.1
external-sorting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App