Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GameState more read-only #30

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions BinaryMatrixEngine/CardList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ public CardList(int initialCapacity) {
this.cards = listPool.Rent(initialCapacity);
}

private static (Card[] storage, int count) CreateListFromCollection(ICollection<Card> cardList) {
Card[] storage = listPool.Rent(cardList.Count);
cardList.CopyTo(storage, 0);
return (storage, cardList.Count);
}

public CardList(IEnumerable<Card> cards) {
if(cards is CardList list) {
this.cards = listPool.Rent(list.Count);
this.AddAll(list);
return;
}

Card[] theCards = cards.ToArray();
this.cards = listPool.Rent(theCards.Length);
Array.Copy(theCards, this.cards, theCards.Length);
this.Count = theCards.Length;
(this.cards, this.Count) = CreateListFromCollection(cards as ICollection<Card> ?? cards.ToArray());
if(FAIL_FAST_INVALID) {
foreach(ref Card card in this) {
if(card.IsInvalid)
Expand All @@ -54,9 +57,7 @@ public CardList(IEnumerable<Card> cards) {
}

public CardList(IList<Card> cards) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be weakened to ICollection<Card> (possibly outside this branch) to allow ImmutableList<Card>-to-CardList conversions.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Figure out why we didn't do this earlier. CreateListFromCollection is basically the constructor minus the FAIL_FAST_INVALID checks.

this.cards = listPool.Rent(cards.Count);
cards.CopyTo(this.cards, 0);
this.Count = this.cards.Length;
(this.cards, this.Count) = CreateListFromCollection(cards);
if(FAIL_FAST_INVALID) {
foreach(ref Card card in this) {
if(card.IsInvalid)
Expand Down
15 changes: 14 additions & 1 deletion BinaryMatrixEngine/GameBoard.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using Fayti1703.CommonLib.Enumeration;
using JetBrains.Annotations;

Expand Down Expand Up @@ -26,6 +27,11 @@ internal Cell(CellName name) {
});
}

internal Cell(CellName name, IEnumerable<Card> cards) {
this.name = name;
this.cards = new CardList(cards);
}

public bool Revealed {
get => this.name switch {
>= X0 and <= X5 => true,
Expand Down Expand Up @@ -84,7 +90,12 @@ public sealed class GameBoard : IDisposable {
private readonly IReadOnlyList<Cell> cells;

public GameBoard() {
this.cells = Enum.GetValues<CellName>().Select(x => new Cell(x)).ToList();
this.cells = Enum.GetValues<CellName>().Select(x => new Cell(x)).ToImmutableList();
}

public GameBoard(IReadOnlyCollection<IEnumerable<Card>> stacks) {
if(stacks.Count != Enum.GetValues<CellName>().Length) throw new ArgumentException("Invalid number of stacks", nameof(stacks));
this.cells = stacks.Select((x, i) => new Cell((CellName) i, x)).ToImmutableList();
}

public Cell GetCell(CellName name) {
Expand Down Expand Up @@ -115,6 +126,8 @@ public GameBoard Copy() {
return newBoard;
}

public IReadOnlyList<IEnumerable<Card>> GetStacks() => this.cells.Select(x => x.cards).ToImmutableList();

public void Dispose() {
foreach(Cell cell in this.cells) {
cell.Dispose();
Expand Down
8 changes: 4 additions & 4 deletions BinaryMatrixEngine/GameContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace BinaryMatrix.Engine;

public readonly struct GameState {
public readonly int turnCounter;
public readonly GameBoard board;
public readonly IReadOnlyList<IEnumerable<Card>> board;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an IReadOnlyList<Card> instead. Question is whether serialization overhead from CardList <-> ImmutableList<Card> is going to be a problem (I'd hope no).

public readonly IReadOnlyList<PlayerData> players;
public readonly PlayerRole? victor;
public readonly IReadOnlyList<TurnLog> binlog;

public GameState(
int turnCounter,
IReadOnlyList<PlayerData> players,
GameBoard board,
IReadOnlyList<IEnumerable<Card>> board,
PlayerRole? victor,
IReadOnlyList<TurnLog> binlog
) {
Expand Down Expand Up @@ -75,7 +75,7 @@ public GameContext(IEnumerable<Player> players, RNG rng, GameHooks hooks)
: this(players, rng, hooks, new GameBoard(), new List<TurnLog>()) { }

public GameContext(GameState state, IReadOnlyDictionary<PlayerID, PlayerActor> actors, RNG rng, GameHooks hooks)
: this(state.CreatePlayers(actors), rng, hooks, state.board.Copy(), new List<TurnLog>(state.binlog)) {
: this(state.CreatePlayers(actors), rng, hooks, new GameBoard(state.board), new List<TurnLog>(state.binlog)) {
this.TurnCounter = state.turnCounter;
this.Victor = state.victor;
}
Expand All @@ -98,7 +98,7 @@ public GameState SaveState() {
return new GameState(
this.TurnCounter,
this.Players.Select(x => x.data.Copy()).ToImmutableList(),
this.board.Copy(),
this.board.GetStacks(),
this.Victor,
this.binlog.ToImmutableList()
);
Expand Down