コード例 #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, string name = "no name", string AIType = "dumb", int n = 1)
        {
            JsonValidation.ValidateJTokenAIPlayer(jtoken);

            switch (jtoken.ElementAt(0).ToObject <string>())
            {
            case "register":
                if (AIType == "less dumb")
                {
                    _player = new AIPlayer(name, AIType, n);
                }
                else
                {
                    _player = new AIPlayer(name, AIType);
                }
                return(JToken.Parse(JsonConvert.SerializeObject(name)));

            case "receive-stones":
                JsonValidation.ValidateStone(jtoken.ElementAt(1));
                _player.ReceiveStones(jtoken.ElementAt(1).ToObject <string>());
                return(JToken.Parse(JsonConvert.SerializeObject(null)));

            case "make-a-move":
                JsonValidation.ValidateBoards(jtoken.ElementAt(1));
                try
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(_player.MakeAMove(
                                                                        jtoken.ElementAt(1).ToObject <string[][][]>()))));
                }
                catch (AIPlayerException e)
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(e.Message)));
                }
            }

            throw new InvalidJsonInputException("Unrecognized JSONCommand passed to AIPlayerWrapper");
        }