6 Replies - 1908 Views - Last Post: 19 August 2010 - 05:56 AM Rate Topic: -----

#1 Longblob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-December 09

Fighting System Problem For Text Based Game

Posted 09 August 2010 - 02:19 AM

Hi,

i have created fighting system for text based game project but there are some bug in page problem here it is my code :

1.first of all i have listed NPC character :
<?php
mysql_connect("localhost","root","");
mysql_select_db("rpg");

echo "<table><tr><td>No</td><td>Name</td><td>Power</td><td>HP</td><td>MP</td></tr>";

$sql = mysql_query("SELECT * FROM enemy ORDER BY id_enemy");
while($pilih = mysql_fetch_array($sql))
{
 echo "<tr><td>$pilih[id_enemy]</td><td>$pilih[name]</td><td>$pilih[power]</td><td>$pilih[hp]</td><td>$pilih[mp]</td><td><a href=profight.php?module=fight&act=attack&id=$pilih[id_enemy]>Attack</a></td></tr>";
}  
?>


2.Then it is the battle arena process
mysql_connect("localhost","root","");
mysql_select_db("rpg");

$sql = mysql_query("SELECT * FROM players WHERE id_player='4'");
$sql2 = mysql_query("SELECT * FROM enemy WHERE id_enemy='$_GET[id]' ORDER BY id_enemy");
$row = mysql_fetch_array($sql);
$row2 = mysql_fetch_array($sql2);

if($_GET[module]=='fight' AND $_GET[act]=='attack')
{
 echo "<b>$row[username]</b><br>$row[power]<br>$row[hp]<br>$row[mp]<hr><h2>VERSUS</h2><hr>";
 
 echo "<b>$row2[name]</b><br>$row2[power]<br>$row2[hp]<br>$row2[mp]";
  
 echo "<form action=profight.php?module=hit&id=$row2[id_enemy] method=post>";
 echo "<input type=submit name=attack value=ATTACK>";
 echo "</form>";
 
 echo "<form action=$_SERVER[PHP_SELF]?module=escape&id=row2[id_enemy] method=post>";
 echo "<input type=submit name=escape value=ESCAPE>";
 echo "</form>";
}

elseif(isset($_POST[attack]))
{
 $plevade = rand(1,7) * $row[power];
 $aievade = rand(1,7) * $row2[power];
 $pldamage = $row[power] * 10 / 100;
 $aidamage = $row2[power] * 10 / 100;
 
  if($plevade > $aievade)
  {
   if($row[power] > $row2[power])
   {
    $slspldamage = $pldamage - $aidamage;
	
	mysql_query("UPDATE enemy SET hp = hp - '$slspldamage' WHERE id_enemy='$_GET[id]'");
	
	$sql4 = mysql_query("SELECT * FROM enemy WHERE id_enemy='$_GET[id]'");
	$row3 = mysql_fetch_array($sql4);
	
	echo "<b>$row[username]</b><br>$row[power]<br>$row[hp]<br>$row[mp]<hr><h2>VERSUS</h2><hr>";
 
    echo "<b>$row2[name]</b><br>$row2[power]<br>$row2[hp]<br>$row2[mp]";
  
    echo "<form action=profight.php?module=hit&id=$row2[id_enemy] method=post>";
    echo "<input type=submit name=attack value=ATTACK>";
    echo "</form>";//->>HOW ABOUT YOUR OPINION WITH THIS FORM ACTION?
 
    echo "<form action=$_SERVER[PHP_SELF]?module=escape&id=row2[id_enemy] method=post>";
    echo "<input type=submit name=escape value=ESCAPE>";
    echo "</form>";
	echo "$row3[hp]";
	
   }
   elseif($row[power] < $row2[power])
   {
    $slsaidamage = $aidamage - $pldamage;
	
   	mysql_query("UPDATE players SET hp = hp - '$slsaidamage' WHERE id_player='4'");
   }	
  } 
  elseif($plevade < $aievade)
  {
   if($row[power] > $row2[power])
   {
    $slspldamage = $pldamage - $aidamage;
	
	mysql_query("UPDATE enemy SET hp = hp - '$slspldamage' WHERE id_enemy='$_GET[id]'");
   }
   elseif($row[power] < $row2[power])
   {
    $slsaidamage = $aidamage - $pldamage;
	
   	mysql_query("UPDATE players SET hp = hp - '$slsaidamage' WHERE id_player='4'");
   }	
  }
}


