BattleshipUtility.BattleshipPlayer.Log C# (CSharp) Method

Log() public method

Stores a game log
You can choose to save or process this in your player implementation
public Log ( string s ) : void
s string string to log
return void
        public void Log(string s)
        {
            _log.AppendLine(s);
        }

Usage Example

示例#1
0
        /// <summary>
        /// Main battleship game loop
        /// </summary>
        /// <param name="opponent">Name of opponent</param>
        /// <param name="width">Width of board</param>
        /// <param name="height">Height of board</param>
        /// <param name="ships">Ships in play</param>
        /// <param name="offense">Offensive strategy</param>
        /// <param name="defense">Defensive strategy</param>
        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...");
        }