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

Disable CSS concatenation in Symfony2
header(“Location:”); breaking with .htaccess setup
PHP OOP Error - Missing argument 1 for User::__construct()
MySQL is stripping em dash despite database and connection set to UTF8
SQL WordPress order by date
PHP show function results after calling the function
Zend Framework 2:: why inlineScript()->captureStart() is not working in layout.phtml
Is images in HTTP post request base64 encoded?
Error in Php upload image to directory [closed]
How to enable error log in Ubuntu Apache
Adding a non-laravel package to my Laravel project with composer
.htaccess pass request on for /UA/ folder
Laravel 4 - extending blade, problems with command without brackets
how to not create empty sessions for guest visitors
PHP Globally visible login
PHP Fatal error: Call to a member function fetch_assoc() on a non-object

Categories

HOME
mod-rewrite
clearcase
facebook-messenger
histogram
tabs
jersey-2.0
mirc
scheduler
kurento
anchor
cqrs
lapack
erd
grocery-crud
outlook-restapi
texas-instruments
global-variables
async-await
vmware-workstation
web-deployment
google-sites
varnish-vcl
http-authentication
angular2-highcharts
sqlplus
intel
eclipse-emf
quartz.net
ratio
web-config-transform
webviewclient
tortoisehg
react-bootstrap-table
basic-authentication
region
mdns
substance
tilemill
toad
phpstorm-2016.1
instructions
caesar-cipher
ms-media-foundation
serversocket
android-canvas
pdfminer
keyboard-layout
fastlane
sspi
praat
virtual-memory
apache-toree
viewmodel
windows-media-player
iptv
line-intersection
scala-collections
asp.net-web-api-routing
openerp-6
pjax
zynq
maven-release-plugin
django-validation
pack
odesk
python-cffi
android-mapview
ruby-on-rails-4.1
mousehover
sqlbase
mgwt
python-c-extension
createfile
nhibernate-criteria
dos2unix
microsoft-expression-web
symja
cakephp-2.2
rubber
network-printers
two.js
stripe.net
roxy-fileman
mft
mute
indesign-server
acceptance-testing
unicoins
ntlmv2
websphere-6.1
pageheap
quartz-core
sigar
litespeed
haiku
kernel32
directshow.net
grails-validation
channelfactory
android-2.1-eclair
ixmldomdocument
source-code-protection
.net-client-profile
tacit-programming
inversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App