Skip to content

Commit

Permalink
Improve readme (#5)
Browse files Browse the repository at this point in the history
* Add ci badge
* CI for docs
* index for github pages
* Fix clippy warnings
* Add cli image README
* Rename test_apply_move
  • Loading branch information
joajfreitas committed Apr 12, 2023
1 parent 0f161cf commit 00414d1
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 59 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: docs

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
docs:
name: docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly

- name: Build documentation
run: cargo doc --no-deps
- name: Generate index
run: echo "<head><meta http-equiv=\"Refresh\" content=\"0; url='https://joajfreitas.github.io/fchess/fchess/index.html'\" /></head>" > target/doc/index.html
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./target/doc
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Run move generation tests
run: ./scripts/run_move_generation_tests.sh
run: ./scripts/apply_move_tests.sh
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ regex = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.9"
clap = { version = "4.2.1", features = ["derive"] }

[dependencies.uuid]
version = "1.3.1"
Expand All @@ -34,8 +35,8 @@ path = "bin/uci.rs"


[[bin]]
name = "test_move_generation"
path = "bin/test_move_generation.rs"
name = "test_apply_move"
path = "bin/test_apply_move.rs"

[[bin]]
name = "cli"
Expand Down
83 changes: 78 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,88 @@
# fchess

## Run
![https://github.com/joajfreitas/fchess/actions/workflows/main.yaml/badge.svg](https://github.com/joajfreitas/fchess/actions/workflows/main.yaml/badge.svg)

cli tool:
fchess is a work in progress chess engine written in Rust.


## Implementation status


## Running

fchess provides a uci interface:
```
λ cargo run --bin uci
```
cargo run --bin cli


Alternativelly you can play directly in the terminal:

```
λ cargo run --bin cli
┌───┬───┬───┬───┬───┬───┬───┬───┐
8 │ ♖ │ ♘ │ ♗ │ ♕ │ ♔ │ ♗ │ ♘ │ ♖ │
├───┼───┼───┼───┼───┼───┼───┼───┤
7 │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │
├───┼───┼───┼───┼───┼───┼───┼───┤
6 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
5 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
4 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
3 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
2 │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │
├───┼───┼───┼───┼───┼───┼───┼───┤
1 │ ♜ │ ♞ │ ♝ │ ♛ │ ♚ │ ♝ │ ♞ │ ♜ │
└───┴───┴───┴───┴───┴───┴───┴───┘
a b c d e f g h
> e2e4
┌───┬───┬───┬───┬───┬───┬───┬───┐
8 │ ♖ │ ♘ │ ♗ │ ♕ │ ♔ │ ♗ │ ♘ │ ♖ │
├───┼───┼───┼───┼───┼───┼───┼───┤
7 │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │ ♙ │
├───┼───┼───┼───┼───┼───┼───┼───┤
6 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
5 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
4 │ │ │ │ │ ♟︎ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
3 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
2 │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ │ ♟︎ │ ♟︎ │ ♟︎ │
├───┼───┼───┼───┼───┼───┼───┼───┤
1 │ ♜ │ ♞ │ ♝ │ ♛ │ ♚ │ ♝ │ ♞ │ ♜ │
└───┴───┴───┴───┴───┴───┴───┴───┘
a b c d e f g h
=> Book move
┌───┬───┬───┬───┬───┬───┬───┬───┐
8 │ ♖ │ ♘ │ ♗ │ ♕ │ ♔ │ ♗ │ ♘ │ ♖ │
├───┼───┼───┼───┼───┼───┼───┼───┤
7 │ ♙ │ ♙ │ ♙ │ ♙ │ │ ♙ │ ♙ │ ♙ │
├───┼───┼───┼───┼───┼───┼───┼───┤
6 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
5 │ │ │ │ │ ♙ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
4 │ │ │ │ │ ♟︎ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
3 │ │ │ │ │ │ │ │ │
├───┼───┼───┼───┼───┼───┼───┼───┤
2 │ ♟︎ │ ♟︎ │ ♟︎ │ ♟︎ │ │ ♟︎ │ ♟︎ │ ♟︎ │
├───┼───┼───┼───┼───┼───┼───┼───┤
1 │ ♜ │ ♞ │ ♝ │ ♛ │ ♚ │ ♝ │ ♞ │ ♜ │
└───┴───┴───┴───┴───┴───┴───┴───┘
a b c d e f g h
```

tests:
## Tests

```
cargo test
λ cargo test
λ ./scripts/run_move_generation_tests.sh
```
4 changes: 2 additions & 2 deletions bin/book.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

use fchess::board::Board;
use fchess::book::Book;
use fchess::Board;
use fchess::Book;

fn main() {
let args: Vec<String> = env::args().collect();
Expand Down
39 changes: 24 additions & 15 deletions bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
use rustyline::{Editor, Result};
use std::env;
use clap::Parser;

use fchess::board::Board;
use fchess::book::Book;
use fchess::moves::Move;
use fchess::side::Side;
use fchess::solver::Solver;
use fchess::Board;
use fchess::Book;
use fchess::Move;
use fchess::Side;
use fchess::Solver;

fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
/// fchess cli interface
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// path to polyglot openning book
#[arg(short, long)]
book: Option<String>,
}

fn main() -> Result<()> {
// Setup shell history
let mut rl = Editor::<()>::new()?;
if rl.load_history(".fchess_history").is_err() {
println!("No previous history");
}

let args = Args::parse();

let book = args.book.map(|book| Book::from_filename(&book));

let book_filename = args.get(1).unwrap();

let book = Book::from_filename(book_filename);
let mut board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0");
let solver = Solver::new();
println!("{}", board);
Expand All @@ -38,17 +47,17 @@ fn main() -> Result<()> {
}
}
}
Side::Black => dbg!(match book.get_best_move(&board) {
Side::Black => match book.as_ref().and_then(|b| b.get_best_move(&board)) {
Some(mov) => {
println!("Book move");
println!("=> Book move");
mov
}
_ => {
println!("Search move");
println!("=> Search move");
solver.best_move(&board).unwrap()
}
}
.to_algebraic()),
.to_algebraic(),
})
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion bin/fen_diagram.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::io;

