Jump to content

Can this be done?


Kryllster

Recommended Posts

I want to write a block of code that uses case switch, but make it kind of linear something like this =>

 

case 'fight1':
$mob_hp = 30;
// execute more code then carry over $mob_hp to next switch where new // hp is given the name of $mob_hp
break;
case 'fight2':
$mob_hp = 25;
// and so forth till the requirements are met
break;

was just wondering can this be done some way or do I need to send it with the header to the next switch??

 

Hope you understand what Im trying to get at??

Link to comment
https://forums.phpfreaks.com/topic/172362-can-this-be-done/
Share on other sites

Ok maybe some more code then might help explaining =>

<?php
$fight = (!isset($_GET['fight'])) ? 'main' : $_GET['fight'];
switch($fight){
case 'fight1':
$fight = $_POST['fight'];
$hitpoints = 30;
$monster_hp = 30;
$attack = $base_bonus + $weapon_bonus;
$mob_attack = $base_bonus + 3;

$first = mt_rand(1,100);
if($first <= 60){
	$first = "The Cultist Guard attacks first causing $mob_attack points of damage!";
	$hitpoints = $hitpoints - $mob_attack;
	echo "You now have $hitpoints hitpoints left!";
	exit();
}
if($first > 60){
	$first = "You attack first causing $attack points of damage!";
	$mob_hitpoints = $mob_hitpoints - $attack;
	echo "The Cultist Guard has $mob_hitpoints left!";
	exit();
}
header("Location:fight_view.php?fight=fight2&hitpoints=$hitpoints&monster_hp=$monster_hp");
break;
case 'fight2':
$fight = $_POST['fight'];
$hitpoints = 30;
$monster_hp = 30;
$attack = $base_bonus + $weapon_bonus;
$mob_attack = $base_bonus + 3;

if(isset($_POST['fight'])){
$first = mt_rand(1,100);
if($first <= 60){
	$first = "The Cultist Guard attacks first causing $mob_attack points of damage!";
	$hitpoints = $hitpoints - $mob_attack;
	echo "You now have $hitpoints hitpoints left!";
	exit();
}
if($first > 60){
	$first = "You attack first causing $attack points of damage!";
	$mob_hitpoints = $mob_hitpoints - $attack;
	echo "The Cultist Guard has $mob_hitpoints left!";
	exit();
}
header("Location:fight_view.php?fight=fight3&hitpoints=$hitpoints&monster_hp=$monster_hp");
}
else {
	echo "Please Click the Link to continue the fight!";
	exit();
}
break;
 }
}
?>

 

I want to be able to carry over the reduction of hitpoints to the next case or switch what ever you call it but without having to send it like this

 

header("Location:fight_view.php?fight=fight3&hitpoints=$hitpoints&monster_hp=$monster_hp");

 

 

hope that makes it a little more clearer??

Link to comment
https://forums.phpfreaks.com/topic/172362-can-this-be-done/#findComment-908806
Share on other sites

Ok, you want a function.

 

function checkfight($fight, $hitpoints=30, $monster_hp=30) {
  switch ($fight) {
    // rest of your code
  }
}

 

