// EDIT 好的,我打开了错误报告并更改了模拟代码,但结果很糟糕。我收到错误致命错误:超过30秒的最大执行时间或警告:在行中
中除以零 $hit = ($attacker['attack']/$defender['defense']) + rand(1, 2);
下面是完整的代码,我尝试了rand(0,count($ unit_attacker_def)-1);但我认为它没有变化。此外,我添加了选择新的随机数组与单位,如果有0健康和未设置。并且它仍在运行一个单位,而不是所有攻击者阵列,以及所有或左侧单位的后卫阵列。我以某种方式进入了脚本调用下一个单元的阶段,但它没有加载任何变量。
case 'battle_wizard_execute';
?>
<table>
<tr><td><h3>Utok</h3></td></tr>
<?
$query_street = mysql_query("SELECT * FROM game_army_attacks WHERE attack_id = '".$_GET['id']."' ");
$row_street = mysql_fetch_assoc($query_street);
$query_loc_info = mysql_query("SELECT * FROM game_location_street WHERE street_id = '".$row_street['attack_attacker']."' ");
$row_loc_info = mysql_fetch_assoc($query_loc_info);
$tilee_info = mysql_num_rows($query_street);
if ($tilee_info > 0){
$query_units_info = mysql_query("SELECT * FROM game_army_units_attacking WHERE army_attack = '".$_GET['id']."' ");
while (($unitsinfo = mysql_fetch_assoc($query_units_info)) != NULL) {
$query_unit_info = mysql_query("SELECT * FROM game_army_class WHERE army_class_id = '".$unitsinfo['army_class_id']."' ");
$unit = mysql_fetch_assoc($query_unit_info);
$unit_attacker = array();
$unit_attacker[] = array(
'name' => $unit['army_class_name'],
'power' => $unitsinfo['army_power'],
'attack' => $unitsinfo['army_att'],
'defense' => $unitsinfo['army_def']
);
///// Kolko jednotiek máš tolko krat sa vypise
$x = 1;
while($x <= $unitsinfo['army_population']) {
foreach($unit_attacker as $index => $record){
///// Tato cast pusti kod pre kazdu jednu jednotku ktora je v poli
echo "<tr><td>Jednotka: {$record['name']} ID: {$record['power']} ParentID: {$record['attack']} Title: {$record['defense']}</td></tr>";
$x++;
}
}
}
}
?>
</table>
<table>
<tr><td><h3>Utok protivnik</h3></td></tr>
<?
$query_street_def = mysql_query("SELECT * FROM game_army_units WHERE army_street = '".$row_street['attack_defender']."' ");
$tilee_info_def = mysql_num_rows($query_street_def);
if ($tilee_info_def > 0){
$query_units_info_def = mysql_query("SELECT * FROM game_army_units WHERE army_street = '".$row_street['attack_defender']."' ");
while (($unitsinfo_def = mysql_fetch_assoc($query_units_info_def)) != NULL) {
$query_unit_info_def = mysql_query("SELECT * FROM game_army_class WHERE army_class_id = '".$unitsinfo_def['army_class_id']."' ");
$unit_def = mysql_fetch_assoc($query_unit_info_def);
$unit_attacker_def = array();
$unit_attacker_def[] = array(
'name' => $unit_def['army_class_name'],
'power' => $unitsinfo_def['army_power'],
'attack' => $unitsinfo_def['army_att'],
'defense' => $unitsinfo_def['army_def']
);
///// Kolko jednotiek máš tolko krat sa vypise
$y = 1;
while($y <= $unitsinfo_def['army_population']) {
foreach($unit_attacker_def as $index => $record_def){
///// Tato cast pusti kod pre kazdu jednu jednotku ktora je v poli
echo "<tr><td>Jednotka: {$record_def['name']} ID: {$record_def['power']} ParentID: {$record_def['attack']} Title: {$record_def['defense']}</td></tr>";
$y++;
}
}
}
}
模拟:
$count = 0;
while ((count($unit_attacker_def) > 0) && (count($unit_attacker) > 0)){
$count++;
$attacker_key = rand(0,count($unit_attacker_def)-1);
$attacker =& $unit_attacker[$attacker_key];
$defender_key = rand(0,count($unit_defender)-1);
$defender =& $unit_attacker_def[$defender_key];
while (($defender['power'] >= 0) && ($defender['power'] >= 0)){
$hit = ($attacker['attack']/$defender['defense']) + rand(1, 2);
echo "<tr><td>{$count}.xx {$attacker_key} xJednotka {$defender['name']} ({$defender['power']} hp) bola zranená a dostala {$hit} zranenia jednotkou {$attacker['name']} ({$attacker['power']} hp)</td></tr>";
$defender['power'] = $defender['power'] - $hit;
$attacker['power'] = $attacker['power'] - $hit;
if ($defender['power'] <= 0) {
echo "<tr>Jednotka {$defender['name']} umrela, jednotka {$attacker['name']} vyhrala!</tr>";
unset($defender[$defender_key]);
$defender_key = rand(0,count($unit_defender)-1);
$defender =& $unit_attacker_def[$defender_key];
}
if ($attacker['power'] <= 0) {
echo "<tr>Jednotka {$attacker['name']} umrela, jednotka {$defender['name']} vyhrala!</tr>";
unset($attacker[$attacker_key]);
$attacker_key = rand(0,count($unit_attacker_def)-1);
$attacker =& $unit_attacker[$attacker_key];
}
}
}
?>
</table>
<?
break;
答案 0 :(得分:0)
首先,请确保您已启用错误报告:
<?php
// Turn off error reporting
error_reporting(0);
// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report all errors
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>
通过简单的调试,您将找到错误的位置。只需var_dump()每个重要变量并检查其状态。没有人可以为您调试,因为我们不知道您的mysql表中的数据是什么。
尝试使用mysqli_ *函数,因为不推荐使用mysql。
尝试使用类来描述游戏中的所有内容,使其更具可读性和可维护性。例如:
<?php
abstract class Unit{
protected $hp;
protected $damage;
protected $defense;
// Force Extending class to define this method
abstract protected function applyDamage($damage);
}
class GroundUnit extends Unit{
//GroundUnit inherits everything from Unit
//define methods specific for GroundUnits
}
class FlyingUnit extends Unit{
//FlyingUnit inherits everything from Unit
//define methods specific for GroundUnits
}
当您发现错误的位置时,如果您仍然卡住,请使用相关数据更新您的问题。
请注意,您的代码容易受到SQL injection
的攻击$query_street = mysql_query("SELECT * FROM game_army_attacks WHERE attack_id = '".$_GET['id']."' ");//don't put $_GET['id'] directly into query because that is security hole
简单的步骤(与您的示例相关)可以改善对sql注入的保护:
//assume that $_GET['id'] should hold an integer value
$id = isset($_GET['id']? trim($_GET['id']) ? 0);
$id = (int)$id;
现在让我们假装还有一个$ _GET [&#39; some_key&#39;]并且它应该保存字符串值。现在检查会有所不同:
$some_string_value = isset($_GET['some_key']? trim($_GET['some_key']) ? '');
$some_string_value = trim($some_string_value);//triming whitespace
$some_string_value = mysql_real_escape_string($some_string_value);//escaping troublesome quotes(')
此外,您可以使用我认为是首选方式的prepared statements。
答案 1 :(得分:0)
你使用array_rand()
它从数组中选择一个元素当你使用$array[$key]
其中$key = 1
因为数组到var转换它返回你的数组atackers的第二个元素和第二个元素保卫者使用rand(0,count($unit_attacker)-1)
生成随机密钥。但它仍然会很烦人,因为你可以生成相同的密钥))。