예제 #1
0
 /// <summary>
 /// ProcessShot will be called uppon when a ship is found.
 /// It will create a stack with targets it will try to hit. These targets
 /// will be around the tile that has been hit.
 /// </summary>
 /// <param name="row">the row it needs to process</param>
 /// <param name="col">the column it needs to process</param>
 /// <param name="result">the result og the last shot (should be hit)</param>
 protected override void ProcessShot(int row, int col, AttackResult result)
 {
     if (result.Value == ResultOfAttack.Hit) {
         _CurrentState = AIStates.Searching;
         AddTarget(row - 1, col);
         AddTarget(row, col - 1);
         AddTarget(row + 1, col);
         AddTarget(row, col + 1);
     } else if (result.Value == ResultOfAttack.ShotAlready) {
         throw new ApplicationException("Error in AI");
     }
 }
예제 #2
0
        /// <summary>
        /// Shoot at a given row/column
        /// </summary>
        /// <param name="row">the row to attack</param>
        /// <param name="col">the column to attack</param>
        /// <returns>the result of the attack</returns>
        internal AttackResult Shoot(int row, int col)
        {
            _shots += 1;
            AttackResult result = default(AttackResult);

            result = EnemyGrid.HitTile(row, col);

            switch (result.Value)
            {
            case ResultOfAttack.Destroyed:
            case ResultOfAttack.Hit:
                _hits += 1;
                break;

            case ResultOfAttack.Miss:
                _misses += 1;
                break;
            }

            return(result);
        }
예제 #3
0
        // '' <summary>
        // '' Shoot will swap between players and check if a player has been killed.
        // '' It also allows the current player to hit on the enemygrid.
        // '' </summary>
        // '' <param name="row">the row fired upon</param>
        // '' <param name="col">the column fired upon</param>
        // '' <returns>The result of the attack</returns>
        public AttackResult Shoot(int row, int col)
        {
            AttackResult newAttack;
            int          otherPlayer = ((_playerIndex + 1)
                                        % 2);

            newAttack = Player.Shoot(row, col);
            // Will exit the game when all players ships are destroyed
            if (_players(otherPlayer).IsDestroyed)
            {
                newAttack = new AttackResult(ResultOfAttack.GameOver, newAttack.Ship, newAttack.Text, row, col);
            }

            AttackCompleted(this, newAttack);
            // change player if the last hit was a miss
            if ((newAttack.Value == ResultOfAttack.Miss))
            {
                _playerIndex = otherPlayer;
            }

            return(newAttack);
        }
예제 #4
0
        /// <summary>
        /// Checks the results of the attack and switches to
        /// Ending the Game if the result was game over.
        /// </summary>
        /// <param name="result">the result of the last
        /// attack</param>
        /// <remarks>Gets the AI to attack if the result switched
        /// to the AI player.</remarks>
        private static void CheckAttackResult(AttackResult result)
        {
            switch (result.Value)
            {
            case ResultOfAttack.Hit:
                if (object.ReferenceEquals(_theGame.Player, ComputerPlayer))
                {
                    AIAttack();
                }
                break;

            case ResultOfAttack.Miss:
                if (object.ReferenceEquals(_theGame.Player, ComputerPlayer))
                {
                    AIAttack();
                }
                break;

            case ResultOfAttack.GameOver:
                SwitchState(GameState.EndingGame);
                break;
            }
        }
예제 #5
0
        // '' <summary>
        // '' ProcessShot is able to process each shot that is made and call the right methods belonging
        // '' to that shot. For example, if its a miss = do nothing, if it's a hit = process that hit location
        // '' </summary>
        // '' <param name="row">the row that was shot at</param>
        // '' <param name="col">the column that was shot at</param>
        // '' <param name="result">the result from that hit</param>
        protected override void ProcessShot(int row, int col, AttackResult result)
        {
            switch (result.Value)
            {
            case ResultOfAttack.Miss: {
                _CurrentTarget = null;
                break;
            }

            case ResultOfAttack.Hit: {
                ProcessHit(row, col);
                break;
            }

            case ResultOfAttack.Destroyed: {
                this.ProcessDestroy(row, col, result.Ship);
                break;
            }

            case ResultOfAttack.ShotAlready: {
                throw new ApplicationException("Error in AI");
                break;
            }
            }
            if ((_Targets.Count == 0))
            {
                _CurrentState = AIStates.Searching;
            }

            // <summary>
            // ProcessDetroy is able to process the destroyed ships targets and remove _LastHit targets.
            // It will also call RemoveShotsAround to remove targets that it was going to shoot at
            // </summary>
            // <param name="row">the row that was shot at and destroyed</param>
            // <param name="col">the row that was shot at and destroyed</param>
            // <param name="ship">the row that was shot at and destroyed</param>
        }
