mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 11:10:48 +01:00
chore: add board and pawn
This commit is contained in:
26
.gitignore
vendored
26
.gitignore
vendored
@@ -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/
|
||||
|
||||
48
src/logic/board.rs
Normal file
48
src/logic/board.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::logic::utils::Field;
|
||||
use std::ops::Index;
|
||||
|
||||
pub struct Board {
|
||||
board: Vec<Field>,
|
||||
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<usize>) -> Self {
|
||||
let size = size.unwrap_or(8);
|
||||
|
||||
let board: Vec<Field> = (0..size)
|
||||
.flat_map(|row| (0..size).map(move |column| Field { row, column }))
|
||||
.collect();
|
||||
|
||||
Board { board, size }
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/logic/mod.rs
Normal file
4
src/logic/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
mod board;
|
||||
mod pawn;
|
||||
mod player;
|
||||
mod utils;
|
||||
54
src/logic/pawn.rs
Normal file
54
src/logic/pawn.rs
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
4
src/logic/utils.rs
Normal file
4
src/logic/utils.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub struct Field {
|
||||
pub row: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
Reference in New Issue
Block a user