コード例 #1
0
            public override BoardState ReadJson(JsonReader reader, Type objectType, BoardState?existingValue, bool hasExistingValue, JsonSerializer serializer)
            {
                CheckArg.NotNull(reader);
                CheckArg.NotNull(serializer);

                var bs = BoardState.Empty;

                reader.EnsureStartObject();
                while (reader.EnsureRead() != JsonToken.EndObject)
                {
                    var cellStr = reader.EnsureProperty();
                    var cell    = Cell.Parse(cellStr);
                    reader.EnsureRead();
                    reader.EnsureStartObject();
                    reader.EnsureRead();
                    reader.EnsureProperty(nameof(Figure));
                    reader.EnsureRead();
                    var figure = serializer.Deserialize <Figure>(reader);
                    reader.EnsureRead();
                    reader.EnsureProperty(nameof(Color));
                    reader.EnsureRead();
                    var color = serializer.Deserialize <Color>(reader);
                    reader.EnsureRead();
                    reader.EnsureEndObject();

                    bs = bs.With(figure, color, cell);
                }

                return(bs);
            }
コード例 #2
0
        public BoardState Apply(BoardState state, Turn turn)
        {
            CheckArg.NotNull(state);
            CheckArg.NotNull(turn);

            if (turn is not Move move)
            {
                throw new NotSupportedException();
            }

            var(f, c) = state[move.From] ?? throw new UserErrorException($"There are no figure at {move.From}");

            if (move.From == move.To)
            {
                throw new RuleViolationError(new RuleViolation("Figure can't move to the same cell"));
            }

            state = state.Without(move.From);

            if (state[move.To].HasValue)
            {
                state = state.Without(move.To);
            }

            return(state.With(f, c, move.To));
        }
コード例 #3
0
        public RuleViolation?Check(BoardState state, Turn turn)
        {
            CheckArg.NotNull(state);
            CheckArg.NotNull(turn);

            // TODO: just "everything is correct" yet
            return(null);
        }
コード例 #4
0
        public void Bind(IPlayer implementation)
        {
            CheckArg.NotNull(implementation);

            if (IsBound)
            {
                throw new InvalidOperationException("Adapter is already bound");
            }

            _implementation = implementation;
        }
コード例 #5
0
        public ConsoleUI(GameFlow gameFlow, IConsoleIO console, IStorage storage, RulesEngine rulesEngine)
        {
            CheckArg.NotNull(gameFlow);
            CheckArg.NotNull(console);
            CheckArg.NotNull(storage);
            CheckArg.NotNull(rulesEngine);

            GameCmdHandler = new GameCmdHandler(gameFlow, storage, console);
            TurnCmdHandler = new TurnCmdHandler(rulesEngine);
            BoardPrinter   = new BoardPrinter(console, gameFlow);
            CommandParser  = new CommandParser();
            CommandCycle   = new CommandCycle(console, GameCmdHandler, TurnCmdHandler, BoardPrinter, CommandParser);
        }
コード例 #6
0
        public IEnumerable <Turn> GetPossibleTurns(BoardState state, Color playerColor)
        {
            CheckArg.NotNull(state);

            foreach (var(cell, (_, color)) in state.Figures)
            {
                if (color == playerColor)
                {
                    foreach (var t in GetPossibleTurns(state, cell))
                    {
                        yield return(t);
                    }
                }
            }
        }
コード例 #7
0
            public override Turn ReadJson(JsonReader reader, Type objectType, Turn?existingValue, bool hasExistingValue, JsonSerializer serializer)
            {
                CheckArg.NotNull(reader);
                CheckArg.NotNull(serializer);

                reader.EnsureStartObject();
                reader.EnsureRead();
                reader.EnsureProperty(nameof(Move.From));
                var from = serializer.Deserialize <Cell>(reader);

                reader.EnsureRead();
                reader.EnsureProperty(nameof(Move.To));
                var to = serializer.Deserialize <Cell>(reader);

                reader.EnsureRead();
                reader.EnsureEndObject();

                return(new Move(from, to));
            }
