some lib work
This commit is contained in:
+7
-11
@@ -1,14 +1,10 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
}
|
||||
pub mod piece;
|
||||
pub mod tile;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{piece::Color, tile::Tile};
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
||||
pub struct Board<const N: usize> {
|
||||
to_move: Color,
|
||||
tiles: [[Tile; N]; N],
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
||||
#[repr(u8)]
|
||||
pub enum Color {
|
||||
White = 0,
|
||||
Black = 1,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
#[repr(u8)]
|
||||
pub enum Piece {
|
||||
None = 0,
|
||||
Flat = 1,
|
||||
Wall = 2,
|
||||
Caps = 3,
|
||||
}
|
||||
impl Display for Piece {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Piece::None => write!(f, "----"),
|
||||
Piece::Flat => write!(f, "Flat"),
|
||||
Piece::Wall => write!(f, "Wall"),
|
||||
Piece::Caps => write!(f, "Caps"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
#[repr(u8)]
|
||||
pub enum ColoredPiece {
|
||||
None = 0,
|
||||
WhiteFlat = 1,
|
||||
WhiteWall = 2,
|
||||
WhiteCaps = 3,
|
||||
|
||||
BlackFlat = 5,
|
||||
BlackWall = 6,
|
||||
BlackCaps = 7,
|
||||
}
|
||||
|
||||
impl ColoredPiece {
|
||||
/// Constructs a new `ColoredPiece`.
|
||||
pub fn new(piece: Piece, color: Color) -> Self {
|
||||
let mut piece_val = (piece as u8) | ((color as u8) << 2);
|
||||
if piece_val == 4 {
|
||||
piece_val = 0;
|
||||
}
|
||||
// safety: value will always be in range
|
||||
unsafe { std::mem::transmute(piece_val) }
|
||||
}
|
||||
|
||||
/// Gets the color of the piece,
|
||||
/// returning `Color::White` if `self` is `None`.
|
||||
pub fn color(&self) -> Color {
|
||||
if (*self as u8 >> 2) == 1 {
|
||||
Color::Black
|
||||
} else {
|
||||
Color::White
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the color of the piece,
|
||||
/// returning `None` if `self` is `None`.
|
||||
pub fn color_opt(&self) -> Option<Color> {
|
||||
let val = *self as u8;
|
||||
if val == 0 {
|
||||
return None;
|
||||
} else if (val >> 2) == 1 {
|
||||
Some(Color::Black)
|
||||
} else {
|
||||
Some(Color::White)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the piece type of this piece.
|
||||
pub fn piece(&self) -> Piece {
|
||||
let piece_val = *self as u8 & 0b11;
|
||||
// safety: value will always be in range
|
||||
unsafe { std::mem::transmute(piece_val) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::piece::Piece;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Tile {
|
||||
/// The colors of the stacked pieces.
|
||||
///
|
||||
/// bit 0 represents the top of the stack (not counting the top piece).
|
||||
stack: u64,
|
||||
/// The length of the stack.
|
||||
stack_len: u8,
|
||||
/// The piece that is on the tile
|
||||
piece: Piece,
|
||||
}
|
||||
|
||||
impl Debug for Tile {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if self.stack_len == 0 {
|
||||
write!(f, "{}", self.piece)
|
||||
} else {
|
||||
let stack: String = (0..self.stack_len)
|
||||
.rev()
|
||||
.map(|i| if (self.stack >> i) & 1 == 1 { 'w' } else { 'b' })
|
||||
.collect();
|
||||
write!(f, "{}{}", stack, self.piece)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn stack_len(&self) -> u8 {
|
||||
self.stack_len
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user