コード例 #1
0
        public static JToken JsonCommand(JToken jtoken)
        {
            JsonValidation.ValidateJTokenRuleChecker(jtoken);

            //If object is a Board...
            try //Try validating board, then Score
            {
                JsonValidation.ValidateBoard(jtoken);
                return(JToken.Parse(RuleChecker.Score(jtoken.ToObject <string[][]>()).ToString()));
            }
            catch //figure out why board is invalid if this is a board
            {
                if (jtoken.Count() > 2)
                {
                    JsonValidation.ValidateBoard(jtoken);
                }
            }

            JsonValidation.ValidateStone(jtoken.ElementAt(0));
            JsonValidation.ValidatePass(jtoken.ElementAt(1));
            //If object is [Stone, Move] and Move is "pass"
            try
            {
                if (jtoken.ElementAt(1).ToObject <string>() == "pass") //Pass this to RuleChecker
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(
                                            RuleChecker.Pass())));
                }
            }
            catch { }

            //If object is [Stone, Move] and Move is a Play
            JsonValidation.ValidatePlay(jtoken.ElementAt(1));
            JsonValidation.ValidatePoint(jtoken.ElementAt(1).ElementAt(0));
            JsonValidation.ValidateBoards(jtoken.ElementAt(1).ElementAt(1));
            try
            {
                return(JToken.Parse(JsonConvert.SerializeObject(
                                        RuleChecker.Play(
                                            jtoken.ElementAt(0).ToObject <string>(),
                                            jtoken.ElementAt(1).ElementAt(0).ToObject <string>(),
                                            jtoken.ElementAt(1).ElementAt(1).ToObject <string[][][]>()))));
            }
            catch
            {
                return(JToken.Parse(JsonConvert.SerializeObject(false)));
            }

            throw new InvalidJsonInputException("Unrecognized JSONCommand passed to RuleCheckerWrapper");
        }
コード例 #2
0
        public JToken JsonCommand(JToken jtoken)
        {
            JToken statement;
            Board  boardObject;

            JsonValidation.ValidateJTokenBoard(jtoken);
            JsonValidation.ValidateBoard(jtoken.ElementAt(0));
            boardObject = new Board(jtoken.ElementAt(0).ToObject <string[][]>());
            statement   = jtoken.ElementAt(1);

            switch (statement.ElementAt(0).ToObject <string>())
            {
            case "occupied?":
                JsonValidation.ValidateStatementElements(statement, 1);
                JsonValidation.ValidatePoint(statement.ElementAt(1));
                return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Occupied(
                                                                    statement.ElementAt(1).ToObject <string>()))));

            case "occupies?":
                JsonValidation.ValidateStatementElements(statement, 2);
                JsonValidation.ValidateStone(statement.ElementAt(1));
                JsonValidation.ValidatePoint(statement.ElementAt(2));
                return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Occupies(
                                                                    statement.ElementAt(1).ToObject <string>(),
                                                                    statement.ElementAt(2).ToObject <string>()))));

            case "reachable?":
                JsonValidation.ValidateStatementElements(statement, 2);
                JsonValidation.ValidatePoint(statement.ElementAt(1));
                JsonValidation.ValidateMaybeStone(statement.ElementAt(2));
                return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Reachable(
                                                                    statement.ElementAt(1).ToObject <string>(),
                                                                    statement.ElementAt(2).ToObject <string>()))));

            case "place":
                JsonValidation.ValidateStatementElements(statement, 2);
                JsonValidation.ValidateStone(statement.ElementAt(1));
                JsonValidation.ValidatePoint(statement.ElementAt(2));
                try     //PlaceStone may return an exception
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(boardObject.PlaceStone(
                                                                        statement.ElementAt(1).ToObject <string>(),
                                                                        statement.ElementAt(2).ToObject <string>()))));
                }
                catch (BoardException)
                {
                    return(JToken.Parse(JsonConvert.SerializeObject("This seat is taken!")));
                }

            case "remove":
                JsonValidation.ValidateStatementElements(statement, 2);
                JsonValidation.ValidateStone(statement.ElementAt(1));
                JsonValidation.ValidatePoint(statement.ElementAt(2));
                try
                {     //RemoveStone may return an exception
                    return(JToken.Parse(JsonConvert.SerializeObject(boardObject.RemoveStone(
                                                                        statement.ElementAt(1).ToObject <string>(),
                                                                        statement.ElementAt(2).ToObject <string>()))));
                }
                catch (BoardException)
                {
                    return(JToken.Parse(JsonConvert.SerializeObject("I am just a board! I cannot remove what is not there!")));
                }

            case "get-points":
                JsonValidation.ValidateStatementElements(statement, 1);
                JsonValidation.ValidateMaybeStone(statement.ElementAt(1));
                return(JToken.Parse(JsonConvert.SerializeObject(boardObject.GetPoints(
                                                                    statement.ElementAt(1).ToObject <string>()))));
            }

            throw new InvalidJsonInputException("Unrecognized query/command passed to BoardWrapper");
        }