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

Conversation

Fayti1703
Copy link
Owner

Currently, you can inadvertently modify a GameState by messing with its GameBoard. GameState is meant to be an immutable snapshot of a game state, so this is problematic.


Includes some other refactoring that may be pulled in separately.

@Fayti1703 Fayti1703 added correctness incorrect behavior z-project: engine something in the engine itself labels Jan 7, 2025
@@ -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).

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
correctness incorrect behavior z-project: engine something in the engine itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant