BattleshipUtility.BattleshipGame.GameLoop C# (CSharp) Метод

GameLoop() публичный Метод

Main battleship game loop
public GameLoop ( string opponent, int width, int height, List ships, BattleshipPlayer player ) : void
opponent string Name of opponent
width int Width of board
height int Height of board
ships List Ships in play
player BattleshipPlayer
Результат void
        public void GameLoop(string opponent, int width, int height, List<Ship> ships, BattleshipPlayer player)
        {
            #region Argument Validation

            if (width < 1)
                throw new ArgumentException("width");

            if (height < 1)
                throw new ArgumentException("height");

            if (ships == null)
                throw new ArgumentNullException("ships");

            if (ships.Count == 0)
                throw new ArgumentException("ships");

            if (player == null)
                throw new ArgumentNullException("player");

            #endregion

            _width = width;
            _height = height;
            _ships = ships;
            _shipCount = ships.Count;
            _player = player;

            // Initialize the command dictionary
            _commands =  new Dictionary<string, Func<string, CommandResult>>()
            {
                { "accept", Accept },
                { "reject", Reject },
                { "fire", Fire },
                { "hit", Hit },
                { "miss", Miss },
                { "incoming", Incoming },
                { "sink", Sink },
                { "win", Win },
                { "loss", Loss },
                { "tie", Tie },
                { "exit", Exit }
            };

            try
            {
                // Initialize the player
                CommandResult lastResult = CommandResult.Continue;

                _player.Initialize(_width, _height, _ships, opponent);

                // Write out initial positions of the ships
                List<Ship> placedShips = _player.Defense.PlaceShips();
                foreach (Ship ship in placedShips)
                {
                    Console.WriteLine(ship.ToString());
                    Console.Out.Flush();
            #if DEBUG
                    _player.Log(">> " + ship.ToString());
            #endif
                }

                // Main game loop
                do
                {
                    // Get incoming command
                    string command = Console.ReadLine();
            #if DEBUG
                    _player.Log("<< " + command);
            #endif

                    if (String.IsNullOrEmpty(command))
                    {
                        Debug.WriteLine("Unexpected input: No data received.");
                        break;
                    }

                    // Split the command up
                    string[] parsedCommand = command.Split(delimiters);

                    if (parsedCommand != null && parsedCommand.Length > 0)
                    {
                        // See if an argument was sent with the command
                        string commandArgument = null;
                        if (parsedCommand.Length > 1)
                        {
                            commandArgument = parsedCommand[1];
                        }

                        // Determine what command was sent
                        Func<string, CommandResult> commandFunction;
                        if (_commands.TryGetValue(parsedCommand[0].ToLower(), out commandFunction))
                        {
                            lastResult = commandFunction(commandArgument);
                        }
                        else
                        {
                            Debug.WriteLine("Invalid command: " + command);
                        }
                    }
                    else
                    {
                        Debug.Write("Failed to parse command, attempting to continue.");
                    }
                } while (lastResult == CommandResult.Continue);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unexpected error occurred.  Exception: " + ex.Message);
                Console.WriteLine("Stack Trace: " + ex.StackTrace);
            #if DEBUG
                player.Log("Unexpected error occurred.  Exception: " + ex.Message);
                player.Log("Stack Trace: " + ex.StackTrace);
            #endif
                player.Cleanup(GameEndState.Loss);
                return;
            }

            Debug.WriteLine("Exiting...");
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Program entry method
        /// </summary>
        /// <param name="args">command line arguments - unused</param>
        static void Main(string[] args)
        {
            string opponent;
            if (args == null || args.Length < 1)
                // No opponent name supplied, create one
                opponent = Guid.NewGuid().ToString();
            else
                opponent = args[0];

            AdaptiveBattleshipPlayer player = new AdaptiveBattleshipPlayer();
            BattleshipGame game = new BattleshipGame();
            List<Ship> ships = new List<Ship>()
            {
                new Ship() { Code = "D", Size = 2 },
                new Ship() { Code = "S", Size = 3 },
                new Ship() { Code = "S", Size = 3 },
                new Ship() { Code = "C", Size = 4 },
                new Ship() { Code = "B", Size = 5 }
            };

            game.GameLoop(opponent, 10, 10, ships, player);
        }