when i tested the fighting system with click some ATTACK button.there are some error occured on it.please help me to build the fighting systme with correct page management/form structure

Is This A Good Question/Topic? 0
  • +

Replies To: Fighting System Problem For Text Based Game

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Fighting System Problem For Text Based Game

Posted 09 August 2010 - 04:09 AM

What is the error that you are getting?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: Fighting System Problem For Text Based Game

Posted 09 August 2010 - 07:11 AM

I rewrote this is to make it more clear:

function openDb() {
	mysql_connect("localhost","root","");
	mysql_select_db("rpg");
}

function getDbArray($sql) {
	$q = mysql_query($sql);
	return mysql_fetch_array($q);
}

function getPlayer($id_player) { return getDbArray("SELECT * FROM players WHERE id_player='" . $id_player . "'"); }

function getEnemy($id_enemy) { return getDbArray("SELECT * FROM enemy WHERE id_enemy='" . $id_enemy . "'"); }

function decPlayerHp($id_player, $hp) { mysql_query("UPDATE players SET hp = hp - $hp WHERE id_player='$id_player'"); }

function decEnemyHp($id_enemy, $hp) { mysql_query("UPDATE enemy SET hp = hp - $hp WHERE id_enemy='$id_enemy'"); }


function showStatus($row, $row2) {
	echo "<b>$row[username]</b><br>$row[power]<br>$row[hp]<br>$row[mp]<hr><h2>VERSUS</h2><hr>";

	echo "<b>$row2[name]</b><br>$row2[power]<br>$row2[hp]<br>$row2[mp]";

	echo "<form action=profight.php?module=hit&id=$row2[id_enemy] method=post>";
	echo "<input type=submit name=attack value=ATTACK>";
	echo "</form>";//->>HOW ABOUT YOUR OPINION WITH THIS FORM ACTION?

	echo "<form action=$_SERVER[PHP_SELF]?module=escape&id=row2[id_enemy] method=post>";
	echo "<input type=submit name=escape value=ESCAPE>";
	echo "</form>";
}


function processAttack($id_player, $id_enemy) {
	if($_GET[module]=='fight' AND $_GET[act]=='attack') {
		$row = getPlayer($id_player);
		$row2 = getEnemy($id_enemy);
		showStatus($row, $row2);
	
	} elseif(isset($_POST[attack])) {
		$row = getPlayer($id_player);
		$row2 = getEnemy($id_enemy);
		
		$plevade = rand(1,7) * $row[power];
		$aievade = rand(1,7) * $row2[power];
		$pldamage = $row[power] * 10 / 100;
		$aidamage = $row2[power] * 10 / 100;
 
		if($plevade > $aievade) {
			if($row[power] > $row2[power]) {
				showStatus($row, $row2);
				decEnemyHp($id_enemy, $pldamage - $aidamage);
				$row2 = getEnemy($id_enemy);
				echo "$row2[hp]";
	
			} elseif($row[power] < $row2[power]) {
				decPlayerHp($id_player, ($aidamage - $pldamage));
			}	
		} elseif($plevade < $aievade) {
			if($row[power] > $row2[power]) {
				decEnemyHp($id_enemy, $pldamage - $aidamage);
			} elseif($row[power] < $row2[power]) {
				decPlayerHp($id_player, ($aidamage - $pldamage));
			}	
		} 
	} else {
		// do I do now?
	}
}

processAttack("4", $_GET[id]);




I also looked at the logic, I'm confused:
if($evade1 > $evade2) {
	if($power1 > $power2) { // damage $row2 for ($power1 -$power2) * 10/100
	} elseif($power1 < $power2) { // damage $row1 for ($power2 -$power1) * 10/100
	} else { // no damange if $power1 == $power2
	}
} elseif($evade1 < $evade2) {
	if($power1 > $power2) { // damage $row2 for ($power1 -$power2) * 10/100
	} elseif($power1 < $power2) { // damage $row1 for ($power2 -$power1) * 10/100
	} else { // no damange if $power1 == $power2
	}
} else {
	// nothing
}



So, $evade1==$evade2 means nothing happens. Otherwise, it's basically ignored. If $power1==$power2, nothing happens. Otherwise, the size with the highest power does damage to the other.

You can more simply write you logic like so:
$damage = $power1 - $power2;
if ($damage>0) { // damage $row2 for $damage * 10/100
} elseif ($damage<0) { // damage $row2 for $damage * -10/100
}



