diff --git a/.gitignore b/.gitignore index eb5a316..0732256 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,27 @@ target +.devenv/ + +# Generated by Cargo +# will have compiled files and executables +debug +target + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# Generated by cargo mutants +# Contains mutation testing data +**/mutants.out*/ + +# rustc will dump stack traces when hitting an internal compiler error to PWD +rustc-ice-*.txt + +# RustRover +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/src/logic/board.rs b/src/logic/board.rs new file mode 100644 index 0000000..b77e750 --- /dev/null +++ b/src/logic/board.rs @@ -0,0 +1,48 @@ +use crate::logic::utils::Field; +use std::ops::Index; + +pub struct Board { + board: Vec, + pub size: usize, +} + +impl Board { + /// Board is always a square. Size parameter is a side of this rectangle. Defaults to 8 + pub fn new(size: Option) -> Self { + let size = size.unwrap_or(8); + + let board: Vec = (0..size) + .flat_map(|row| (0..size).map(move |column| Field { row, column })) + .collect(); + + Board { board, size } + } +} + +impl Index for Board { + type Output = [Field]; + + fn index<'a>(&'a self, i: usize) -> &'a [Field] { + let start = i * &self.size; + let end: usize = start + &self.size; + &self.board[start..end] + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_board_test() { + let test_board: Board = Board::new(None); + let size: usize = test_board.size; + + for row in 0..size { + for column in 0..size { + assert_eq!(test_board[row][column].row, row); + assert_eq!(test_board[row][column].column, column); + } + } + } +} diff --git a/src/logic/mod.rs b/src/logic/mod.rs new file mode 100644 index 0000000..2fe4b6d --- /dev/null +++ b/src/logic/mod.rs @@ -0,0 +1,4 @@ +mod board; +mod pawn; +mod player; +mod utils; diff --git a/src/logic/pawn.rs b/src/logic/pawn.rs new file mode 100644 index 0000000..40b7ffa --- /dev/null +++ b/src/logic/pawn.rs @@ -0,0 +1,54 @@ +use crate::logic::utils::Field; + +pub enum PawnState { + Man(Field), + Dame(Field), + Captured, +} + +/// Basic pawn +pub struct Pawn { + state: PawnState, + // TODO! Change this to Player when Player is implemented + owner: String, +} + +impl Pawn { + // TODO! Look at line 16 + /// Every new Pawn is of type Man + pub fn new(start_pos: Field, owner: String) -> Self { + Self { + state: PawnState::Man(start_pos), + owner, + } + } + + /// Return true if a pawn on given position is owned by the same player that this is + // TODO! Implement + fn is_mine(pos: Field) -> bool { + true + } + + pub fn can_capture(&self) -> bool { + match &self.state { + PawnState::Man(pos) => { + // Man can capture only on diagonal axis going one up, left or right + let left_pos: Field = Field { + row: (pos.row + 1), + column: (pos.column - 1), + }; + let left_right: Field = Field { + row: (pos.row + 1), + column: (pos.column + 1), + }; + } + PawnState::Dame(pos) => {} + PawnState::Captured => { + // Captured pawns cannot capture + return false; + } + } + + false + } +} diff --git a/src/logic/utils.rs b/src/logic/utils.rs new file mode 100644 index 0000000..72ab60d --- /dev/null +++ b/src/logic/utils.rs @@ -0,0 +1,4 @@ +pub struct Field { + pub row: usize, + pub column: usize, +}