用PHP进行逻辑计算

时间:2012-06-30 21:59:56

标签: php mysql sql

我对php计算有疑问。 我正在重组一个基于文本的游戏。 这场比赛现在有两名“本土球员”。它们会被cron文件定期插入。 插入是:

//播放器1登录ID = 54

mysql_query("INSERT INTO {$game}_ships (ship_name,login_name,shipclass,class_name,class_name_abbr,fighters,max_fighters,max_shields,cargo_bays,mine_rate_metal,mine_rate_fuel,config,size,upgrades,move_turn_cost,point_value,location,login_id
    ) values(   'Three 7','A','8','name','WM','4773','4773','8353','2003','5','45','bs:dt:ot','6','99','19','10',FLOOR(RAND() * 298 + 2),'54')");

玩家二有相同的插入,只有玩家ID不同。 让我们说44

它们被插入随机位置 FLOOR(RAND()* 298 + 2),从2到300不等。

我想在游戏中创建一个 AI ,方法是选择同一系统中这两个玩家的船只,比较战斗机数量和从数据库中删除最弱的数据。

到目前为止,我有:

mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 54 AND 54 AND `location` = 234");

但我对其他人有一些问题。

非常感谢任何评论或帮助。

PS:我随意选择 location = 234“

2 个答案:

答案 0 :(得分:1)

您可以遍历所有位置并检查每个位置,看看每个位置是否至少有一艘船;如果确实如此,那么你就会破坏弱者。参见:

for ($i = 2; $i < 300; $i++) {
  $p1 = mysql_fetch_array(mysql_query("SELECT count(*) AS 'q' FROM `{$game}_ships` WHERE `login_id` = 54 AND `location` = " . $i));
  $p2 = mysql_fetch_array(mysql_query("SELECT count(*) AS 'q' FROM `{$game}_ships` WHERE `login_id` = 44 AND `location` = " . $i));
  if ($p1['q'] > 0 && $p2['q'] > 0) {
    //Get the first ship; if you want, can select a ship randomly
    $p1s = mysql_fetch_array(mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 54 AND `location` = ".i));
    $p2s = mysql_fetch_array(mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 44 AND `location` = ".i));
    //Here you compare $p1s and $p2s, and check which one is stronger. I am using the 'fighters' value, but you can change it if I misunderstood you
    if ($p1s['fighters'] > $ps2['fighters'])
      mysql_query ("DELETE FROM `{$game}_ships` WHERE `ship_name` = " + $ps2['ship_name']);
    else //Not checking for ties here, you can add it too
      mysql_query ("DELETE FROM `{$game}_ships` WHERE `ship_name` = " + $ps1['ship_name']);
  }
}

我不确定我是否完全理解你,但我希望这至少是你想要的一部分。

答案 1 :(得分:1)

此查询将为您提供玩家54和44都有船只的位置列表:

SELECT distinct p1s.location
FROM `{$game}_ships` p1s JOIN `{$game}_ships` p2s ON p1s.location=p2s.location
WHERE p1s.login_id=54 AND p2s.login_id=44;

此查询将删除位置234中的最弱的玩家54或44:

DELETE from `{$game}_ships`
WHERE login_id IN (54, 44) AND location=234
ORDER BY fighters LIMIT 1;