1 unstable release
Uses new Rust 2024
| 0.1.0 | Jan 7, 2026 |
|---|
#91 in Games
205KB
2K
SLoC
cliard24 ๐
The project name is a combination of CLI (Command Line Interface), Card, and 24 (game objective), indicating a 24-point game played through the command line.
๐ฏ Project Introduction
cliard24 is a 24-point card game implemented in Rust that runs in the command line interface. It offers two modes:
- ๐ฒ Game Mode: Play the classic 24-point game interactively in the command line
- ๐ Solver Mode: Compute all possible expressions for any combination of numbers to reach a target value
๐ What is the 24-point game?
The 24-point game is a classic mathematical puzzle where players use addition, subtraction, multiplication, and division to combine the values of 4 playing cards to make 24.
๐ Quick Start
๐ฆ Installation Methods
Method 1: crates.io (Recommended)
cargo install cliard24
After successful installation, the executable will be placed in Cargo's binary directory (typically ~/.cargo/bin/)
Method 2: Source Compilation
git clone https://github.com/Expien1/cliard24.git
cd cliard24
cargo build --release
After successful compilation, the executable is located at ./target/release/cliard24
๐ฎ Game Mode Usage Examples
# Start interactive game (using default parameters)
cliard24
# Explicitly specify game mode
cliard24 play
# Endless mode (unlimited rounds, suitable for practice)
cliard24 play --endless
# Customize attempt limit and solution display limit
cliard24 play --max-attempts 5 --limit 3
Parameter Description:
--max-attemptsor-a: Maximum attempts per round (default: 3, 0 means unlimited)--limitor-l: Limit on the number of solutions displayed (default: 1, 0 means display all)--endlessor-e: Enable endless mode (default: false)
๐ Solver Mode Usage Examples
# Solve classic 24-point puzzle (3,3,8,8)
cliard24 solve 3 3 8 8
# Custom target value (find expressions for 1,2,3,4 to reach 10)
cliard24 solve 1 2 3 4 --target 10
# Limit displayed solution count (avoid excessive output)
cliard24 solve 1 2 3 4 5 --limit 5
Parameter Description:
- Number list: At least 2 numbers, separated by spaces, supports decimals (โ ๏ธ Negative numbers not currently supported)
--targetor-t: Target value (default: 24)--limitor-l: Limit on the number of solutions displayed (default: 0, display all)
๐ Game Rules and Operations
Classic 24-Point Game Rules
- Start: Randomly draw 4 playing cards
- Card Values: A=1, J=11, Q=12, K=13, other cards use their face value
- Operators: Only addition (
+), subtraction (-), multiplication (*), and division (/) are allowed - Usage Restriction: Each card must be used exactly once
- Parentheses: Arbitrary levels of parentheses nesting are supported
- Win Condition: The expression evaluates to 24 (or the specified target value in solver mode)
Expression Input Formats
In game mode, the following expression types are supported:
| Type | Example | Description |
|---|---|---|
| Numbers Only | (3+3)*(2+2) |
Direct number input |
| Card Letters | (K-j)*Q*a |
Use card letters (case-insensitive) |
| Mixed Use | (13-j)*12*A |
Numbers + letters |
Game Interaction Commands
In game mode, in addition to entering expressions, the following commands are available:
| Command | Short | Function Description |
|---|---|---|
help |
h |
Display game rules and command help |
nosol |
n |
Declare no solution for current hand (system will verify correctness) |
stats |
s |
Display current game statistics |
pass |
p |
Skip current hand (surrender) |
quit |
q |
Exit game |
๐ผ๏ธ Game Interface Examples
๐๏ธ Technical Implementation
Project Features
- Complete expression parsing, evaluation, and validation implementation
- Uses only 3 third-party libraries (
clap,rand,thiserror) - Modular design with 3 modules: expression tree, playing cards, and TUI
Project Architecture
cliard24/
โโโ src/
โ โโโ main.rs # Command line entry, parameter parsing and dispatch, depends on clap
โ โโโ expression/
โ โ โโโ mod.rs # Expression tree module exports
โ โ โโโ ast.rs # Expression tree data structure and basic operations (including evaluation)
โ โ โโโ parser.rs # Expression string parser
โ โ โโโ solver.rs # Expression solver (exhaustive search)
โ โ โโโ error.rs # Expression tree error type definitions, depends on thiserror
โ โโโ playing_cards/
โ โ โโโ mod.rs # Playing card simulation module exports
โ โ โโโ card.rs # Playing card data structure representation and ASCII rendering
โ โ โโโ generator.rs # Deck management and random card drawing, depends on rand
โ โโโ tui/
โ โโโ mod.rs # TUI interaction module exports
โ โโโ game_mode.rs # Interactive game main loop
โ โโโ solve_mode.rs # Solver mode input processing
โ โโโ error.rs # Game error type definitions, depends on thiserror
โโโ Cargo.toml # Project configuration and dependency declaration
Module Dependencies
โ ๏ธ Notes and Explanations
๐ฌ About AI-Generated Comments
After completing the code development for this project, I used AI to check code readability and generate English comments. The AI helped optimize some variable names, function names, and added detailed English comments. If you want to view the original version of the code comments, you can check the Git history.
I personally believe there are many benefits to using AI to help review code readability and write comments. First, it increases writing efficiency; second, AI's comment style and terminology are more consistent; and I found that the quality of comments written by AI is higher than my handwritten onesโit helps me express my design intentions more clearly.
Although AI-generated comments are useful, it's important to be cautious about AI-generated content. For complex or incorrect logic, AI might misunderstand or further deepen errors, so complex logic code and the generated comments still require manual review.
๐ Project Nature Statement
This project is a personal learning project, and my implementation represents just one feasible approach.
- There is still much room for optimization (algorithms, performance, code structure, etc.)
- Please do not regard it as the "standard answer" or "best practice"
- Suggestions for improvement are welcome
๐ English Translation Note
- This English version of the README was translated from Chinese by AI.
- The AI comment prompts I used are not included in the English version; they are placed in the Chinese README.
๐ Acknowledgments
- Thanks to the inventor of the 24-point game, who provided the core design inspiration for this project and allowed me to experience the joy of problem-solving in my childhood.
- Thanks to the Rust language and its ecosystem for providing a stable and efficient toolchain for this project's implementation, as well as the maintainers of the open-source libraries this project depends on (clap, rand, thiserror).
- Thanks to the contributors and researchers of the relevant algorithms used in this project (expression parsing, expression tree construction, backtracking search, etc.).
Dependencies
~1.3โ2MB
~35K SLoC