-3

我正在运行一个基于文本的游戏,并试图让一个函数使用 while 循环重复自身,但是当满足条件时它不会停止。

<?php
/****************************************************************/
/*********************** Created By Seker ***********************/
/**** Feel free to modify in any way, but keep this section. ****/
/****************************************************************/
session_start();
require "global_func.php";
if ($_SESSION['loggedin'] == 0)
{
header("Location: login.php");
exit;
}
$userid = $_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is =
    mysql_query(
            "SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",
            $c) or die(mysql_error());
$ir = mysql_fetch_array($is);
check_level();
$fm = money_formatter($ir['money']);
$cm = money_formatter($ir['crystals'], '');
$lv = date('F j, Y, g:i a', $ir['laston']);
$h->userdata($ir, $lv, $fm, $cm);
$h->menuarea();

switch ($_GET['action'])
{
case 'walk':
walk();
break;

case 'walk2':
 walk();
 break;

default:
home();
break;
}

function home()
{


global $ir,$h,$c,$userid;

$q=mysql_query("SELECT u.*,c.* FROM users u LEFT JOIN cities c ON u.location=c.cityid WHERE u.userid=$userid");
$r=mysql_fetch_array($q);

$city = $r['cityname'];
$trav = $ir['steps'];

echo "
    <table width='70%' border='1'>
        <tr>
            <td align='center'>
                <font size='3'><b><u>The $city Streets</u></b></font>
            </td>
        </tr>
        <tr>
            <td align='center'>
                Take your chances with a walk through the $city streets!
            </td>
        </tr>
        <tr>
            <td align='center'>
                Each day, you get <b>10</b> steps! <b>25</b> if you are a donator!
            </td>
        </tr>
        <tr>
            <td align='center'>
                While traveling, you can find crystals, cash, or even gain experience!
            </td>
        </tr>
        <tr>
            <td align='center'>
                But, be careful! There are dangers in the streets!
            </td>
        </tr>
    </table>
";

echo "
    <table width='70%' border='1'>
        <tr>
            <td align='center' colspan='2'>
                <font size='3'><b><u>What would you like to do?</u></b></font>
            </td>
        </tr>
        <tr>
            <td align='center' colspan='2'>
                You can travel <b>{$trav}</b> more times today!
            </td>
        </tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'>[Go For A Walk]</a>
            </td>
            <td align='center' width='50%'>
                <a href='index.php'>[Change Your Mind]</a>
            </td>
        </tr>
    </table>
";
}



function walk()
{
do
{


global $ir,$h,$c,$userid;

$q=mysql_query("SELECT u.*,c.* FROM users u LEFT JOIN cities c ON u.location=c.cityid WHERE u.userid=$userid");
$r=mysql_fetch_array($q);

$chance = mt_rand(1,100);
$city = $r['cityname'];
$trav = $ir['steps'];

if ($ir['steps'] <= 0)
{
    echo "
        You have traveled enough for today! Come back tomorrow!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}

if ($ir['hospital'])
{
    echo "
        You cannot travel while in the hospital!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}

if ($ir['jail'])
{
    echo "
        You cannot travel while in jail!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}


echo "
    <table width=70% border='1'>
        <tr>
            <td align='center' width='50%'>
                <font size='2'>You start your journey through the $city streets!</font>
            </td>
            <td align='center' width='50%'>
                <font size='2'>You can travel up to <b>{$trav}</b> more times today!</font>
            </td>
        </tr>
        <tr>";

if ($chance <= 10)
{
    $rewardtok = mt_rand(5,15);

    mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}

if (($chance >= 15) && ($chance <= 30))
{
    $rewardg = mt_rand(100,5000);
    $reward = money_formatter($rewardg);

    mysql_query("UPDATE users SET money=money+$rewardg,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$reward</b> while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}


if (($chance >= 40) && ($chance <= 50))
{
    $rewardexp = mt_rand(5,50) * ($ir['level'] /2);

    mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}

if (($chance >= 55) && ($chance <= 65))
{
    $rewardexp = mt_rand(5,50);

    mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
        }

 if (($chance >= 75) && ($chance <= 85))
    {
     $rewardtok = mt_rand(5,15);

    mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
 }
if ((($chance > 10) && ($chance < 15)) || (($chance > 30) && ($chance < 40)) || (($chance > 50) && ($chance < 55)) || (($chance > 65) && ($chance < 75)) || ($chance > 85))
{
     $rewardtok = mt_rand(5,15);
      mysql_query("UPDATE users SET steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    You found nothing while walking through the streets
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";


  }

 }
while ($ir['steps'] > 0);
}
$h->endpage();

该函数应该运行直到 step 值为 0,当函数运行一次时,它会从总数中减去。但是当它达到 0 时它并没有停止,我得到这样的东西“步骤:-15535”

我相信这是“重生代码”

function walk()
{
do
{


global $ir,$h,$c,$userid;

$q=mysql_query("SELECT u.*,c.* FROM users u LEFT JOIN cities c ON u.location=c.cityid WHERE u.userid=$userid");
$r=mysql_fetch_array($q);

$chance = mt_rand(1,100);
$city = $r['cityname'];
$trav = $ir['steps'];

if ($ir['steps'] <= 0)
{
echo "
    You have traveled enough for today! Come back tomorrow!
    <a href='index.php'>Home</a>";
    exit($h->endpage());
}

if ($ir['hospital'])
{
echo "
    You cannot travel while in the hospital!
    <a href='index.php'>Home</a>";
    exit($h->endpage());
}

if ($ir['jail'])
{
echo "
    You cannot travel while in jail!
    <a href='index.php'>Home</a>";
    exit($h->endpage());
}


echo "
<table width=70% border='1'>
    <tr>
        <td align='center' width='50%'>
            <font size='2'>You start your journey through the $city streets!</font>
        </td>
        <td align='center' width='50%'>
            <font size='2'>You can travel up to <b>{$trav}</b> more times today!</font>
        </td>
    </tr>
    <tr>";

if ($chance <= 10)
{
$rewardtok = mt_rand(5,15);

mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";
}

if (($chance >= 15) && ($chance <= 30))
{
$rewardg = mt_rand(100,5000);
$reward = money_formatter($rewardg);

mysql_query("UPDATE users SET money=money+$rewardg,steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                Congratulations! You found <b>$reward</b> while walking through the streets!
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";
}


if (($chance >= 40) && ($chance <= 50))
{
$rewardexp = mt_rand(5,50) * ($ir['level'] /2);

mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";
}

if (($chance >= 55) && ($chance <= 65))
{
$rewardexp = mt_rand(5,50);

mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";
    }

 if (($chance >= 75) && ($chance <= 85))
{
 $rewardtok = mt_rand(5,15);

mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";
 }
if ((($chance > 10) && ($chance < 15)) || (($chance > 30) && ($chance < 40)) || (($chance > 50) && ($chance < 55)) || (($chance > 65) && ($chance < 75)) || ($chance > 85))
{
 $rewardtok = mt_rand(5,15);
  mysql_query("UPDATE users SET steps=steps-1 WHERE userid=$userid");

echo "
            <td align='center' colspan='2'>
                You found nothing while walking through the streets
            </td>
        </tr>
        <tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
            </td>
            <td align='center' width='50%'>
                <a href='streets.php'><b>[Back]</b></a>
            </td>
        </tr>
    </table>";


  }

 }
while ($ir['steps'] > 0);
}
$h->endpage();
4

1 回答 1

2
$ir['steps']

在任何地方都没有改变/减少。

在循环结束之前递减它:

$ir['steps'] -= 1;

就在循环的右括号之前。

于 2013-04-22T20:44:13.220 回答