To test your code, just run the functions from the command line in a test.php file. Make sure they work there, then mess with the web pages.

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#4 Longblob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-December 09

Re: Fighting System Problem For Text Based Game

Posted 18 August 2010 - 09:44 PM

Thanks Baavqai i have test this,but why there are some errors in the database

<?php
function openDb() 
{
 mysql_connect("localhost","root","");
 mysql_select_db("rpg");
}
	 
function getDbArray($sql) 
{
 $q = mysql_query($sql);
 return mysql_fetch_array($q);//error occured on this line
}
	 
function getPlayer($id_player) 
{ 
 return getDbArray("SELECT * FROM players WHERE id_player='" . $id_player . "'"); 
}
	 
function getEnemy($id_enemy) 
{ 
 return getDbArray("SELECT * FROM enemy WHERE id_enemy='" . $id_enemy . "'"); 
}
	 
function decPlayerHp($id_player, $hp) 
{ 
 mysql_query("UPDATE players SET hp = hp - $hp WHERE id_player='$id_player'"); 
}
	 
function decEnemyHp($id_enemy, $hp) 
{ 
 mysql_query("UPDATE enemy SET hp = hp - $hp WHERE id_enemy='$id_enemy'"); 
}
	 
	 
function showStatus($row, $row2) 
{
	    echo "<b>$row[username]</b><br>$row[power]<br>$row[hp]<br>$row[mp]<hr><h2>VERSUS</h2><hr>";
	 
	    echo "<b>$row2[name]</b><br>$row2[power]<br>$row2[hp]<br>$row2[mp]";
	 
	    echo "<form action=profight.php?module=hit&id=$row2[id_enemy] method=post>";
	    echo "<input type=submit name=attack value=ATTACK>";
	    echo "</form>";//->>HOW ABOUT YOUR OPINION WITH THIS FORM ACTION?
	 
	    echo "<form action=$_SERVER[PHP_SELF]?module=escape&id=row2[id_enemy] method=post>";
	    echo "<input type=submit name=escape value=ESCAPE>";
	    echo "</form>";
}
	 
	 
function processAttack($id_player, $id_enemy) 
{
 if($_GET[module]=='fight' AND $_GET[act]=='attack') 
 {
  $row = getPlayer($id_player);
  $row2 = getEnemy($id_enemy);
  showStatus($row, $row2);
	     
 } 
 elseif(isset($_POST[attack])) 
 {
  $row = getPlayer($id_player);
  $row2 = getEnemy($id_enemy);
	         
  $plevade = rand(1,7) * $row[power];
  $aievade = rand(1,7) * $row2[power];
  $pldamage = $row[power] * 10 / 100;
  $aidamage = $row2[power] * 10 / 100;
	  
  if($plevade > $aievade) 
  {
   if($row[power] > $row2[power]) 
   {
	showStatus($row, $row2);
	decEnemyHp($id_enemy, $pldamage - $aidamage);
	$row2 = getEnemy($id_enemy);
	echo "$row2[hp]";
	     
   } 
   elseif($row[power] < $row2[power]) 
   {
	decPlayerHp($id_player, ($aidamage - $pldamage));
   }  
  } 
  elseif($plevade < $aievade) 
  {
   if($row[power] > $row2[power]) 
   {
	decEnemyHp($id_enemy, $pldamage - $aidamage);
   } 
   elseif($row[power] < $row2[power]) 
   {
	decPlayerHp($id_player, ($aidamage - $pldamage));
   }  
  }
 } 
 else 
 {
  echo "run";
 }
}
processAttack("4", $_GET[id]);

?>


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Latihan\profight.php on line 11

why the SQL is invalid?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Fighting System Problem For Text Based Game

Posted 18 August 2010 - 11:30 PM

you would have to look at the SQL error message (via mysql_error()).

(see also http://www.dreaminco...ling-in-php-5/)

This post has been edited by Dormilich: 18 August 2010 - 11:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: Fighting System Problem For Text Based Game

Posted 19 August 2010 - 02:34 AM

View PostLongblob, on 18 August 2010 - 10:44 PM, said:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Latihan\profight.php on line 11


I would guess you haven't called openDb() yet. That's the danger of the global mysql state approach. I prefer to pass a connection object around, but you chose the former route.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Fighting System Problem For Text Based Game

Posted 19 August 2010 - 05:56 AM

the advantage of object constructors, they are "self-calling".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1