Example #1
0
        /// <summary>
        /// This method handles the computer's turn.
        /// </summary>
        /// <param name="player"></param>
        /// <param name="rendering"></param>
        public void TakeShot(Player player, Rendering rendering)
        {
            int[,] possibilityMap;
            int currentHighestX     = 0;
            int currentHighestY     = 0;
            int currentHighestScore = 0;
            int tempScore           = 0;

            hunting = true;

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    if (playerMap[x, y] == 2) //if there is a hit ship that has not been fully destroyed...
                    {
                        hunting = false;      //go into target mode
                    }
                }
            }

            if (hunting)
            {
                possibilityMap = CalculatepossiblePlacements();
            }
            else
            {
                possibilityMap = CalculateTargetedPlacements();
            }

            //clear all previously fired on tiles just to be safe

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    if (playerMap[x, y] != 0)
                    {
                        possibilityMap[x, y] = 0;
                    }
                }
            }

            //choose most likely location of the ship

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    tempScore = possibilityMap[x, y];
                    if (tempScore > currentHighestScore)
                    {
                        currentHighestX     = x;
                        currentHighestY     = y;
                        currentHighestScore = tempScore;
                    }
                }
            }

            player.SquareHit(currentHighestX, currentHighestY, this, rendering);
        }