コード例 #8
0
            public override void WriteJson(JsonWriter writer, BoardState?value, JsonSerializer serializer)
            {
                CheckArg.NotNull(writer);
                CheckArg.NotNull(value);
                CheckArg.NotNull(serializer);

                writer.WriteStartObject();
                foreach (var(cell, (figure, color)) in value.Figures)
                {
                    writer.WritePropertyName(cell.ToString());
                    writer.WriteStartObject();
                    writer.WritePropertyName(nameof(Figure));
                    serializer.Serialize(writer, figure);
                    writer.WritePropertyName(nameof(Color));
                    serializer.Serialize(writer, color);
                    writer.WriteEndObject();
                }
                writer.WriteEndObject();
            }
コード例 #9
0
        // TODO: SRE violation and logic duplication with CommandParser! CellParser should be extracted as well as Move/Turn Parser
        public static Cell Parse(string s)
        {
            CheckArg.NotNull(s);

            if (s.Length != 2)
            {
                throw new ArgumentOutOfRangeException(nameof(s));
            }

            char h = s[0];
            int  v = int.Parse(s[1].ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture);

            if (!IsValid(h, v))
            {
                throw new ArgumentOutOfRangeException(nameof(s), s, "Invalid cell value");
            }

            return(At(h, v));
        }
コード例 #10
0
            public override void WriteJson(JsonWriter writer, Turn?value, JsonSerializer serializer)
            {
                CheckArg.NotNull(writer);
                CheckArg.NotNull(value);
                CheckArg.NotNull(serializer);

                if (value is Move move)
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(nameof(Move.From));
                    serializer.Serialize(writer, move.From);
                    writer.WritePropertyName(nameof(Move.To));
                    serializer.Serialize(writer, move.To);
                    writer.WriteEndObject();
                }
                else
                {
                    throw new JsonSerializationException("Unsupported Turn kind");
                }
            }
コード例 #11
0
 public override void WriteJson(JsonWriter writer, Cell value, JsonSerializer serializer)
 {
     CheckArg.NotNull(writer);
     writer.WriteValue(value.ToString());
 }
コード例 #12
0
 public TurnCmdHandler(RulesEngine rulesEngine)
 {
     RulesEngine = CheckArg.NotNull(rulesEngine);
 }
コード例 #13
0
ファイル: GameFlow.cs プロジェクト: iamhere2/HLCD-Researches
 public GameFlow(IPlayer playerA, IPlayer playerB, RulesEngine rulesEngine)
 {
     PlayerA     = CheckArg.NotNull(playerA);
     PlayerB     = CheckArg.NotNull(playerB);
     RulesEngine = CheckArg.NotNull(rulesEngine);
 }
コード例 #14
0
 public FileStorage(IFileIO fileIO)
 {
     FileIO     = CheckArg.NotNull(fileIO);
     Serializer = new GameSerializer();
 }
コード例 #15
0
 public GameCmdHandler(GameFlow gameFlow, IStorage storage, IConsoleIO consoleIO)
 {
     GameFlow = CheckArg.NotNull(gameFlow);
     Storage  = CheckArg.NotNull(storage);
     Console  = CheckArg.NotNull(consoleIO);
 }
コード例 #16
0
 public override Cell ReadJson(JsonReader reader, Type objectType, Cell existingValue, bool hasExistingValue, JsonSerializer serializer)
 {
     CheckArg.NotNull(reader);
     return(Cell.Parse(reader.ReadAsString() ?? throw new SerializationException("cell string expected")));
 }
コード例 #17
0
ファイル: AiPlayer.cs プロジェクト: iamhere2/HLCD-Researches
 public AiPlayer(RulesEngine rulesEngine)
 {
     RulesEngine = CheckArg.NotNull(rulesEngine);
 }