The Game Library project is a C# library designed to manage players, their statistics, inventory items, and game mechanics such as treasure chests. The project is structured to provide a clean and maintainable codebase, with clear separation of concerns between different components.
Player.cs: Represents a player in the game. It includes properties likeName,Level,JoinDate, and methods for managing the player's inventory and experience points.PlayerDto.cs: A Data Transfer Object (DTO) for transferring simplified player information.PlayerReportDto.cs: A DTO for generating player reports, including statistics like games played, total score, and average score.PlayerStatistics.cs: Represents a player's statistics, including games played and total score.InventoryItem.cs: Represents an item in the player's inventory, with properties likeId,Name, andDescription.
IPlayerStatisticsService.cs: An interface defining methods for retrieving and updating player statistics.FileBasedPlayerStatisticsService.cs: An implementation ofIPlayerStatisticsServicethat stores and retrieves player statistics from a JSON file.
GameWorld.cs: Manages the game world, including generating player reports and recording game wins.TreasureChest.cs: Represents a treasure chest in the game, with logic to determine if it can be opened based on whether it is locked and if the player has a key.
PlayerExtensions.cs: Contains extension methods for thePlayerclass, such as converting aPlayerobject to aPlayerDto.
- The
Playerclass manages player information, including their level, inventory, and experience points. - Players can increase their level, add items to their inventory, and gain experience points based on task difficulty and time taken.
- The
PlayerStatisticsclass tracks player statistics such as games played and total score. - The
FileBasedPlayerStatisticsServiceclass reads and writes player statistics to a JSON file, allowing for persistent storage.
- The
GameWorldclass provides methods for generating player reports and recording game wins. - The
TreasureChestclass manages the logic for opening treasure chests, depending on whether they are locked and if the player has a key.
The project includes a comprehensive suite of unit tests to ensure the correctness of the code. The tests are written using Xunit, Moq, NSubstitute, and FluentAssertions.
PlayerTests.cs: Tests for thePlayerclass, including level increases, inventory management, and event handling.- Examples:
IncreaseLevel_WhenCalled_HasExpectedLevel: Ensures that the player's level increases correctly.AddItemToInventory_WithValidItem_AddsTheItem: Verifies that items are correctly added to the player's inventory.
PlayerExtensionsTests.cs: Tests for thePlayerExtensionsclass, ensuring that theToDtomethod correctly maps aPlayerobject to aPlayerDto.- Example:
ToDto_WhenCalled_MapsProperties: Verifies that theToDtomethod correctly maps player properties.
GameWorldTests.cs: Tests for theGameWorldclass, including player report generation and recording game wins.- Examples:
GetPlayerReport_PlayerExists_ReturnsExpectedReport: Ensures that the player report is generated correctly.RecordPlayerGameWin_ValidPlayerAndScore_UpdatesPlayerStatistics: Verifies that player statistics are updated correctly after a game win.
GameWorldMoqTests.cs: Similar toGameWorldTests, but uses Moq for mocking dependencies.- Examples:
GetPlayerReport_PlayerExists_ReturnsExpectedReport: Ensures that the player report is generated correctly using Moq.RecordPlayerGameWin_ValidPlayerAndScore_UpdatesPlayerStatistics: Verifies that player statistics are updated correctly using Moq.
TreasureChestTests.cs: Tests for theTreasureChestclass, ensuring that the chest can be opened under the correct conditions.- Examples:
CanOpen_WhenCalled_ReturnsExpectedOutcome: Tests various scenarios for opening a treasure chest based on whether it is locked and if the player has a key.
FakePlayerStatisticsService.cs: A fake implementation ofIPlayerStatisticsServiceused in tests to avoid dependencies on external systems.
To run the tests, use the following command in the terminal:
dotnet testThe Game Library project is a well-structured C# library for managing players, their statistics, and game mechanics. The accompanying unit tests ensure that the code is reliable and maintainable. This project serves as a great example of how to structure a C# project and write comprehensive unit tests.