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); }
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)); }
public RuleViolation?Check(BoardState state, Turn turn) { CheckArg.NotNull(state); CheckArg.NotNull(turn); // TODO: just "everything is correct" yet return(null); }
public void Bind(IPlayer implementation) { CheckArg.NotNull(implementation); if (IsBound) { throw new InvalidOperationException("Adapter is already bound"); } _implementation = implementation; }
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); }
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); } } } }
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)); }
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(); }
// 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)); }
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"); } }
public override void WriteJson(JsonWriter writer, Cell value, JsonSerializer serializer) { CheckArg.NotNull(writer); writer.WriteValue(value.ToString()); }
public TurnCmdHandler(RulesEngine rulesEngine) { RulesEngine = CheckArg.NotNull(rulesEngine); }
public GameFlow(IPlayer playerA, IPlayer playerB, RulesEngine rulesEngine) { PlayerA = CheckArg.NotNull(playerA); PlayerB = CheckArg.NotNull(playerB); RulesEngine = CheckArg.NotNull(rulesEngine); }
public FileStorage(IFileIO fileIO) { FileIO = CheckArg.NotNull(fileIO); Serializer = new GameSerializer(); }
public GameCmdHandler(GameFlow gameFlow, IStorage storage, IConsoleIO consoleIO) { GameFlow = CheckArg.NotNull(gameFlow); Storage = CheckArg.NotNull(storage); Console = CheckArg.NotNull(consoleIO); }
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"))); }
public AiPlayer(RulesEngine rulesEngine) { RulesEngine = CheckArg.NotNull(rulesEngine); }