tags:

views:

63

answers:

3

I am working on a game like Mafia Wars and i am trying to get the fighting system working but i keep getting lose trying to work out who is going to win the fight and it still needs to know if the stats are close then there is a random chace of them winning.

 $strength = $my_strength;
 $otherplayerinfo = mysql_query("SELECT * FROM accounts WHERE id='$player_id'");
 $playerinfo = mysql_fetch_array($otherplayerinfo);
 $players_strength = $playerinfo['stre'];
 $players_speed = $playerinfo['speed'];
 $players_def = $playerinfo['def'];

 if($players_strength > $strength){
 $strength_point_player = 1;
 $strength_point_your = 0;
 }else{
 $strength_point_your = 1;
 $strength_point_player = 0;
 }

I was trying a point system but i still could not do it.

+2  A: 

You need to work out a formula/algorithm to determine who wins. I'd suggest to think it out and write it out in plain language or pseudo-code first, and then translate it into PHP.

It could be something like

"Compare each players strength, speed and defense. Calculate overall superiority by subtracting one's defense from the other strength and multiplying by speed/10. If overall score is within 5%, pick winner at random"

for example. Or perhaps

"Compare each stat to see if it is greater, equal or within 5% of the other player's stats. Score is 1 if superior, 0 if close enough, -1 if lower. If 0, winner is random. Otherwise higher score wins."

From there you can translate it into logic in your programming language.

Alex JL
well said. Make it like runescape or whatnot with armor, shield, resistance and stuff. This would be a fun project :)
ggfan
+2  A: 

I was trying a point system but i still could not do it.

I strongly urge you to work out what the game mechanic should do first. Once you can describe how combat is supposed to work in a few sentences, then you might get better help as to how to implement it.

William Leader
+1  A: 

First, mysql_fetch_array returns strings only. So, be sure to cast each field to (int) when you assign them to local variables just in case. It makes comparisons behave as expected and easier to pick out in the code exactly what should be happening.

Second, you need an else if($players_strength === $strength) {/* do something random */} in there. Otherwise, combat win will always go to you if stats are the same or greater.

Third, no need to tally up the comparisons between all stats if it's just comparing if one side is greater than the other on each stat. Just total up all stats on each player's side, then do a final comparison on both totals. Otherwise, if you do have special comparisons going on for each individual stat and will be running a tally on stat comparisons, you'll need to increase win flags, not just keep assigning them to 1 or 0 over and over:

$strength_point_your += 1;
$strength_point_player += 0;  // you don't even need this line, just for show
  • These are just very simple solutions, by the way. There's lot's more you can do to refine your code, but focus on getting it to actually run from the beginning.
bob-the-destroyer
Note: mysql_fetch_array() can return a NULL value instead of a string.
Wrikken
@Wrikken correct, but the field values would just be cast to 0 in those cases. Even more than that though, this function can just return `false`, so a whole new set of code needs to be added to handle that case.
bob-the-destroyer