Fenex Chess Library
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.
Features
Coordinates And Notations
Both Coordinates and Notations are 1-indexed.
Coordinates Struct
The Coordinates module provides the Coordinates struct for representing a position on the chess board.
-
Constructor:
Coordinates { x: i8, y: i8 }- Creates a new
Coordinatesstruct with given x and y values.
- Creates a new
-
From Notation:
Coordinates::from_notation(notation: Notation) -> Result<Coordinates, &'static str>- Converts a
Notationstruct into aCoordinatesstruct.
- Converts a
-
To Notation:
Coordinates::to_notation(&self) -> Result<Notation, &'static str>- Converts the
Coordinatesstruct into aNotationstruct.
- Converts the
Notation Struct
The Notation module provides the Notation struct for representing a chess move in algebraic notation.
-
Constructor:
Notation::new(file: char, rank: char) -> Option<Notation>- Creates a new
Notationstruct with given file and rank values.
- Creates a new
-
From Coordinates:
Notation::from_coordinates(coordinates: Coordinates) -> Result<Notation, &'static str>- Converts a
Coordinatesstruct into aNotationstruct.
- Converts a
-
To Coordinates:
Notation::to_coordinates(&self) -> Result<Coordinates, &'static str>- Converts the
Notationstruct into aCoordinatesstruct.
- Converts the
Boards And Movements
Board Struct
The Board module provides the Board struct for handling chess boards in both 1D and 2D representations.
-
Constructor:
Board::new_one_dimensional() -> BoardandBoard::new_two_dimensional() -> Board- Creates a new
1Dor2Dboard with all squares empty.
- Creates a new
-
Starting Position:
Board::new_one_dimensional_starting_position() -> BoardandBoard::new_two_dimensional_starting_position() -> Board- Creates a new
1Dor2Dboard with pieces in the starting position.
- Creates a new
-
Set Piece:
Board::set_piece(&mut self, coordinates: Coordinates, piece: ChessPieceEnum)- Sets a piece at the given coordinates.
-
Get Piece:
Board::get_piece(&self, coordinates: Coordinates) -> Option<&ChessPieceEnum>- Gets the piece at the given coordinates.
-
Generate Moves:
Board::generate_moves(&self, color: Color) -> Vec<Move>- Generates all valid moves for the pieces of the given color on the board.
Move Struct
The 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.
- Constructor:
Move::new(from: Coordinates, to: Coordinates, piece_type: PieceType) -> Move- Creates a new
Movewith the given start and end coordinates, and the type of the piece being moved.
- Creates a new
How To Install
Just run the cargo add fenexin your project directory.
Or add fenex = "0.1.3" in your Cargo.toml, Under dependencies.
Examples
Contribution
Contributions to Fenex are welcome! Please ensure your code is formatted with cargo fmt before creating a pull request.