示例#1
0
        private async Task <dynamic> GameInfo(dynamic args, CancellationToken cancellation_token)
        {
            string             game_id = Request.Query["gameId"];
            QuantumChessEngine engine  = await WebAppManagers.DatabaseManager.RequestEngine(game_id);

            var response = new Model.InfoResponse();

            response.ActivePlayer = PlayerUtils.ToString(engine.ActivePlayer);
            response.GameState    = GameStateUtils.ToString(engine.GameState);
            response.Squares      = new Dictionary <String, Model.InfoResponse.SquareEncoded>();
            for (int i = 0; i < 64; i++)
            {
                Position     pos    = Position.FromIndex(i);
                QuantumPiece qpiece = engine.QuantumChessboard.GetQuantumPiece(pos);
                if (qpiece.Piece.HasValue)
                {
                    response.Squares[pos.ToString()] = new Model.InfoResponse.SquareEncoded()
                    {
                        Player      = PlayerUtils.ToString(qpiece.Piece.Value.Player),
                        Piece       = PieceTypeUtils.ToString(qpiece.Piece.Value.PieceType),
                        Probability = qpiece.Probability
                    };
                }
                else
                {
                    response.Squares[pos.ToString()] = null;
                }
            }

            response.LastMovePositions = engine.LastMovePositions.Select((pos) => pos.ToString().ToLower()).ToArray();

            return(Response.AsJson(response));
        }
示例#2
0
        public static QuantumChessMove ReadMove(QuantumChessEngine engine)
        {
            switch (engine.ActivePlayer)
            {
            case Player.White:
                Console.Write("white> ");
                break;

            case Player.Black:
                Console.Write("black> ");
                break;
            }

            string move_str = Console.ReadLine();

            string capitulate_move_regex   = @"^(quit|exit|capitulate)$";
            string agree_to_tie_move_regex = @"^tie$";
            string ordinary_move_regex     = @"^([A-Za-z][1-8])\s*([A-Za-z][1-8])$";
            string quantum_move_regex      = @"^(?:q|Q|quantum)\s+([A-Za-z][1-8])\s*((?:[A-Za-z][1-8])?)\s*([A-Za-z][1-8])$";
            string castle_move_regex       = @"^castle (left|right)$";

            Match ordinary_match = Regex.Match(move_str, ordinary_move_regex);
            Match quantum_match  = Regex.Match(move_str, quantum_move_regex);
            Match castle_match   = Regex.Match(move_str, castle_move_regex);

            if (Regex.IsMatch(move_str, capitulate_move_regex))
            {
                return(new CapitulateMove(engine.ActivePlayer));
            }
            else if (Regex.IsMatch(move_str, agree_to_tie_move_regex))
            {
                return(new AgreeToTieMove(engine.ActivePlayer));
            }
            else if (ordinary_match.Success)
            {
                Position     source = Position.Parse(ordinary_match.Groups[1].Captures[0].Value);
                Position     target = Position.Parse(ordinary_match.Groups[2].Captures[0].Value);
                QuantumPiece qpiece = engine.QuantumChessboard.GetQuantumPiece(source);
                if (qpiece.Piece.HasValue)
                {
                    return(new OrdinaryMove(qpiece.Piece.Value, source, target));
                }
                else
                {
                    throw new MoveParseException($"No piece found at {source}");
                }
            }
            else if (quantum_match.Success)
            {
                Position source = Position.Parse(quantum_match.Groups[1].Captures[0].Value);
                Position?middle = null;
                if (quantum_match.Groups[2].Captures[0].Length > 0)
                {
                    middle = Position.Parse(quantum_match.Groups[2].Captures[0].Value);
                }
                Position     target = Position.Parse(quantum_match.Groups[3].Captures[0].Value);
                QuantumPiece qpiece = engine.QuantumChessboard.GetQuantumPiece(source);
                if (qpiece.Piece.HasValue)
                {
                    return(new QuantumMove(qpiece.Piece.Value, source, middle, target));
                }
                else
                {
                    throw new MoveParseException($"No piece found at {source}");
                }
            }
            else if (castle_match.Success)
            {
                string     castle_type_str = castle_match.Groups[1].Captures[0].Value;
                CastleType castle_type;
                if (castle_type_str == "left")
                {
                    castle_type = CastleType.Left;
                }
                else if (castle_type_str == "right")
                {
                    castle_type = CastleType.Right;
                }
                else
                {
                    throw new MoveParseException($"Unsupported castle type: {castle_type_str}");
                }
                return(new CastleMove(engine.ActivePlayer, castle_type));
            }
            else
            {
                throw new MoveParseException("Unable to parse move");
            }
        }