コード例 #1
0
        private void NewTurn()
        {
            // find the next player and their tank
            currentTank   = currentGame.GetPlayerTank();
            currentPlayer = currentTank.GetPlayerNumber();
            //set the title of a form to current round of total rounds
            this.Text = string.Format("Tank battle - Round {0} of {1}", currentGame.CurrentRound(), currentGame.GetNumRounds());
            // set backcolor of controlpanel to currentplayers colour
            controlPanel.BackColor = currentPlayer.PlayerColour();
            // show the current player's name
            currentPlayerLabel.Text = currentPlayer.Name();
            // set angle to current gameplaytank's angle
            this.Aim(currentTank.GetTankAngle());
            //show current tank's power
            this.SetTankPower(currentTank.GetPower());
            //show current windspeed
            //postive values should state its a eastly wind while negative values come from windly west
            if (currentGame.GetWind() >= 0)
            {
                windspeedLabel.Text = string.Format("{0} E", currentGame.GetWind());
            }
            else
            {
                windspeedLabel.Text = string.Format("{0} W", (currentGame.GetWind() * -1)); // times by a negative number to shows a flat value for wind
            }
            //clear weapon choices in weaponSelect
            weaponSelect.Items.Clear();
            // find all weapons available to current tank
            foreach (string weapon in currentTank.GetTank().ListWeapons())
            {
                // add each weapon to selection choice of weaponSelect
                weaponSelect.Items.Add(weapon);
            }
            //set the current weapon to be used of current tank
            SetWeapon(currentTank.GetWeapon());

            if (currentPlayer is PlayerController)
            {
                // give controls for firing a weapon
                currentPlayer.BeginTurn(this, currentGame);
            }
            else if (currentPlayer is AIOpponent)
            {
                //run the firing command on currentplayer
                currentPlayer.BeginTurn(this, currentGame);
            }
        }
コード例 #2
0
ファイル: AIOpponent.cs プロジェクト: AdamBanham/TankFight
        /// <summary>
        /// Runs when a computer play gets a turn , works out how to fire shot depending on aiSetting
        /// </summary>
        /// <param name="gameplayForm"></param>
        /// <param name="currentGame"></param>
        public override void BeginTurn(BattleForm gameplayForm, Battle currentGame)
        {
            //grab the current fight
            currentFight = currentGame;

            if (aiSetting == difficulty.easy)
            {
                // this ai only knows how to fire and doesn't remember if it hits
                //take a random angle and power and fire
                TakeACrappyShot(gameplayForm);
            }
            else if (aiSetting == difficulty.medium)
            {
                //this ai knows how to fire , if it hit last time and where to shoot to hit a tank
                //check if last hit hit a player
                if (this.shotHit.Count() > 0 && this.shotHit.Last())
                {
                    // re aim for previous shot so it might still hit tank again
                    CalForWind(currentGame);
                    // fire shot
                    gameplayForm.Fire();
                    // remove entry to reset in case shot doesnt hit
                    this.shotHit.Clear();
                }
                else // didnt hit with last shot
                {
                    //grab the current wind speed
                    windOfLastTurn = currentGame.GetWind();
                    // work out which way to shoot
                    int    maxAngleFacing = 90;
                    int    minAngleFacing = -90;
                    int [] fireRange;
                    // check where the tank is , if it close to the edge make sure to fire to the inside of battlfeild
                    fireRange = CalMinAndMaxAngle(minAngleFacing, maxAngleFacing, currentGame);
                    //grab min and max from fireRange
                    minAngleFacing = fireRange[0];
                    maxAngleFacing = fireRange[1];
                    // choose a random angle to fire at
                    float randomAngle = (myRandom.Next(minAngleFacing, maxAngleFacing));
                    //choose a random firepwoer
                    int randomFirepower = (myRandom.Next(10, 101));
                    //fire shot
                    TakeAimAndFire(randomAngle, randomFirepower, gameplayForm);
                }
            }
            else if (aiSetting == difficulty.hard)
            {
                //stub
            }
        }
コード例 #3
0
ファイル: AIOpponent.cs プロジェクト: AdamBanham/TankFight
 /// <summary>
 /// used to work out how to hit the last target if the wind changed
 /// </summary>
 /// <param name="currentGame"></param>
 private void CalForWind(Battle currentGame)
 {
     // retake the shot but check for wind change
     // check to see if the wind is blowing more to east
     if (windOfLastTurn > currentGame.GetWind())
     {
         //check firing angle
         // if angle is positive , bullet will travel farer
         // but if angle is negative the bullet will travel less
         if (currentGame.GetPlayerTank().GetTankAngle() > 0)
         {
             // reduce power to hit tank
             currentGame.GetPlayerTank().SetTankPower((currentGame.GetPlayerTank().GetPower() * 98) / 100);
         }
         else
         {
             // increase power to hit tank
             currentGame.GetPlayerTank().SetTankPower((currentGame.GetPlayerTank().GetPower() * 102) / 100);
         }
     }
     else
     {
         // the wind is blowing more to west
         //check angle
         if (currentGame.GetPlayerTank().GetTankAngle() <= 0)
         {
             // tank is firing with the wind , reduce power
             currentGame.GetPlayerTank().SetTankPower((currentGame.GetPlayerTank().GetPower() * 98) / 100);
         }
         else
         {
             // tank is firing against the wind , increase power
             currentGame.GetPlayerTank().SetTankPower((currentGame.GetPlayerTank().GetPower() * 102) / 100);
         }
     }
 }