Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:59:20+00:00 2026-06-11T02:59:20+00:00

I am creating a PLayer 1 vs Player 2 simple system and the system

  • 0

I am creating a PLayer 1 vs Player 2 simple system and the system limit only till 30 rounds if 30 rounds is done and both player are still alive then it is called a draw. If player 1 gets 0 or less than 0 before 30 rounds and player 2 is still alive then player 2 wins the games and etc…

Problem is why I am still having negative values whats wrong with my code? I already set an if statement in there. Any idea will be a big help for me and I am open for improvements since I am still a beginner programmer THANK YOU.

<?php 

        //Player 1
        $p1Health = 100;
        $p1Attack = 5;
        $p1Speed = 3;

        //Player 2
        $p2Health = 70;
        $p2Attack = 8;
        $p2Speed = 5;

        //Greater speed attack first
        $speed1=0;
        $speed2=0;

        echo '<td>'.$p1Health.'</td><td>'.$p1Attack.'</td><td>'.$p1Speed.'</td>';

        echo '<td>'.$p2Health.'</td><td>'.$p2Attack.'</td><td>'.$p2Speed.'</td>';

        //Compare speed
        if($p1Speed<$p2Speed){
            $speed1=1; //start first
            $speed2=0;
        }
        else {
            $speed1=0; //start first
            $speed2=1;
        }

        $rounds = 30; //maximum rounds
        $count = 0;

        while($count<=30){

            if($p1Health<=0 || $p2Health<=0){ //if any of the players health is equal or below zero loop stop and declare winner

                break;

            }
            else if($speed1==1){

                $p2Health = $p2Health - $p1Attack;

                echo 'Player 2 damaged by '.$p1Attack.' points.Health points left: '.$p2Health.'<br>'; 

                //turn to other player to attack
                $speed1=0;
                $speed2=1;

            }
            else if($speed2==1){

                $p1Health = $p1Health - $p2Attack;

                echo 'Player 1 damaged by '.$p2Attack.' points.Health points left: '.$p1Health.'<br>';

                //turn to other player to attack
                $speed1=1;
                $speed2=0;

            }

            $count++;
        }

        if($p1Health>0 && $p2Health<=0){

            echo 'Player 1 wins the battle';

        }
        else if($p2Health>0 && $p1Health<=0){

            echo 'Player 2 wins the battle';

        }
        else if($p1Health>0 && $p2Health>0){

            echo 'Battle draw';

        }

    ?>

I don’t know if my code is right but this is based for my understanding any idea to improve this will be really a big help for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:59:21+00:00Added an answer on June 11, 2026 at 2:59 am

    Player 1 starts with 100 health. After each attack from player 2, that goes down by 8. After the 12th attack, player 1 will have 4 health. On the 13th attack, that value is reduced by another 8, yielding −4.

    You’ll see this phenomenon any time one player’s attack strength doesn’t evenly divide the other’s health.

    If you don’t want the value to go below zero, even after an attack, then check for that and fix it:

    $p1Health = $p1Health - $p2Attack;
    if ($p1Health < 0)
      $p1Health = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a simple media player using jlGUI and jAudiotagger libraries. I've used a
I'm currently creating my first android app and I've a simple class 'Player' I
I am creating a simple tennis game in Objective-C. Currently, when the player or
I'm creating a simple media player with Silverlight 4 (C#). Now I'm using the
I am creating a simple mp3 player and my first task was to create
I am creating a simple video player but I'm having problem to show the
I was thinking of creating a music player with waveform like in zippyshare like
i am having problem in creating a video player in flash via as3, the
I'm starting my hand at creating a multi-player game using HTML 5 / Javascript.
I'm creating a flex-based video player, using the FLVPlayback component (imported from Flash Pro

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.