PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,365 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,437 people online right now. Registration is fast and FREE... Join Now!




Php Battle Script

 

Php Battle Script

nick1200

30 Jun, 2009 - 08:57 AM
Post #1

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions


i have been thinking about how would i set this up
i have a rpg and would like a battle script

so the user battles some 1 or a monster and goes up a lvl

i all ready have a battle script but its two basic

the one i have now is they click a link and its a 50/50 chance of them winning or losing if they win they go up a lvl and get money

in the database i have attack defence and health of each pokemon

was just wounding were would i start

is there any open source battle script i have searched google ( mans best friend) but theres nothing



User is offlineProfile CardPM
+Quote Post


gregwhitworth

RE: Php Battle Script

30 Jun, 2009 - 09:19 AM
Post #2

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 82 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
Like always - it would be really helpful if you posted your code. You say that you have defense and health but if it is a 50/50 chance then you must not be taking into account these elements in your equation.

Example:

If I come to battle a pokemon with:

Health: 50, Defense: 75, Attack: 125, Magic: 50

and I have:

Health: 75, Defense 65, Attack: 100, Magic: 25

shouldn't the probability of me loosing go up.

Just a thought.

Please build on this:

CODE

$y = 0; // If 'y' equals 1 then you win

// USERS SETTINGS
$attack = $_POST['attack'];
$defense = $_POST['defense'];
$health = $_POST['health'];
$magic = $_POST['magic'];

// VS SETTINGS
$vattack = $_POST['vattack'];
$vdefense = $_POST['vdefense'];
$vhealth = $_POST['vhealth'];
$vmagic = $_POST['vmagic'];


I'm going to thing about the for loop that is needed and the probablity of the overall attack.

Because remember each element has a probability against the other that will effect the overall probability of the match.

--

Greg

This post has been edited by gregwhitworth: 30 Jun, 2009 - 09:19 AM
User is offlineProfile CardPM
+Quote Post

nick1200

RE: Php Battle Script

30 Jun, 2009 - 09:25 AM
Post #3

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions
heres my battle script smile.gif

CODE

div class="mid_box">
                    <div class="mid_box_title">Your Pokemon</div>
                    <?php
                        // show the user their pokemon
                        include_once('pokemon_level.php');
                        // end file include
                    ?>
            </div>
            
            <div class="mid_box">
                <div class="mid_box_title">Current Battle</div>
                
                <img src="/pokemon/<?php echo $pokemon->{ 'pok' . $_SESSION['current_pokemon']}['pokePIC']; ?>" width="107" height="102">
                <div align="right"><img src="gym ledders/074.png" width="158" height="141"></div>

<?php
        if (isset($_POST['passalong']) && ($_SESSION['level_aquired'] == 2)) {    
            
            switch (Battle_Generator()) {
                case 1:
                    $_SESSION['Battle_Comment'] = 'You have lost the battle.';
                    echo ('<meta http-equiv="refresh" content="0;url=http:/Gyms.php">');
                    die;
                  break;
                
                case 0:
                    $_SESSION['Battle_Comment'] = "You have won the battle,<br/>
                            addiontally your pokemon has gained 2 levels,<br/>
                            you have gained 300 exp you have also won &pound;10.";
                    echo ('<meta http-equiv="refresh" content="0;url=http:/Gyms.php">');
                    die;
                  break;        
            }

        } else {
           ?>
            <form method="post" action="" name="myform" id="myform">                    
                      <p class="style3">Moves</p>
                        <a href="#" onClick="java script:PerformAttack('Ember');return false;"><h2>Ember<h2></a>
                        <a href="#" onClick="java script:PerformAttack('Fire Blast');return false;"><h2>Fire Blast</h2></a>

                        <a href="#" onClick="java script:PerformAttack('Slash');return false;"><h2>Slash<h2></a>
                        <a href="#" onClick="java script:PerformAttack('Fire Spin');return false;"><h2>Fire Spin</h2></a>

                        <input type="hidden" id="passalong" name="passalong" value=""/>
            </form>
<?php
        }
?>
    <script language="JavaScript" type="text/javascript">
        function PerformAttack(VAR) {
            <?php $_SESSION['level_aquired'] = 2; ?>
            document.getElementById("passalong").value = VAR;
            document.myform.submit();
        }
    </script>
            
            </div>
        </div>                
    </div>
    
