Jump to content
MakeWebGames

Recommended Posts

Posted

OK guys I'm still working on my minions system but now I am trying to get them to fight each other. I have the basic coding for it done but I'm having a little trouble with the win/lose system.

I'm trying to use a WHILE statement with two conditions.

 

while (($lose >= 0) || ($lose2 >= 0)){
// Attack
$myrand = rand(1,3); 
if ($myrand ==1){
	$type = "Human";
	$q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=1"));
	$base = $q['base'];
	$suc = rand(1,10);
	$tot = $suc + $base;
} else if ($myrand==2){
	$type = "Elf";
	$q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=2"));
	$base = $q['base'];
	$suc = rand(1,10);
	$tot = $suc + $base;
} else {
	$type = "Dwarf";
	$q = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=3"));
	$base = $q['base'];
	$suc = rand(1,10);
	$tot = $suc + $base;
}

//Defend
$myrand2 = rand(1,3); 
if ($myrand2 ==1){
	$type2 = "Human";
	$q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=1"));
	$base2 = $q2['base'];
	$suc2 = rand(1,10);
	$tot2 = $suc2 + $base2;
} else if ($myrand==2){
	$type2 = "Elf";
	$q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=2"));
	$base2 = $q2['base'];
	$suc2 = rand(1,10);
	$tot2 = $suc2 + $base2;
} else {
	$type2 = "Dwarf";
	$q2 = $db->fetch_row($db->query("SELECT base FROM minions WHERE id=3"));
	$base2 = $q2['base'];
	$suc2 = rand(1,10);
	$tot2 = $suc2 + $base2;
}	

if ($tot > $tot2){
	$lose = $lose;
	$lose2 = $lose2 - 1;
	$result = "Player two loses 1 of ".number_format($perc)."";
}	else if ( $tot == $tot2) {
	$lose = $lose;
	$lose2 = $lose2;
	$result = "It was a tie";
} else {
	$lose = $lose-1;
	$lose2 = $lose2;
	$result = "Player one loses 1 of ".number_format($perc)."";
}

print" $result<br>";
}

 

I have been searching and reading on trying to do this in this fashion but I can't find anything pointing me in the right direction. Now this code DOES actually work. It just doesn't stop when one or the other hits 0.

Player two loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

Player one loses 1 of 10

Player one loses 1 of 10

Player one loses 1 of 10

Player one loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

It was a tie

Player two loses 1 of 10

Player one loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

Player one loses 1 of 10

Player one loses 1 of 10

Player two loses 1 of 10

Player one loses 1 of 10

Player two loses 1 of 10

Player two loses 1 of 10

It was a tie

Player one loses 1 of 10

Player one loses 1 of 10

Player one loses 1 of 10

 

Random: 3

Type: Dwarf

Base: 6

Multi: 1

Total: 7

Win/lose: -1 <----- Here

Random: 2

Type: Dwarf

Base: 6

Multi: 5

Total: 11

Win/lose: -4 <----- And here

Posted

I'm on my phone so I'm not going to write it all, but you can use break; and continue; to manipulate loops. That knowledge will come in handy ;)

Put the max turns as the while condition ($i <= 100)

Increment $i each iteration

If $lose > 0 then break;

Etc...

  • 2 weeks later...
Posted

Condition is messed up

 

your condition

while (($lose >= 0) || ($lose2 >= 0)){ //only ONE needs to be true to CONTINUE LOOP

 

Correct Condition

while (($lose >= 0) && ($lose2 >= 0)){ //both must be true to CONTINUE LOOP

 

I don't see why Robot mentioned iteration (max turns), but I would change it to a for loop then

 

for($I = 0; $I < $max_turns; $I++)
{

//all your battle code



if($lose <= 0 || $lose2 <= 0)//exit if loser before max turns
{
break; //exit loop
}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...