繁体   English   中英

除非逐步调试,否则控制台程序将挂起

[英]Console program hangs unless stepping through with debugger

嗨,我正在编写一艘战舰游戏,但是如果我设置多艘船的位置,它似乎会挂起。 但是,如果我单步执行代码,它将起作用。 这让我感到困惑。 这是给我悲伤的代码。

    class BsGame
    {
        private  GameHelper _gameHelper = new GameHelper();
        private List<Ship> _shipList = new List<Ship>();
        private int numberOfGuesses;

        static void Main(string[] args)
        {
            BsGame b = new BsGame();
            b.WelcomeMessage();
            b.CreateShips();
            b.PlayBattleships();

        }


        /// <summary>
        /// Creates the ships and gets a location
        /// </summary>
        public void CreateShips()
        {
            Ship b1 = new Ship("Battleship", 5);
            Ship b2 = new Ship("Battleship", 5);
            Ship d1 = new Ship("Destoyer", 4);


            _shipList.Add(b1);
            _shipList.Add(b2);
            _shipList.Add(d1);

            foreach (Ship s in _shipList)
            {
                int size = s.Size;
                List<string> newLoc = _gameHelper.PlaceShip(size);
                s.Location = newLoc;
            }
        }


        /// <summary>
        /// Contains a loop that gets user input and calls
        /// the game helper to force the case of the input
        /// to upper to match the AlPHA coordinates constant's case
        /// in the GameHelper class.
        /// </summary>
        public void PlayBattleships()
        {
            Console.WriteLine("Let's go!");
            while(_shipList.Count() > 0)
            {
                string guess = Console.ReadLine(); 
                guess = _gameHelper.ForceCase(guess);
                CheckGuess(guess);
            }
            EndGame();
        }

        private void EndGame()
        {
            throw new NotImplementedException();
        }

        private string CheckGuess(string guess)
        {
            numberOfGuesses++;
            string result = "miss";

            foreach (Ship ship in _shipList)
            {
                result = ship.CheckSelf(guess);
                if (result == "hit")
                {
                    break;
                }
                if (result == "kill")
                {
                    _shipList.Remove(ship);
                    break;
                }
            }
            return result;
        }

        public void WelcomeMessage()
        {
            Console.WriteLine("BattleShips - Drew Watson");
            Console.WriteLine("This is a simple one sided game of battleships. Your goal is to sink all ships");
            Console.WriteLine("Type co-ordinated to pick a square ie, A5, B1 etc.");
        }
    }
}

    /// <summary>
    /// Handles the main game loginc off the application.
    /// Creates and places ships on a grid.
    /// </summary>
    public class GameHelper
    {
        private const int MAX_ATTEMPTS = 200;
        private const string ALPHA = "abcdefghij";   // Concat to 0123456789 on the X axis
        private const int GRID_LENGTH = 10;        // The length of a grid row 
        private const int GRID_SIZE = 100;          // The total size of the grid

        private int[] _grid;            // The grid as an array of integers
        private int _shipCount;         // The number of ships


        public GameHelper()
        {
            _grid = new int [GRID_SIZE];
        }

        /// <summary>
        /// Changes the case of the co-ordinates to upper
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ForceCase(string input)
        {
            string inputLine = input;
            return inputLine.ToUpper();
        }

        /// <summary>
        /// Places a ship on the grid
        /// </summary>
        /// <param name="ship"></param>
        /// <returns></returns>
        public List<string> PlaceShip(int size)
        {
            List<string> alphaCells = new List<string>();  // Holds 'A5' style co-ordinates
            string[] alphaCoords = new string[size];
            int[] coords = new int[size];
            int location = 0;   
            int attempts = 0;   // Attempts counter
            string tempConcat;  // Temporary string for concatination
            bool success = false;

            _shipCount++;
            int increment = 1;
            if ((size % 3) == 1)
            {
                increment = GRID_LENGTH;
            }

            Random rnd = new Random();
            while (!success && attempts++ < MAX_ATTEMPTS)
            {
                location = rnd.Next(GRID_SIZE);
                int x = 0;
                success = true;
                while (success && x < size)
                {
                    if (_grid[location] == 0)
                    {
                        coords[x++] = location;
                        location += increment;

                        if (location >= GRID_SIZE)
                        {
                            success = false;
                        }
                        if (x > 0 && (location % GRID_LENGTH == 0)) // Right edge is out of bounds
                        {
                            success = false;
                        }
                        else
                        {
                            success = true;
                        }
                    }
                }
            }
            int i = 0;
            int row = 0;
            int column = 0;
            while (i < size)
            {
                _grid[coords[i]] = 1;
                row = (int)(coords[i] / GRID_LENGTH);
                column = coords[i] % GRID_LENGTH;
                tempConcat = Convert.ToString(ALPHA[column]).ToUpper();
                string stringRow = Convert.ToString(row);
                string result = String.Concat(tempConcat,stringRow);
                alphaCells.Add(result);
                i++;
                Console.WriteLine(result);
            }
            return alphaCells;
        }
    }

    public class Ship
    {
        // Properties
        public string Name { get; set; }
        public int Size { get; set; }
        public List<string> Location { get; set; }

        // Constructor
        public Ship(string name, int size)
        {
            Location = new List<string>();
            this.Name = name;
            this.Size = size;
        }

        // Methods
        public string CheckSelf(string input)
        {
            string result = "Miss";                         
            int index = Location.IndexOf(input); // If not found returns -1
            if (index >= 0)
            {
                Location.Remove(input); // Remove the cell

                if (Location.Count == 0) // If there are no locations left
                {
                    result = "Sink";    
                    Console.WriteLine("BOOM! You sunk a: " + Name); // The ship has been sunk
                }
                else
                {
                    result = "Hit";// Register a hit
                }
            }
            Console.WriteLine(result);
            return result;
        }
    }

编辑::::::::::::::

当添加多艘船时,它似乎陷入无限循环。 多年来一直盯着这个,所以新鲜的眼睛可能会有所帮助:D

        while (success && x < size)
        {
            if (_grid[location] == 0)
            {
                coords[x++] = location;
                location += increment;

                if (location >= GRID_SIZE)
                {
                    success = false;
                }
                if (x > 0 && (location % GRID_LENGTH == 0)) // Right edge is out of bounds
                {
                    success = false;
                }
                else
                {
                    success = true;
                }
            }
        }

如果将代码更改为此,它将起作用。

while (success && x < size)
{
  if (_grid[location] == 0)
  {
    coords[x++] = location;
    location += increment;

    if (location >= GRID_SIZE)
    {
        success = false;
    }
    if (x > 0 && (location % GRID_LENGTH == 0)) // Right edge is out of bounds
    {
        success = false;
    }
    else
    {
        success = true;
    }
  }
  x++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM