This commit is contained in:
Kai Vogelgesang 2023-12-22 19:37:13 +01:00
commit 2244651aa1
5 changed files with 670 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

400
Cargo.lock generated Normal file
View File

@ -0,0 +1,400 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "console"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"unicode-width",
"windows-sys 0.45.0",
]
[[package]]
name = "dialoguer"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de"
dependencies = [
"console",
"shell-words",
"tempfile",
"thiserror",
"zeroize",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "fastrand"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]]
name = "knucklebones"
version = "0.1.0"
dependencies = [
"dialoguer",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "proc-macro2"
version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "rustix"
version = "0.38.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316"
dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "syn"
version = "2.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5"
dependencies = [
"cfg-if",
"fastrand",
"redox_syscall",
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "thiserror"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc 0.42.2",
"windows_i686_gnu 0.42.2",
"windows_i686_msvc 0.42.2",
"windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "zeroize"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d"

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "knucklebones"
version = "0.1.0"
edition = "2021"
[dependencies]
dialoguer = "0.11.0"

237
src/game.rs Normal file
View File

@ -0,0 +1,237 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Player {
Bottom,
Top,
}
#[derive(Debug, Clone, Copy)]
pub enum Column {
Left,
Center,
Right,
}
/// Encodes the state of the game
///
/// Each cell can take 7 values (empty or 1-6) -> 3 * 18 = 54 bits required.
/// We need one additional bit to store which player's turn it is.
///
/// Cell Layout:
/// ```
/// 11 14 17
/// 10 13 16
/// 9 12 15
///
/// 0 3 6
/// 1 4 7
/// 2 5 8
/// ```
///
/// Offsets:
/// ```
/// 33 42 51
/// 30 39 48
/// 27 36 45
///
/// 0 9 18
/// 3 12 21
/// 6 15 24
/// ```
///
/// current_player: `54`
#[derive(Debug, Clone)]
pub struct Board(u64);
#[derive(Debug)]
pub struct IllegalMove(());
impl Board {
pub fn empty() -> Self {
Self::from_values([&[], &[], &[]], [&[], &[], &[]], Player::Bottom)
}
pub fn from_values(bottom: [&[u8]; 3], top: [&[u8]; 3], current_player: Player) -> Self {
let mut bits = 0;
for (start, half) in [(0, bottom), (27, top)] {
for (index, &col) in half.iter().enumerate() {
for (offset, &val) in col.iter().enumerate() {
let shift = start + index * 9 + offset * 3;
bits |= (val as u64) << shift;
}
}
}
if current_player == Player::Top {
bits |= 1 << 54
}
Self(bits)
}
pub fn visualize(&self) {
let &Self(bits) = self;
let print_half = |rows| {
for row in rows {
for offset in row {
let value = (bits >> offset & 0b111u64) as u8;
if value > 0 {
print!("{value} ");
} else {
print!("_ ");
}
}
println!()
}
};
print_half([[33, 42, 51], [30, 39, 48], [27, 36, 45]]);
println!();
print_half([[0, 9, 18], [3, 12, 21], [6, 15, 24]]);
}
pub fn current_player(&self) -> Player {
let &Self(bits) = self;
if bits >> 54 & 1 == 0 {
Player::Bottom
} else {
Player::Top
}
}
pub fn is_game_over(&self) -> bool {
let &Self(bits) = self;
let has_empty_slots = |offset| -> bool {
for i in [0, 3, 6] {
if (bits >> offset + i) & 0b111u64 == 0u64 {
return true;
}
}
false
};
!((has_empty_slots(0) || has_empty_slots(9) || has_empty_slots(18))
&& (has_empty_slots(27) || has_empty_slots(36) || has_empty_slots(45)))
}
/// (bottom, top)
pub fn column_scores(&self) -> ((u8, u8, u8), (u8, u8, u8)) {
let &Self(bits) = self;
let score_col = |offset| {
let mut counts = [0; 6];
for i in [0, 3, 6] {
let value = ((bits >> offset + i) & 0b111u64) as usize;
if value == 0 {
continue;
}
counts[value - 1] += 1;
}
counts
.iter()
.enumerate()
.map(|(value, &count)| {
let value = (value + 1) as u8;
value
* count
* match count {
2 => 2,
3 => 9,
_ => 1,
}
})
.sum()
};
(
(score_col(0), score_col(9), score_col(18)),
(score_col(27), score_col(36), score_col(45)),
)
}
pub fn score(&self) -> i16 {
let ((a, b, c), (d, e, f)) = self.column_scores();
a as i16 + b as i16 + c as i16 - d as i16 - e as i16 - f as i16
}
pub fn play(&self, column: Column, value: u8) -> Result<Self, IllegalMove> {
let Self(mut bits) = *self;
let mut apply_move = |offset, opposite| -> Result<(), IllegalMove> {
// push value to the first zero after offset
let mut found = false;
for index in [0, 3, 6] {
if ((bits >> offset + index) & 0b111u64) != 0u64 {
continue;
}
found = true;
bits &= !((0b111u64 << offset + index) as u64);
bits |= (value as u64) << offset + index;
break;
}
// raise if all three are non-zero
if !found {
return Err(IllegalMove(()));
}
// eliminate all occurences from value after opposite
let mut tmp = [0, 0, 0];
let mut i = 0;
for index in [0, 3, 6] {
let other = ((bits >> opposite + index) & 0b111u64) as u8;
if other == value {
continue;
}
tmp[i] = other;
i += 1;
}
if i != 3 {
for (index, &value) in tmp.iter().enumerate() {
bits &= !((0b111u64 << opposite + index) as u64);
bits |= (value as u64) << opposite + index;
}
}
Ok(())
};
match (column, self.current_player()) {
(Column::Left, Player::Bottom) => apply_move(0, 27)?,
(Column::Left, Player::Top) => apply_move(27, 0)?,
(Column::Center, Player::Bottom) => apply_move(9, 36)?,
(Column::Center, Player::Top) => apply_move(36, 9)?,
(Column::Right, Player::Bottom) => apply_move(18, 45)?,
(Column::Right, Player::Top) => apply_move(45, 18)?,
}
// switch current player
bits ^= 1 << 54;
Ok(Self(bits))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example() {
let board = Board::from_values(
[&[6, 5, 5], &[5, 5], &[1, 5]],
[&[2, 2, 1], &[4, 3, 3], &[4, 2, 3]],
Player::Bottom,
);
board.visualize();
assert_eq!(board.column_scores(), ((26, 20, 6), (9, 16, 9)));
assert!(board.score() > 0);
assert!(board.is_game_over());
}
}

25
src/main.rs Normal file
View File

@ -0,0 +1,25 @@
use dialoguer::Select;
mod game;
use game::{Board, Column};
fn main() {
let mut b = Board::empty();
while !b.is_game_over() {
b.visualize();
println!("Current player: {:?}", b.current_player());
let d6 = (Select::new().items(&[1, 2, 3, 4, 5, 6]).interact().unwrap() + 1) as u8;
let column = [Column::Left, Column::Center, Column::Right][Select::new()
.items(&["Left", "Center", "Right"])
.interact()
.unwrap()];
b = b.play(column, d6).expect("Illegal move!");
}
println!("Game over. Score: {} ({:?})", b.score(), b.column_scores());
}