<?php
    // random generator
    function Battle_Generator() {

        function executeWin() {
            $sql = "UPDATE user_pokemon
                    SET
                    pok".$_SESSION['current_pokemon']."LV = pok".$_SESSION['current_pokemon']."LV + 2,
                    pokeMONEY = pokeMONEY + 10,
                    pok".$_SESSION['current_pokemon']."EXP = pok".$_SESSION['current_pokemon']."EXP + 300
                    WHERE username = '$_SESSION[username]'";
    $results = mysql_query($sql) or die(mysql_error());

        }
        
        $gen_rand = array('Win','Lose', 'Lose');
        //$gen_rand = array('Win'); // for testing only

        $rand_pick = $gen_rand[array_rand($gen_rand)];
        
        if ($rand_pick == "Win") {
            executeWin();
            $_SESSION['level_aquired'] = 0;
            $_POST['passalong'] = '';
            return 0;
        }
        elseif ($rand_pick == "Lose") {
            $_SESSION['level_aquired'] = 0;
            $_POST['passalong'] = '';
            return 1;
        }
    }
    
    // all the stuff on the right column of the page
        include_once('sections/new_rightbar.php');
        
    // close everything down with a footer
    require('sections/footer.php');

?>



a lot of coding biggrin.gif



p.s i think i no how i would do the session bit but would would i make the info from the database into a session

like $_POST['attack'];
and so on

This post has been edited by nick1200: 30 Jun, 2009 - 09:31 AM
User is offlineProfile CardPM
+Quote Post

gregwhitworth

RE: Php Battle Script

30 Jun, 2009 - 09:38 AM
Post #4

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 82 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
A simple method could be the following:

CODE

$probability = array();
$items = array($attack, $defense, $health, $magic);

foreach($items as $value) {
      if($value is > ${"v" . $value}) {
                $probability[$value] = 1;
      }
      else {
               $probability[$value] = 0;
       }
}

// Gets number of all set '1s' and '0s'
$probability = $probability['attack'] + $probability['defense'] + $probability['health'] + $probability['magic'];

if($probability >= count($items)) {
         echo "You have a stronger chance of winning";
}

else {
          echo "You will probably loose";
}



Like I said it is simple, because you would then take that final number into another equation I think, but I'm not a mathematician. Hope that helps.

--

Greg

This post has been edited by gregwhitworth: 30 Jun, 2009 - 10:01 AM
User is offlineProfile CardPM
+Quote Post

nick1200

RE: Php Battle Script

30 Jun, 2009 - 09:41 AM
Post #5

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions
we don't have items or magic on there yet tongue.gif

i guess that would just make it pop up if they would win or not ?


that would just get the users pokemon attack and stuff ?

well wouldn't cus it ent connecting to the db or anything tongue.gif

This post has been edited by nick1200: 30 Jun, 2009 - 10:03 AM
User is offlineProfile CardPM
+Quote Post

gregwhitworth

RE: Php Battle Script

30 Jun, 2009 - 10:03 AM
Post #6

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 82 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
Well exactly, I basically did away with the $y = 0,

but basically it takes each number and compares them and gives it a 1 or 0 if it is or isn't larger. Then it counts the array of items and if it is greater or equal to them all then they win.

It would make it hard to win because they would have to beat them in every category, you may want to say:
CODE

$grace = 1;
$count = count($items) - $grace;
if($probability >= $count) {
         echo "You have a stronger chance of winning";
}


Hope that helps.
User is offlineProfile CardPM
+Quote Post

nick1200

RE: Php Battle Script

30 Jun, 2009 - 10:09 AM
Post #7

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions


can i say 1 thing tho what if all there pokemon are less that the 1 that there going against ? then there messed lol ?

smile.gif real good idea tho smile.gif
User is offlineProfile CardPM
+Quote Post

gregwhitworth

RE: Php Battle Script

30 Jun, 2009 - 10:41 AM
Post #8

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 82 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
That's why you would run a php script on the previous page using a probability equation (which could be really long)

if (x > y) {
$prob = x * .025;}

etc...

That would show the user the probability in percentage of them winning so they don't destroy their pokes too bad. OR!!

You could simply use the data of the other pokes they're going against and let the user use common sense as to the chances of them winning.

--

Greg
User is offlineProfile CardPM
+Quote Post

nick1200

RE: Php Battle Script

30 Jun, 2009 - 10:47 AM
Post #9

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions


could u help me with another problem ?

i think im gonna do the user boxes be for the battle script ( seems hard )


if i shows u some of my coding will u take a look ?
User is offlineProfile CardPM
+Quote Post

nick1200

RE: Php Battle Script

30 Jun, 2009 - 10:54 AM
Post #10

D.I.C Addict
****

Joined: 21 Mar, 2009
Posts: 551



Thanked: 6 times
My Contributions
in the rpg the user has 6 pokemon

i wanna try and make it so when the user ent using these 6 pokemon they can add them to there box and take some new pokemon out

but i worked out i cant just take it out of user_pokemon and add it into user_box

because there are sessions made

here the coding
that shows the sessions

yourpokemon.php
CODE


<?php
    function bgColor($var) {
        if ($_SESSION['current_pokemon'] == $var) {
            return 'bgcolor="#FF9900"';
        } else {
            return 'bgcolor="#333333"';
        }
    }
            
    function pokmessage($var) {
        if ($_SESSION['current_pokemon'] == $var) {
            return 'ACTIVE';
        } else {
            return '<a href="java script:ChangeActive(\''.$var.'\');">'.$var.'</a>';
        }
    }