예제 #6
0
 /// <summary>
 /// The last shot had the following result. Child classes can use this
 /// to prepare for the next shot.
 /// </summary>
 /// <param name="result">The result of the shot</param>
 /// <param name="row">the row shot</param>
 /// <param name="col">the column shot</param>
 protected abstract void ProcessShot(int row, int col, AttackResult result);
예제 #7
0
 /// <summary>
 /// The last shot had the following result. Child classes can use this
 /// to prepare for the next shot.
 /// </summary>
 /// <param name="result">The result of the shot</param>
 /// <param name="row">the row shot</param>
 /// <param name="col">the column shot</param>
 protected abstract void ProcessShot(int row, int col, AttackResult result);
예제 #8
0
        /// <summary>
        /// Shoot will swap between players and check if a player has been killed.
        /// It also allows the current player to hit on the enemygrid.
        /// </summary>
        /// <param name="row">the row fired upon</param>
        /// <param name="col">the column fired upon</param>
        /// <returns>The result of the attack</returns>
        public AttackResult Shoot(int row, int col)
        {
            AttackResult newAttack = default(AttackResult);
            int otherPlayer = (_playerIndex + 1) % 2;

            newAttack = Player.Shoot(row, col);

            //Will exit the game when all players ships are destroyed
            if (_players[otherPlayer].IsDestroyed)
            {
                newAttack = new AttackResult(ResultOfAttack.GameOver, newAttack.Ship, newAttack.Text, row, col);
            }

            if (AttackCompleted != null)
            {
                AttackCompleted(this, newAttack);
            }

            //change player if the last hit was a miss
            if (newAttack.Value == ResultOfAttack.Miss)
            {
                _playerIndex = otherPlayer;
            }

            return newAttack;
        }
예제 #9
0
        /// <summary>
        /// ProcessShot is able to process each shot that is made and call the right methods belonging
        /// to that shot. For example, if its a miss = do nothing, if it's a hit = process that hit location
        /// </summary>
        /// <param name="row">the row that was shot at</param>
        /// <param name="col">the column that was shot at</param>
        /// <param name="result">the result from that hit</param>
        protected override void ProcessShot(int row, int col, AttackResult result)
        {
            switch (result.Value) {
                case ResultOfAttack.Miss:
                    _CurrentTarget = null;
                    break;
                case ResultOfAttack.Hit:
                    ProcessHit(row, col);
                    break;
                case ResultOfAttack.Destroyed:
                    ProcessDestroy(row, col, result.Ship);
                    break;
                case ResultOfAttack.ShotAlready:
                    throw new ApplicationException("Error in AI");
            }

            if (_Targets.Count == 0)
                _CurrentState = AIStates.Searching;
        }
예제 #10
0
using Microsoft.VisualBasic;
예제 #11
0
 /// <summary>
 /// Checks the results of the attack and switches to
 /// Ending the Game if the result was game over.
 /// </summary>
 /// <param name="result">the result of the last
 /// attack</param>
 /// <remarks>Gets the AI to attack if the result switched
 /// to the AI player.</remarks>
 private static void CheckAttackResult(AttackResult result)
 {
     switch (result.Value)
     {
     case ResultOfAttack.Miss:
         if (object.ReferenceEquals(_theGame.Player, ComputerPlayer))
             AIAttack();
         break;
     case ResultOfAttack.GameOver:
         SwitchState(GameState.EndingGame);
         break;
     }
 }
