Gyuri's Blog

TrebleCross: C# Game

2024-04-18

TrebleCross: C# Game

A deep dive into building a strategic game engine using C# and .NET 8, featuring advanced design patterns and strategic gameplay logic.

🎮 Advanced Game Engine: TrebleCross & Reversi Implementation

This project is a sophisticated console-based game engine built with .NET 8. It supports TrebleCross, a linear single-row strategy game, implementing a reusable architecture through abstract base classes and robust design patterns.

🔗 Project Links

📸 System Previews

Gameplay SessionIn-Game Help System
Gameplay ScreenshotHelp System Screenshot

🛠 Architecture & Design Patterns

The project is built on the Single Responsibility Principle, ensuring each class (Board, Player, Game) has a distinct role within the system.

1. Class Hierarchy & Commonalities

I extracted core logic into an abstract Game base class to support both TrebleCross and Reversi efficiently. The player system is built on a Player superclass with specialized HumanPlayer and ComputerPlayer implementations to maximize code reusability.

2. Applied Design Patterns

  • Factory Method Pattern: Implemented in CreatePlayers, CreateBoard, and CreateHelpSys to encapsulate object creation.
  • Template Method Pattern: The Game class defines the high-level algorithm, while subclasses like TrebleCrossGame provide specific logic for Move() and InitializeGame().
  • Command & Strategy Patterns: Encapsulates player commands (Place, Undo, Redo) and allows flexible switching between play modes (Human vs. Human or Human vs. Computer).

3. Object & Sequence Diagrams

The following diagrams illustrate state transitions and the Undo/Redo logic. The Undo/Redo functionality specifically reverts two moves at once (the current player and the opponent) to maintain a consistent turn-based flow.

Class Diagram Screenshot

Gameplay Screenshot


🚀 Key Features

  • Undo/Redo Functionality: Uses a Stack-based structure to revert or reapply the last two moves (self and opponent) simultaneously.
  • Save/Restore: Utilizes System.Text.Json to serialize the game state into a JSON file, allowing players to resume games at any time.
  • Computer AI: Features an automated player that calculates valid positions and places moves using a random selection algorithm.
  • Real-time Validation: For human players, the system identifies valid move ranges and positions before every turn to ensure game integrity.

📈 Performance & Big O Analysis

The system is optimized for quick response times and efficient state management.

FeaturePattern/AlgorithmComplexityAchievement Justification
Move ValidationSearch AlgorithmO(n)O(n)Scans the board to identify all valid indices for the next move.
State PersistenceJSON SerializationO(n)O(n)Serializes the GameState and MoveHistory linearly based on total moves.
Undo/RedoStack OperationsO(1)O(1)Employs moveHistory and redoStack for instantaneous state recovery.

🎮 How to Get Started

System Requirements

  • .NET SDK 8.0 or newer

Execution

# Clone the repository git clone [https://github.com/gyuripk/treblecross-game-csharp](https://github.com/gyuripk/treblecross-game-csharp) # Run the application dotnet run
Data Exploration

Data Exploration

Advanced EDA and strategic preprocessing on a global medical dataset to optimize predictive risk classification.

Mobile ML: Fungi Classifier

Mobile ML: Fungi Classifier

87.5% accuracy classifier using DINOv2, optimized for CPU with <200ms latency.

Gyuri's Blog | All Right Reserved.