Crates.io | fenex |
lib.rs | fenex |
version | 0.1.4 |
source | src |
created_at | 2024-01-14 04:00:41.166081 |
updated_at | 2024-01-21 10:49:40.556925 |
description | A rust library for parsing and handling FEN and algebraic chess notations. |
homepage | |
repository | https://github.com/Smallsan/fenex |
max_upload_size | |
id | 1099174 |
size | 59,341 |
Fenex is a Rust library for handling chess game logic. It provides support for parsing Forsyth-Edwards Notation (FEN) strings, handling chess moves in algebraic notation, and more.
Both Coordinates and Notations are 1-indexed.
Coordinates
StructThe Coordinates module provides the Coordinates
struct for representing a position on the chess board.
Constructor: Coordinates { x: i8, y: i8 }
Coordinates
struct with given x and y values.From Notation: Coordinates::from_notation(notation: Notation) -> Result<Coordinates, &'static str>
Notation
struct into a Coordinates
struct.To Notation: Coordinates::to_notation(&self) -> Result<Notation, &'static str>
Coordinates
struct into a Notation
struct.Notation
StructThe Notation module provides the Notation
struct for representing a chess move in algebraic notation.
Constructor: Notation::new(file: char, rank: char) -> Option<Notation>
Notation
struct with given file and rank values.From Coordinates: Notation::from_coordinates(coordinates: Coordinates) -> Result<Notation, &'static str>
Coordinates
struct into a Notation
struct.To Coordinates: Notation::to_coordinates(&self) -> Result<Coordinates, &'static str>
Notation
struct into a Coordinates
struct.Board
StructThe Board
module provides the Board
struct for handling chess boards in both 1D and 2D representations.
Constructor: Board::new_one_dimensional() -> Board
and Board::new_two_dimensional() -> Board
1D
or 2D
board with all squares empty.Starting Position: Board::new_one_dimensional_starting_position() -> Board
and Board::new_two_dimensional_starting_position() -> Board
1D
or 2D
board with pieces in the starting position.Set Piece: Board::set_piece(&mut self, coordinates: Coordinates, piece: ChessPieceEnum)
Get Piece: Board::get_piece(&self, coordinates: Coordinates) -> Option<&ChessPieceEnum>
Generate Moves: Board::generate_moves(&self, color: Color) -> Vec<Move>
Move
StructThe Move
struct represents a move in a chess game. It contains the type of the piece being moved and the start and end coordinates of the move.
Move::new(from: Coordinates, to: Coordinates, piece_type: PieceType) -> Move
Move
with the given start and end coordinates, and the type of the piece being moved.Just run the cargo add fenex
in your project directory.
Or add fenex = "0.1.3"
in your Cargo.toml, Under dependencies.
#[test]
fn notation_and_coordinates() {
// Creates a Notation from chars. ('file' 'rank').
let notation: Notation = Notation::new('e', '4').unwrap();
// Creates Coordinates from an i8. (x, y).
let coordinates: Coordinates = Coordinates::new(5, 4);
// Creates a Notation from string. ("e4").
let notation_from_string: Notation = Notation::from_string("e4").unwrap();
// Creates Coordinates from a string of 2 i8 separated by a comma.
// ("4.3").
let coordinate_from_string: Coordinates = Coordinates::from_string("5,4").unwrap();
}
fn boards_and_moves() {
// Creates a 2D board, With starting pieces.
let mut two_dimensional_board = Board::new_two_dimensional_starting_position();
// Creates a 1D board, With starting pieces.
let mut one_dimensional_board = Board::new_one_dimensional_starting_position();
// Creates Coordinates from a string of 2 i8 separated by a comma.
let from = Coordinates::from_notation_string("e2").unwrap();
// Creates Coordinates from a string of 2 i8 separated by a comma.
let to = Coordinates::from_notation_string("e4").unwrap();
// Moves a piece from one coordinate to another.
one_dimensional_board.move_piece_with_coordinates(from, to);
// Generates all possible movements for White.
let movement = one_dimensional_board.generate_moves(Color::White);
let move = Move
}
Contributions to Fenex are welcome! Please ensure your code is formatted with cargo fmt
before creating a pull request.