From 69946f09be61f740f5f1176329b710b9e86d0cd9 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Sat, 6 Apr 2024 16:11:09 +0300 Subject: [PATCH] Commit working directory --- .gitignore | 3 ++ Cargo.lock | 7 ++++ Cargo.toml | 8 +++++ src/board.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/game.rs | 14 ++++++++ src/main.rs | 9 +++++ 6 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/board.rs create mode 100644 src/game.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92a20f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target + +settings.json \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..dc00e3d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ippos" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fdc4663 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ippos" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/board.rs b/src/board.rs new file mode 100644 index 0000000..c08361a --- /dev/null +++ b/src/board.rs @@ -0,0 +1,97 @@ +use std::fmt; +use u64 as Bitboard; + +pub struct Board { + white_pieces: [Piece; 6], + black_pieces: [Piece; 6], +} + +impl Board { + pub fn new() -> Board { + Board { + white_pieces: [ + Piece::new(0xff00, Kind::Pawn, Color::White), + Piece::new(0x81, Kind::Rook, Color::White), + Piece::new(0x42, Kind::Knight, Color::White), + Piece::new(0x24, Kind::Bishop, Color::White), + Piece::new(0x8, Kind::Queen, Color::White), + Piece::new(0x10, Kind::King, Color::White), + ], + black_pieces: [ + Piece::new(0xff000000000000, Kind::Pawn, Color::Black), + Piece::new(0x8100000000000000, Kind::Rook, Color::Black), + Piece::new(0x4200000000000000, Kind::Knight, Color::Black), + Piece::new(0x2400000000000000, Kind::Bishop, Color::Black), + Piece::new(0x1000000000000000, Kind::Queen, Color::Black), + Piece::new(0x800000000000000, Kind::King, Color::Black), + ], + } + } +} + +impl fmt::Display for Board { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for piece in &self.white_pieces { + writeln!( + f, + "Color: {:?}\nKind: {:?}\n{piece}", + piece.color, piece.kind + )?; + } + writeln!(f, "")?; + for piece in &self.black_pieces { + writeln!( + f, + "Color: {:?}\nKind: {:?}\n{piece}", + piece.color, piece.kind + )?; + } + write!(f, "") + } +} + +pub struct Piece { + pub position: Bitboard, + pub kind: Kind, + pub color: Color, +} + +impl Piece { + pub fn new(position: Bitboard, kind: Kind, color: Color) -> Piece { + Piece { + position, + kind, + color, + } + } +} + +impl fmt::Display for Piece { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for rank in (0..8).rev() { + for file in 0..8 { + let square = 1 << (rank * 8 + file); + let position = if square & self.position != 0x0 { 1 } else { 0 }; + write!(f, "{} ", position)?; + } + writeln!(f, "")?; + } + write!(f, "") + } +} + +#[derive(Debug)] +pub enum Kind { + Pawn, + Rook, + Knight, + Bishop, + Queen, + King, +} + +#[derive(Debug)] +pub enum Color { + White, + Black, +} diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..f308607 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,14 @@ +use crate::board::Board; + +pub struct Game {} + +impl Game { + pub fn new() -> Game { + Game {} + } + + pub fn run(&self) { + let board = Board::new(); + println!("{}", board) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c118b6f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,9 @@ +pub mod board; +pub mod game; + +use game::Game; + +fn main() { + let game = Game::new(); + game.run(); +}