예제 #1
0
        /// <summary>
        /// Gets a move from its CAN (coordinate algebraic notation).
        /// Throws ArgumentException if it's not a valid move.
        /// </summary>
        /// <param name="game">The game</param>
        /// <param name="can">The CAN string</param>
        /// <returns></returns>
        public static Move GetCANMove(Game game, string can)
        {
            // the game must not be null
            if (game == null)
            {
                throw new ArgumentNullException("game", Resources.NullGameMsg);
            }

            // the CAN string must not be null
            if (can == null)
            {
                throw new ArgumentNullException("can", Resources.NullCANMsg);
            }

            // check if the string is well-formed
            if (can.Length < 4 || can[0] < 'a' || can[0] > 'h' || can[1] < '1' || can[1] > '8' || can[2] < 'a' || can[2] > 'h' || can[3] < '1' || can[3] > '8')
            {
                throw new ArgumentException(Resources.IllegalCANFormatMsg, "can");
            }

            // get the starting square
            int from = GetPosition(can[0], can[1]);

            // get the ending square
            int to = GetPosition(can[2], can[3]);

            // get the promotion
            Type promotionType = (can.Length > 4) ? GetPromotionType(game.CurrentBoard.Status.WhiteTurn, can[4]) : null;

            // look into the possible moves to find it
            Move move = game.GetMove(from, to, promotionType);

            if ((move == null) || (move is PromotionMove && (move as PromotionMove).PromotionType == null))
            {
                throw new ArgumentException(Resources.IllegalCANMoveMsg, "can");
            }

            return(move);
        }
예제 #2
0
        /// <summary>
        /// Gets a move from its CAN (coordinate algebraic notation).
        /// Throws ArgumentException if it's not a valid move.
        /// </summary>
        /// <param name="game">The game</param>
        /// <param name="can">The CAN string</param>
        /// <returns></returns>
        public static Move GetCANMove(Game game, string can)
        {
            // the game must not be null
            if (game == null) { throw new ArgumentNullException("game", "Resources.NullGameMsg"); }

            // the CAN string must not be null
            if (can == null) { throw new ArgumentNullException("can", "Resources.NullCANMsg"); }

            // check if the string is well-formed
            if (can.Length < 4 || can[0] < 'a' || can[0] > 'h' || can[1] < '1' || can[1] > '8' || can[2] < 'a' || can[2] > 'h' || can[3] < '1' || can[3] > '8')
            {
                throw new ArgumentException("Resources.IllegalCANFormatMsg", "can");
            }

            // get the starting square
            int from = GetPosition(can[0], can[1]);

            // get the ending square
            int to = GetPosition(can[2], can[3]);

            // get the promotion
            Type promotionType = (can.Length > 4) ? GetPromotionType(game.CurrentBoard.Status.WhiteTurn, can[4]) : null;

            // look into the possible moves to find it
            Move move = game.GetMove(from, to, promotionType);

            if ((move == null) || (move is PromotionMove && (move as PromotionMove).PromotionType == null))
            {
                throw new ArgumentException("Resources.IllegalCANMoveMsg", "can");
            }

            return move;
        }