Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thanadolps/bitboard_xo
XO game implemented in rust with minimum memory usage
https://github.com/thanadolps/bitboard_xo
bitboard rust rust-crate rust-lang tic-tac-toe tictactoe-game
Last synced: 21 days ago
JSON representation
XO game implemented in rust with minimum memory usage
- Host: GitHub
- URL: https://github.com/thanadolps/bitboard_xo
- Owner: thanadolps
- License: mit
- Created: 2019-11-26T13:26:14.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-30T14:34:31.000Z (almost 5 years ago)
- Last Synced: 2024-11-30T13:58:10.653Z (23 days ago)
- Topics: bitboard, rust, rust-crate, rust-lang, tic-tac-toe, tictactoe-game
- Language: Rust
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/thanadolps/bitboard_xo.svg?branch=master)](https://travis-ci.org/thanadolps/bitboard_xo)
[![Crates.io](https://img.shields.io/crates/v/bitboard_xo)](https://crates.io/crates/bitboard_xo)# bitboard_xo
A XO (aka tic-tac-toe/noughts and crosses) library
with focus on minimum memory usage without compromising performance.Minimum memory usage is achieve using [bitboard],
a common data structure in chess, adapted to fit XO game.The current sizeof XO type is 32 bit, smaller than a pointer in most machine.
## Examples
```rust
use bitboard_xo::*;use std::error::Error;
use std::io::{stdin, stdout, Write};/// try read a u8 from console
fn read_u8() -> Result> {
let mut user_input = String::new();
stdin().read_line(&mut user_input)?;
Ok(user_input.trim().parse()?)
}/// prompt user for input to get xo position
fn get_xo_pos() -> XOPos {
fn try_get_xo_pos() -> Result> {
// prompt user and read row from console
print!("Input row : ");
stdout().flush()?;
let row = read_u8()?;// prompt user and read col from console
print!("Input col : ");
stdout().flush()?;
let col = read_u8()?;// parse row, col into XOPos
Ok(XOPos::row_col(row, col)?)
};loop {
match try_get_xo_pos() {
Ok(xo_pos) => break xo_pos,
Err(err) => println!("{}", err),
}
}
}fn main() -> Result<(), XOError> {
// create new XO game
let mut game = XO::new();loop {
println!("{}", game);
// get position from user's input
let input_xo_pos = get_xo_pos();// play at that position and match the returned game state
match game.play(input_xo_pos) {
// game end
Ok(Some(game_result)) => {
println!("{}", game);
println!("game result => {:?}", game_result);
break;
}
// game continue
Ok(None) => println!("game continue..."),
// some game error occurred
Err(xo_err) => println!("Error: {}", xo_err),
}
}Ok(())
}
```
[bitboard]: https://en.wikipedia.org/wiki/BitboardLicense: MIT