use fchess::board::Board;
use fchess::Board;

fn main() {
let args = env::args().collect::<Vec<String>>();
Expand Down
4 changes: 2 additions & 2 deletions bin/test_move_generation.rs → bin/test_apply_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::fs;
use serde::{Deserialize, Serialize};
use serde_json::Result;

use fchess::board::Board;
use fchess::moves::Move;
use fchess::Board;
use fchess::Move;

#[derive(Serialize, Deserialize, Debug)]
struct TestCase {
Expand Down
6 changes: 3 additions & 3 deletions bin/uci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;

use fchess::board::Board;
use fchess::moves::Move;
use fchess::solver::Solver;
use fchess::Board;
use fchess::Move;
use fchess::Solver;

fn main() -> io::Result<()> {
let (tx, rx): (Sender<String>, Receiver<String>) = mpsc::channel();
Expand Down
10 changes: 10 additions & 0 deletions scripts/apply_move_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -e

cargo run --release --bin test_apply_move -- testcases/pawns.yaml
cargo run --release --bin test_apply_move -- testcases/standard.yaml
cargo run --release --bin test_apply_move -- testcases/castling.yaml
cargo run --release --bin test_apply_move -- testcases/famous.yaml
cargo run --release --bin test_apply_move -- testcases/promotions.yaml
cargo run --release --bin test_apply_move -- testcases/taxing.yaml
10 changes: 0 additions & 10 deletions scripts/run_move_generation_tests.sh

This file was deleted.

2 changes: 2 additions & 0 deletions src/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Set bit in bitfield according to value
pub fn set_bit<T>(
bitfield: T,
position: u8,
Expand All @@ -16,6 +17,7 @@ where
(bitfield & !mask) | (value << position)
}

/// Set bit in bitfield to true
pub fn enable_bit<T>(
bitfield: T,
position: u8,
Expand Down
1 change: 1 addition & 0 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum Castling {
BlackLong,
}

/// Bitboard representation of the chess board
#[derive(Default, Clone, Eq, PartialEq, Debug)]
pub struct Board {
pieces: [u64; 13],
Expand Down
1 change: 1 addition & 0 deletions src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl fmt::Debug for Entry {
}
}

/// Representation of a PolyGlot book
pub struct Book(Vec<Entry>);

impl Book {
Expand Down
35 changes: 22 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
/*!
This crate provides a library for chess board representation, move generation and an engine.
*/
extern crate num;
#[macro_use]
extern crate num_derive;

pub mod bitboard;
pub mod bitwise;
pub mod board;
pub mod book;
pub mod common;
pub mod dumb7fill;
pub mod move_generator;
pub mod moves;
pub mod moveset;
pub mod piece;
pub mod side;
pub mod solver;
pub mod square;
pub use crate::board::Board;
pub use crate::book::Book;
pub use crate::moves::Move;
pub use crate::side::Side;
pub use crate::solver::Solver;

mod bitboard;
mod bitwise;
mod board;
mod book;
mod common;
mod dumb7fill;
mod move_generator;
mod moves;
mod moveset;
mod piece;
mod side;
mod solver;
mod square;

#[cfg(test)]
mod tests {
Expand Down
16 changes: 13 additions & 3 deletions src/move_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ impl MoveGenerator {
.collect::<Vec<MoveSet>>()
}

pub fn generate_moves_for_piece(&self, board: &Board, square: Square) -> Option<MoveSet> {
Some(self.attack(board, &Piece::new(square, board.piece_at(square)?)))
}

pub fn attack(&self, board: &Board, piece: &Piece) -> MoveSet {
let square = piece.get_square();
Expand Down Expand Up @@ -269,3 +266,16 @@ impl MoveGenerator {
MoveSet::new(from, piece, flood)
}
}
#[cfg(test)]
mod tests {
use super::Board;
use super::Square;
use super::Piece;
use super::MoveGenerator;
use super::MoveSet;
impl MoveGenerator {
pub fn generate_moves_for_piece(&self, board: &Board, square: Square) -> Option<MoveSet> {
Some(self.attack(board, &Piece::new(square, board.piece_at(square)?)))
}
}
}
1 change: 1 addition & 0 deletions src/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl From<PieceType> for Scope {
}
}

/// Chess move
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Move {
src: Square,
Expand Down
2 changes: 1 addition & 1 deletion src/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PieceType {
_ => None,
}
}
pub fn to_char(&self) -> char {
pub fn to_char(self) -> char {
match self {
PieceType::WhitePawn => 'P',
PieceType::WhiteRook => 'R',
Expand Down
Loading

0 comments on commit 00414d1

Please sign in to comment.