?>
        <input type="hidden" name="Changeactive2" id="Changeactive2" value=""/>
      </form>
      <tr bordercolor="black">
        <?php for ($x=1;$x<4;$x++) { ?>
        <td <?php echo bgColor($x); ?>><center class="style7">
            <span class="style7"><b><?php echo pokmessage($x); ?></b></span>
        </center></td>
        <?php } ?>
      </tr>
      <tr bordercolor="black">
        <?php
    if (isset($pokemon->pok1['pokeNAME'])) { ?>
        <th scope="row" align="center" width="70px"> <img src="/pokemon/<?php echo $pokemon->pok1['pokePIC']; ?>" alt="a" width="80" height="80"/>
            <p><?php echo $pokemon->pok1['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[0].'<br/>';
                echo 'EXP:'.$pokemon->exp[0]; ?> </p></th>
        <?php }
    if (isset($pokemon->pok2['pokeNAME'])) { ?>
        <th scope="row" align="center" width="70px"> <img src="/pokemon/<?php echo $pokemon->pok2['pokePIC']; ?>" alt="a" width="80" height="80"/>
            <p><?php echo $pokemon->pok2['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[1].'<br/>';
                echo 'EXP:'.$pokemon->exp[1]; ?> </p></th>
        <?php }
    if (isset($pokemon->pok3['pokeNAME'])) { ?>
        <th scope="row" align="center" width="70px"> <img src="/pokemon/<?php echo $pokemon->pok3['pokePIC']; ?>" alt="a" width="80" height="80"/>
            <p><?php echo $pokemon->pok3['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[2].'<br/>';
                echo 'EXP:'.$pokemon->exp[2]; ?> </p></th>
        <?php }
    if (isset($pokemon->pok4['pokeNAME'])) { ?>
        <?php }
    if (isset($pokemon->pok5['pokeNAME'])) { ?>
        <?php }
    if (isset($pokemon->pok6['pokeNAME'])) { ?>
        <?php }  ?>
      </tr>
    </table>
      <table width="343" height="30" border="1">
        <tr>
          <td width="77"><p>&nbsp;</p>
              <p><img src="/pokemon/<?php echo $pokemon->pok4['pokePIC']; ?>" alt="a" width="80" height="80"/></p>
            <p><?php echo $pokemon->pok4['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[3].'<br/>';
                echo 'EXP:'.$pokemon->exp[3]; ?></p>
            <p>&nbsp;</p></td>
          <td width="77"><p><img src="/pokemon/<?php echo $pokemon->pok5['pokePIC']; ?>" alt="a" width="80" height="80"/></p>
              <p><?php echo $pokemon->pok5['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[4].'<br/>';
                echo 'EXP:'.$pokemon->exp[4]; ?></p></td>
          <td width="77"><p><img src="/pokemon/<?php echo $pokemon->pok6['pokePIC']; ?>" alt="a" width="80" height="80"/></p>
              <p><?php echo $pokemon->pok6['pokeNAME'].'<br/>';  echo 'Level:'.$pokemon->levels[5].'<br/>';
                echo 'EXP:'.$pokemon->exp[5]; ?></p></td>
        </tr>
      </table></td>
  </tr>
</table>
<p>&nbsp;</p>





has you can see there are $poke1 $poke2 sessions sad.gif




how could i take them 6 pokemon out of that table and add them into another

This post has been edited by nick1200: 30 Jun, 2009 - 11:29 AM
User is offlineProfile CardPM
+Quote Post

gregwhitworth

RE: Php Battle Script

30 Jun, 2009 - 08:15 PM
Post #11

(this).problem + "sucks";
Group Icon

Joined: 20 Jan, 2009
Posts: 1,131



Thanked: 82 times
Dream Kudos: 50
Expert In: HTML, CSS, Web Design

My Contributions
To be honest - that doesn't make much sense - can you explain it in a little more detail and I'll take a look at it in the morning?

Also, I thought about it and you need to randomize the array from before. So:

CODE

$probability = array();
$items = array($attack, $defense, $health, $magic);

foreach($items as $value) {
      if($value is > ${"v" . $value}) {
                $probability[$value] = 1;
      }
      else {
               $probability[$value] = 0;
       }
}

// Gets number of all set '1s' and '0s'
$prob = rand($probability);

if ($prob == 1) {
      echo "You win";}

else {
      echo "You loose";}



Earlier I was making it too difficult, basically for each one you will get a 1 or a 0 based on your initial values. Then you have php do a random search of the array, so your basic probabilities remain, and it will be much more realistic because you could still loose even if you beat the villian in 3 of the 4 areas, your probability is 1/4 but there still is a chance.

I look forward to seeing progress.

--

Greg
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:43PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month