Ejemplo n.º 1
0
        /// <summary>
        /// Parses a string and returns its corresponding <see cref="ICommand"/>.
        /// </summary>
        /// <param name="input">The string to parse.</param>
        /// <returns>The parsed command.</returns>
        public virtual ICommand ParseCommand(string input)
        { 
            input = input.Trim();

            ICommand command;
            if (this.commands.TryGetValue(input, out command))
            {
                return command;
            }

            bool toggleFlag = false;
            if (input.StartsWith("m"))
            {
                // remove the "m"
                input = input.Substring(1);
                toggleFlag = true;
            }

            // Extract row and col
            var tokens = input.Split(' ');
            tokens = tokens.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            if (tokens.Length != 2)
            {
                return this.commands["invalid"];
            }

            CellPos targetCell = CellPos.Empty;
            int parseCommandInteger;

            if (int.TryParse(tokens[0], out parseCommandInteger))
            {
                targetCell.Row = parseCommandInteger;
            }
            else
            {
                return this.commands["invalid"];
            }

            if (int.TryParse(tokens[1], out parseCommandInteger))
            {
                targetCell.Col = parseCommandInteger;
            }
            else
            {
                return this.commands["invalid"];
            }

            // Parsing was successful, the parsed integers are assigned to targetCell
            if (toggleFlag)
            {
                command = new CmdFlagCell(this.Game, targetCell);
            }
            else
            {
                command = new CmdOpenCell(this.Game, targetCell);
            }
            
            return command;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses a string and returns its corresponding <see cref="ICommand"/>.
        /// </summary>
        /// <param name="input">The string to parse.</param>
        /// <returns>The parsed command.</returns>
        public virtual ICommand ParseCommand(string input)
        {
            input = input.Trim();

            ICommand command;

            if (this.commands.TryGetValue(input, out command))
            {
                return(command);
            }

            bool toggleFlag = false;

            if (input.StartsWith("m"))
            {
                // remove the "m"
                input      = input.Substring(1);
                toggleFlag = true;
            }

            // Extract row and col
            var tokens = input.Split(' ');

            tokens = tokens.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            if (tokens.Length != 2)
            {
                return(this.commands["invalid"]);
            }

            CellPos targetCell = CellPos.Empty;
            int     parseCommandInteger;

            if (int.TryParse(tokens[0], out parseCommandInteger))
            {
                targetCell.Row = parseCommandInteger;
            }
            else
            {
                return(this.commands["invalid"]);
            }

            if (int.TryParse(tokens[1], out parseCommandInteger))
            {
                targetCell.Col = parseCommandInteger;
            }
            else
            {
                return(this.commands["invalid"]);
            }

            // Parsing was successful, the parsed integers are assigned to targetCell
            if (toggleFlag)
            {
                command = new CmdFlagCell(this.Game, targetCell);
            }
            else
            {
                command = new CmdOpenCell(this.Game, targetCell);
            }

            return(command);
        }
Ejemplo n.º 3
0
 public void Test_CmdFlagCell_CtorWithNullThrowsEx()
 {
     var cmd = new CmdFlagCell(null, CellPos.Empty);
 }