Created
November 14, 2015 22:32
-
-
Save mpgarate/583258d616655cf57089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/.gitignore b/.gitignore | |
index 37727f9..3a6f03f 100644 | |
--- a/.gitignore | |
+++ b/.gitignore | |
@@ -7,5 +7,7 @@ | |
# Executables | |
*.exe | |
+*.swp | |
+ | |
# Generated by Cargo | |
/target/ | |
diff --git a/src/game/.bitboard.rs.swp b/src/game/.bitboard.rs.swp | |
deleted file mode 100644 | |
index 06d61aa..0000000 | |
Binary files a/src/game/.bitboard.rs.swp and /dev/null differ | |
diff --git a/src/game/bitboard.rs b/src/game/bitboard.rs | |
index 08ac932..2d31f05 100644 | |
--- a/src/game/bitboard.rs | |
+++ b/src/game/bitboard.rs | |
@@ -1,3 +1,5 @@ | |
+use game::position::Position; | |
+ | |
#[allow(dead_code)] | |
const BOARD_STATE_EMPTY: u64 = 0x123456789ABCDEF0; | |
@@ -8,28 +10,89 @@ pub struct BitBoard { | |
#[allow(dead_code)] | |
impl BitBoard { | |
- fn new() -> BitBoard { | |
+ pub fn new() -> BitBoard { | |
BitBoard { data: BOARD_STATE_EMPTY } | |
} | |
- fn is_solved(&self) -> bool { | |
+ pub fn is_solved(&self) -> bool { | |
self.data == BOARD_STATE_EMPTY | |
} | |
+ | |
+ pub fn set(&self, pos: Position, value: u8) -> BitBoard { | |
+ let offset = pos.to_offset() as u64; | |
+ | |
+ // reset value at position to zero | |
+ let zeroed_data = self.data & !(0xF << offset); | |
+ | |
+ // update value at position to new value | |
+ let updated_data = zeroed_data | ((value as u64) << (offset)); | |
+ | |
+ BitBoard { data: updated_data } | |
+ } | |
+ | |
+ pub fn get(&self, pos: Position) -> u8 { | |
+ let offset = pos.to_offset(); | |
+ | |
+ let and = self.data & (0xF << offset); | |
+ | |
+ ((and >> offset as u64) & 0xF) as u8 | |
+ } | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
+ use game::position::Position; | |
#[test] | |
- fn constructs_with_empty_state() { | |
+ fn new_bitboard_uses_empty_state() { | |
let b: BitBoard = BitBoard::new(); | |
assert!(b.data == super::BOARD_STATE_EMPTY); | |
} | |
#[test] | |
- fn new_board_is_solved() { | |
+ fn is_solved_true_for_new_board() { | |
let b: BitBoard = BitBoard::new(); | |
assert!(b.is_solved()); | |
} | |
+ | |
+ #[test] | |
+ fn set_value_at_valid_position() { | |
+ let b: BitBoard = BitBoard::new() | |
+ .set( | |
+ Position::new(0, 3), | |
+ 7 | |
+ ); | |
+ | |
+ assert_eq!( | |
+ 7, | |
+ b.get( | |
+ Position::new(0, 3) | |
+ ) | |
+ ); | |
+ } | |
+ | |
+ #[test] | |
+ fn set_values_at_valid_positions() { | |
+ for value in 0..16 { | |
+ for row in 0..4 { | |
+ for col in 0..4 { | |
+ | |
+ let b = BitBoard::new() | |
+ .set( | |
+ Position::new(row, col), | |
+ value | |
+ ); | |
+ | |
+ assert_eq!( | |
+ value, | |
+ b.get(Position::new(row, col)) | |
+ ); | |
+ } | |
+ } | |
+ } | |
+ } | |
+ | |
+ //fn set_negative_value_should_panic() { | |
+ //} | |
} | |
diff --git a/src/game/mod.rs b/src/game/mod.rs | |
index 7e255be..61f833c 100644 | |
--- a/src/game/mod.rs | |
+++ b/src/game/mod.rs | |
@@ -1,2 +1,5 @@ | |
pub use self::bitboard::BitBoard; | |
+pub use self::position::Position; | |
mod bitboard; | |
+mod position; | |
+ | |
diff --git a/src/game/position.rs b/src/game/position.rs | |
new file mode 100644 | |
index 0000000..d40dc8e | |
--- /dev/null | |
+++ b/src/game/position.rs | |
@@ -0,0 +1,43 @@ | |
+#[allow(dead_code)] | |
+const MAX_ROW_INDEX: u8 = 3; | |
+#[allow(dead_code)] | |
+const MAX_COL_INDEX: u8 = 3; | |
+ | |
+#[allow(dead_code)] | |
+pub struct Position { | |
+ row: u8, | |
+ col: u8, | |
+} | |
+ | |
+#[allow(dead_code)] | |
+impl Position { | |
+ pub fn new(row: u8, col: u8) -> Position { | |
+ if row > MAX_ROW_INDEX || col > MAX_COL_INDEX { | |
+ panic!("Invalid argument for position"); | |
+ } | |
+ | |
+ Position { row: row, col: col } | |
+ } | |
+ | |
+ pub fn to_offset(&self) -> u8 { | |
+ let index = (self.row * 4) + self.col; | |
+ 4 * (15 - index) | |
+ } | |
+} | |
+ | |
+#[cfg(test)] | |
+mod tests { | |
+ use super::*; | |
+ | |
+ #[test] | |
+ #[should_panic] | |
+ fn high_col_position_should_panic() { | |
+ Position::new(0, 4); | |
+ } | |
+ | |
+ #[test] | |
+ #[should_panic] | |
+ fn high_row_position_should_panic() { | |
+ Position::new(141, 0); | |
+ } | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment