From a1e2261d50ad47f4e2137d86a6fe98cb01f3a2ee Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 23 Jan 2025 16:02:02 -0500 Subject: [PATCH] create Board and Piece types --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 6 ++++++ src/main.rs | 5 +++++ src/repr.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/repr.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a768ae0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "othello" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3af4f43 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "othello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..71b2fcc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod repr; + +fn main() { + println!("Hello, world!"); +} diff --git a/src/repr.rs b/src/repr.rs new file mode 100644 index 0000000..63de19e --- /dev/null +++ b/src/repr.rs @@ -0,0 +1,44 @@ +#[derive(Copy, Clone)] +pub enum Piece { + Black, + White, +} + +const BOARD_SIZE: usize = 8; + +#[derive(Copy, Clone)] +pub struct Board { + board: [[Option; BOARD_SIZE]; BOARD_SIZE], +} + +impl Board { + pub const fn new() -> Self { + Self { + board: [[None; BOARD_SIZE]; BOARD_SIZE], + } + } + + /// Returns a mutable reference to a place on the [`Board`] + /// at (i, j) + pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option { + &mut self.board[i][j] + } + + /// Returns a reference to a place on the [`Board`] + /// at (i, j) + pub const fn get(&self, i: usize, j: usize) -> &Option { + &self.board[i][j] + } + + /// Place a piece on the [`Board`] + /// Returns an error if there already exists a piece there + pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> { + if self.get(i, j).is_some() { + return Err(format!("Cannot place on ({},{}), piece exists", i, j)); + } else { + *self.get_mut(i, j) = Some(piece); + } + + Ok(()) + } +}