Then you need to (within the 'rest of your code part) replace the header calls with for example...

 

checkfight('fight2', $hitpoints, $monster_hp);

 

And to get the entire process started....

 

$fight = (!isset($_GET['fight'])) ? 'main' : $_GET['fight'];
checkfight($fight);

 

This isn't going to wrok straight out of the box because within each case you are redefining some vars...

 

$fight = $_POST['fight'];
$hitpoints = 30;
$monster_hp = 30;
$attack = $base_bonus + $weapon_bonus;
$mob_attack = $base_bonus + 3;

 

You'll need to fix all that but this should get you a good start.

Link to comment
https://forums.phpfreaks.com/topic/172362-can-this-be-done/#findComment-908819
Share on other sites

Ok this is where I'm at now and I get a blank page? here is what I have

 

<?php
function checkfight($fight, $hitpoints=30, $monster_hp=30) {
  switch ($fight) {
    $attack = 3;
    $mob_attack = 3;
    $first = mt_rand(1,100);
if($first <= 50){
	echo "<center><b>The Cultist Guard attacks first causing $mob_attack points of damage!</b></center>";
	echo "<br>";
	echo "<br>";
	$hitpoints = $hitpoints - $mob_attack;
	echo "<table width=\"500\">";
	echo "<td>";
	echo "<center>Your Hitpoints:$hitpoints</center>";
	echo "</td>";
	echo "<td>";
	echo "<center>Monster Hitpoints:$mob_hp</center>";
	echo "</td>";
	echo "</tr>";
	echo "<table>";
	echo "<br>";
	echo "<center>You now have $hitpoints hitpoints left!</center>";
	echo "<br>";
	echo "<br>";
	echo "<center>";
	echo "<a href=\"fight_view.php?fight=checkfight('fight2', $hitpoints, $monster_hp);\">Continue The Fight!</a>";
	echo "</center>";
	exit();
}
if($first > 50){
	echo "<center><b>You attack first causing $attack points of damage!</center></b>";
	echo "<br>";
	echo "<br>";
	$mob_hp = $mob_hp - $attack;
	echo "<table width=\"500\">";
	echo "<td>";
	echo "<center>Your Hitpoints:$hitpoints</center>";
	echo "</td>";
	echo "<td>";
	echo "<center>Monster Hitpoints:$mob_hp</center>";
	echo "</td>";
	echo "</tr>";
	echo "<table>";
	echo "<br>";
	echo "<center>The Cultist Guard has $mob_hp hitpoints left!</center>";
	echo "<br>";
	echo "<br>";
	echo "<center>";
	echo "<a href=\"fight_view.php?fight=checkfight('fight2', $hitpoints, $monster_hp);\">Continue The Fight!</a>";
	echo "</center>";
	exit();
}
  }
switch('fight2'){
$first = mt_rand(1,100);
if($first <= 50){
	echo "<center><b>The Cultist Guard attacks first causing $mob_attack points of damage!</b></center>";
	echo "<br>";
	echo "<br>";
	$hitpoints = $hitpoints - $mob_attack;
	echo "<table width=\"500\">";
	echo "<td>";
	echo "<center>Your Hitpoints:$hitpoints</center>";
	echo "</td>";
	echo "<td>";
	echo "<center>Monster Hitpoints:$mob_hp</center>";
	echo "</td>";
	echo "</tr>";
	echo "<table>";
	echo "<br>";
	echo "<center>You now have $hitpoints hitpoints left!</center>";
	echo "<br>";
	echo "<br>";
	echo "<center>";
	echo "<a href=\"fight_view.php?fight=checkfight('fight2', $hitpoints, $monster_hp);\">Continue The Fight!</a>";
	echo "</center>";
	if($hitpoints <= 0){
		header("Location:mainview.php?show=d4fight_failure");
		exit();
	}
	exit();
}
if($first > 50){
	echo "<center><b>You attack first causing $attack points of damage!</b></center>";
	echo "<br>";
	echo "<br>";
	$mob_hp = $mob_hp - $attack;
	echo "<table width=\"500\">";
	echo "<td>";
	echo "<center>Your Hitpoints:$hitpoints</center>";
	echo "</td>";
	echo "<td>";
	echo "<center>Monster Hitpoints:$mob_hp</center>";
	echo "</td>";
	echo "</tr>";
	echo "<table>";
	echo "<br>";
	echo "<center>The Cultist Guard has $mob_hp hitpoints left!</center>";
	echo "<br>";
	echo "<br>";
	echo "<center>";
    echo "<a href=\"fight_view.php?fight=checkfight('fight2', $hitpoints, $monster_hp);">Continue The Fight!</a>";
    echo "</center>";
    if($mob_hp <= 0){
	    header("Location:d4fight1_special.php");
	    exit();
    }
    exit();
}
}
echo "<a href=\"d4fight2_fight.php?fight=fight\">Start The Fight</a>";
$fight = (!isset($_GET['fight'])) ? 'main' : $_GET['fight'];
checkfight($fight);
?>

I don't understand functions very well but I want to learn why is this not showing up?

Link to comment
https://forums.phpfreaks.com/topic/172362-can-this-be-done/#findComment-908883
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.