Ejemplo n.º 1
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)
        {
            AttackResult result = default(AttackResult);

            result = EnemyGrid.HitTile(row, col);

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

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

            return(result);
        }
Ejemplo n.º 2
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?.Invoke(this, newAttack);

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

            return(newAttack);
        }
Ejemplo n.º 3
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)
        {
            if (result.Value == ResultOfAttack.Miss)
            {
                _CurrentTarget = null;
            }
            else if (result.Value == ResultOfAttack.Hit)
            {
                ProcessHit(row, col);
            }
            else if (result.Value == ResultOfAttack.Destroyed)
            {
                ProcessDestroy(row, col, result.Ship);
            }
            else if (result.Value == ResultOfAttack.ShotAlready)
            {
                throw (new ApplicationException("Error in AI"));
            }

            if (_Targets.Count == 0)
            {
                _CurrentState = AIStates.Searching;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The AI takes its attacks until its go is over.
        /// </summary>
        /// <returns>The result of the last attack</returns>
        public override AttackResult Attack()
        {
            AttackResult result = default(AttackResult);
            int          row    = 0;
            int          column = 0;

            //take single turn, return result of shot
            Delay();

            GenerateCoords(ref row, ref column);
            //generate coordinates for shot
            result = _game.Shoot(row, column);
            //take shot
            ProcessShot(row, column, result);

            if (result.Value == ResultOfAttack.Destroyed)
            {
                result.Text = "destroyed your";
                Console.WriteLine(result.Text);
            }


            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the _ai to attack.
        /// </summary>
        /// <remarks>
        /// Checks the attack result once the attack is complete.
        /// </remarks>
        private static void AIAttack()
        {
            AttackResult result = _myGame.Player.Attack();

            CheckAttackResult(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the player to attack the indicated row and column.
        /// </summary>
        /// <param name="row">the row to attack</param>
        /// <param name="col">the column to attack</param>
        /// <remarks>
        /// Checks the attack result once the attack is complete
        /// </remarks>
        public static void Attack(int row, int col)
        {
            AttackResult result = _myGame.Shoot(row, col);

            CheckAttackResult(result);
        }
Ejemplo n.º 7
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 = _myGame.Player == HumanPlayer;

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

            switch (result.Value)
            {
            case ResultOfAttack.Destroyed: {
                PlayHitSequence(result.Row, result.Column, isHuman);
                if (Extentions.MusicPlaying == true)
                {
                    Audio.PlaySoundEffect(GameResources.GetSound("Sink"));
                }
                else if (Extentions.MusicPlaying == false)
                {
                }

                break;
            }

            case ResultOfAttack.GameOver: {
                PlayHitSequence(result.Row, result.Column, isHuman);
                Audio.PlaySoundEffect(GameResources.GetSound("Sink"));
                while (Audio.SoundEffectPlaying(GameResources.GetSound("Sink")))
                {
                    SwinGame.Delay(10);
                    SwinGame.RefreshScreen();
                }
                if (HumanPlayer.IsDestroyed)
                {
                    if (Extentions.MusicPlaying == true)
                    {
                        Audio.PlaySoundEffect(GameResources.GetSound("Lose"));
                    }
                }
                else
                {
                    if (Extentions.MusicPlaying == true)
                    {
                        Audio.PlaySoundEffect(GameResources.GetSound("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: {
                if (Extentions.MusicPlaying == true)
                {
                    Audio.PlaySoundEffect(GameResources.GetSound("Error"));
                }

                break;
            }
            }
        }
Ejemplo n.º 8
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;
         case ResultOfAttack.Hit:
             if (object.ReferenceEquals(_theGame.Player, ComputerPlayer))
                 AIAttack();
             break;
         case ResultOfAttack.Destroyed:
             if (object.ReferenceEquals(_theGame.Player, ComputerPlayer))
                 AIAttack();
             break;
     }
 }
Ejemplo n.º 9
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) {

                UtilityFunctions.Message = "You " + 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;
            }
        }
Ejemplo n.º 10
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);
Ejemplo n.º 11
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;
        }
Ejemplo n.º 12
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);
 /// <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)
 {
 }
Ejemplo n.º 14
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.TargetingShip;
         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");
     }
 }
Ejemplo n.º 15
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);

            //			Console.WriteLine(newAttack.Text);
            }

            //change player
            if (newAttack.Value == ResultOfAttack.Miss || newAttack.Value == ResultOfAttack.Hit || newAttack.Value == ResultOfAttack.Destroyed) {
                _playerIndex = otherPlayer;
            }

            return newAttack;
        }
        /// <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)
        {
        }