예제 #12
0
        /// <summary>
        /// Listens for attacks to be completed.
        /// </summary>
        /// <param name="sender">the game</param>
        /// <param name="result">the result of the attack</param>
        /// <remarks>
        /// Displays a message, plays sound and redraws the screen
        /// </remarks>
        private static void AttackCompleted(object sender, AttackResult result)
        {
            bool isHuman = false;
            isHuman = object.ReferenceEquals(_theGame.Player, HumanPlayer);

            if (isHuman)
            {
                if (result.Value == ResultOfAttack.Destroyed)
                {
                    UtilityFunctions.Message = "You destroyed the Enemy's" + result.ToString ();
                }
                else
                {
                    UtilityFunctions.Message = "You " + result.ToString ();
                }
            }
            else
            {
                if (result.Value == ResultOfAttack.Destroyed)
                {
                    UtilityFunctions.Message = "The AI destroyed the player's" + result.ToString ();
                }
                else
                {
                    UtilityFunctions.Message = "The AI " + result.ToString ();
                }
            }

            switch (result.Value)
            {
            case ResultOfAttack.Destroyed:
                PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));

                break;
            case ResultOfAttack.GameOver:
                PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));

                while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink")))
                {
                    SwinGame.Delay(10);
                    SwinGame.RefreshScreen();
                }

                if (HumanPlayer.IsDestroyed)
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Lose"));
                }
                else
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Winner"));
                }

                break;
            case ResultOfAttack.Hit:
                PlayHitSequence(result.Row, result.Column, isHuman);
                break;
            case ResultOfAttack.Miss:
                PlayMissSequence(result.Row, result.Column, isHuman);
                break;
            case ResultOfAttack.ShotAlready:
                Audio.PlaySoundEffect(GameResources.GameSound("Error"));
                break;
            }
        }
예제 #13
0
 /// <summary>
 /// ProcessShot will be called uppon when a ship is found.
 /// </summary>
 /// <param name="row">the row it needs to process</param>
 /// <param name="col">the column it needs to process</param>
 /// <param name="result">the result og the last shot (should be hit)</param>
 protected override void ProcessShot(int row, int col, AttackResult result)
 {
     if (result.Value == ResultOfAttack.ShotAlready) {
         throw new ApplicationException("Error in AI");
     }
 }
예제 #14
0
 protected override void ProcessShot(int row, int col, AttackResult result)
 {
 }
예제 #15
0
        /// <summary>
        /// Listens for attacks to be completed.
        /// </summary>
        /// <param name="sender">the game</param>
        /// <param name="result">the result of the attack</param>
        /// <remarks>
        /// Displays a message, plays sound and redraws the screen
        /// </remarks>
        private static void AttackCompleted(object sender, AttackResult result)
        {
            bool isHuman;

            isHuman = _theGame.Player == HumanPlayer;

            if (isHuman)
            {
                UtilityFunctions.Message = "You " + result.ToString();
            }
            else
            {
                UtilityFunctions.Message = "The AI " + result.ToString();
            }

            switch (result.Value)
            {
            case ResultOfAttack.Destroyed: {
                if (isHuman)
                {
                    PlayHitSequence(result.Row, result.Column, isHuman);
                    _count--;
                    Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
                }
                else
                {
                    PlayHitSequence(result.Row, result.Column, isHuman);
                    Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
                }
                break;
            }

            case ResultOfAttack.GameOver: {
                PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GameSound("Sink"));
                while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink")))
                {
                    SwinGame.Delay(10);
                    SwinGame.RefreshScreen();
                }

                if (HumanPlayer.IsDestroyed)
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Lose"));
                }
                else
                {
                    Audio.PlaySoundEffect(GameResources.GameSound("Winner"));
                }
                break;
            }

            case ResultOfAttack.Hit: {
                PlayHitSequence(result.Row, result.Column, isHuman);
                break;
            }

            case ResultOfAttack.Miss: {
                PlayMissSequence(result.Row, result.Column, isHuman);
                break;
            }

            case ResultOfAttack.ShotAlready: {
                Audio.PlaySoundEffect(GameResources.GameSound("Error"));
                break;
            }
            }
        }