示例#1
0
 /// <summary>
 /// runs when a AI tank hits another tank , storing the power and angle
 /// </summary>
 /// <param name="angle">the angle in which a shot was fired</param>
 /// <param name="power">the amount of power behide the shot fired</param>
 public override void ReportHit(float x, float y)
 {
     // record a fired shot it
     firedShotLandingX.Add(x);
     fireShotLandingY.Add(y);
     // check all players alive in fight and see hit locations matches
     try
     {
         for (int playerNum = 1; playerNum < currentFight.NumPlayers(); playerNum++)
         {
             // check that player is alive and shot didnt hit the tank it came from
             if (!(currentFight.GetPlayerTank() == currentFight.GetBattleTank(playerNum)) &&
                 currentFight.GetBattleTank(playerNum).TankExists())
             {
                 // check X location of player ( give or take -10 or 5 )
                 for (int rangeCheck = 0; rangeCheck < 10; rangeCheck++)
                 {
                     if (currentFight.GetBattleTank(playerNum).GetX() == (int)x + rangeCheck ||
                         currentFight.GetBattleTank(playerNum).GetX() == (int)x - rangeCheck)
                     {
                         this.shotHit.Add(true);
                         break;
                     }
                 }
             }
         }
     }
     catch (IndexOutOfRangeException error)
     {
         MessageBox.Show("Error handling players in AIOpponent.ReportHit");
     }
 }
示例#2
0
        private int[] CalMinAndMaxAngle(int min, int max, Battle currentGame)
        {
            // check where the tank is , if it close to the edge make sure to fire to the inside of battlfeild
            for (int inrange = 0; inrange < 5; inrange++)
            {
                if (currentGame.GetPlayerTank().GetX() == (Battlefield.WIDTH / currentGame.NumPlayers() / 2 + inrange) ||
                    currentGame.GetPlayerTank().GetX() == (Battlefield.WIDTH / currentGame.NumPlayers() / 2 - inrange))
                {
                    // player is on the left hand side of map , should only shoot to the right
                    min = 1;
                }
                if (currentGame.GetPlayerTank().GetX() == ((Battlefield.WIDTH - (Battlefield.WIDTH / currentGame.NumPlayers()) / 2) + inrange) ||
                    currentGame.GetPlayerTank().GetX() == ((Battlefield.WIDTH - (Battlefield.WIDTH / currentGame.NumPlayers()) / 2) - inrange)
                    )
                {
                    // player is on the right side of map , should only shoot to the left
                    max = -5;
                }
            }
            //check if there are other players to the left
            int playersToLeft = 0;

            for (int playerNum = 1; playerNum <= currentGame.NumPlayers(); playerNum++)
            {
                // check to see if a player is left of current player
                if (currentGame.GetPlayerTank().GetX() > currentGame.GetBattleTank(playerNum).GetX()
                    &&
                    currentGame.GetBattleTank(playerNum).TankExists())
                {
                    //increment to show that a player is to the left
                    playersToLeft++;
                }
            }
            // if no player to left exist set minimun angle to 1
            min = (playersToLeft == 0) ? (1) : (min);
            //check if there are other players to the right
            int playersToRight = 0;

            for (int playerNum = 1; playerNum <= currentGame.NumPlayers(); playerNum++)
            {
                // check to see if a player is right of current player
                if (currentGame.GetPlayerTank().GetX() < currentGame.GetBattleTank(playerNum).GetX()
                    &&
                    currentGame.GetBattleTank(playerNum).TankExists())
                {
                    //increment to show that a player is to the right
                    playersToRight++;
                }
            }
            // if no player to right exist set maximun angle to 1
            max = (playersToRight == 0) ? (-5) : (max);
            return(new int[2] {
                min, max
            });
        }