Skip to content

tessereth/kalaha-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kalaha in Rust

Because why not?

Usage

To run the demo program:

cargo run

The main entry point to the library is kalaha::Kalaha. To create a kalaha game object:

use kalaha::Kalaha;

let mut game = Kalaha::new();

To make a single move in the game (moves are between 0 and 5 inclusive):

game.choose(3);

You can also create an AI to play the game for you. AI objects implement the kalaha::ai::AI trait, which must return a valid move for the current player.

pub trait AI {
    fn choose(&self, game: &Kalaha) -> usize;
}

The following methods are available on the game object to help you choose the best move.

pub fn valid_move(&self, pond: usize) -> Result<(), Error>;
pub fn current_player(&self) -> &Player;
pub fn is_finished(&self) -> bool;
pub fn bank(&self, player: &Player) -> u32;
pub fn ponds(&self, player: &Player) -> [u32; 6];

You can also determine what the game would look like if you made a particular move with:

game.clone().choose(3);

Once you have two AI objects, you can play them against each other with:

game.play(ai_player_a, ai_player_b, true);

The last parameter is the verbose flag. If true, each move chosen and the board state after each move will be printed to stdout.

There are some AIs already provided in kalaha::ai. You can test them against each other by passing commandline arguments to cargo run:

# run alpha-beta pruning with a depth of 9 against the last valid move AI
cargo run -- -a alphabeta -d 9 -b last

# see all available options
cargo run -- -h

About

Because why not?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages