aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-02-01 16:44:52 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-02-01 16:44:52 +0100
commitf345e944806d493d140190a919739051790324a2 (patch)
treefd94b9ce2316a8a9dacb64b9623319cc6ed517af
downloadconnect4-f345e944806d493d140190a919739051790324a2.tar.gz
connect4-f345e944806d493d140190a919739051790324a2.tar.bz2
connect4-f345e944806d493d140190a919739051790324a2.zip
Initial commit
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml9
-rw-r--r--README.md8
-rw-r--r--src/main.rs86
5 files changed, 110 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c2a9b6f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+tags
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..127371b
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,5 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "connect4"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..a569228
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "connect4"
+version = "0.1.0"
+authors = ["Charles Cabergs <me@cacharle.xyz>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2e4468b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,8 @@
+# connect4
+
+Connect 4 solver.
+
+## Resources
+
+* CodeBullet video - <https://www.youtube.com/watch?v=XRVA5PMSKKE>
+* Pascal Pons blog - <http://blog.gamesolver.org/>
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..35c3e7a
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,86 @@
+const HEIGHT: u64 = 6;
+const WIDTH: u64 = 7;
+
+type Bits = u64;
+
+/**
+ * bit order:
+ *
+ * . . . . . . .
+ * 5 12 19 26 33 40 47
+ * 4 11 18 25 32 39 46
+ * 3 10 17 24 31 38 45
+ * 2 9 16 23 30 37 44
+ * 1 8 15 22 29 36 43
+ * 0 7 14 21 28 35 42
+ *
+ * an extra row is added for the key represention
+ *
+ *
+ *
+ */
+struct Position {
+ /// stones of the current player
+ player: u64,
+ /// stones of the grid
+ mask: u64,
+}
+
+impl Position {
+ fn new() -> Position {
+ Position{ player: 0, mask: 0 }
+ }
+
+ fn from_position(col_moves: &[u64]) -> Position {
+ let mut position = Position::new();
+ for col_pos in col_moves {
+ position.play(*col_pos);
+ }
+ position
+ }
+
+ fn play(&mut self, col_pos: u64) {
+ self.player ^= self.mask;
+ self.mask |= self.mask + (1 << (col_pos * (HEIGHT + 1)));
+ }
+
+ fn key(&self) -> u64 {
+ self.player + self.mask
+ }
+}
+
+use std::fmt;
+
+impl fmt::Debug for Position {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "mask: {:064b}\n", self.mask)?;
+ for i in 0..WIDTH {
+ write!(f, "\t{:07b}\n", (self.mask & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
+ }
+
+ write!(f, "player: {:064b}\n", self.player)?;
+ for i in 0..WIDTH {
+ write!(f, "\t{:07b}\n", (self.player & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
+ }
+
+ write!(f, "key: {:064b}\n", self.key())?;
+ for i in 0..WIDTH {
+ write!(f, "\t{:07b}\n", (self.key() & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
+ }
+ Ok(())
+ }
+}
+
+fn main() {
+ let mut p = Position::new();
+ // println!("{:?}", p);
+ p.play(2);
+ p.play(2);
+ p.play(1);
+ p.play(5);
+ p.play(5);
+ println!("{:?}", p);
+ // p.play(4);
+ // println!("------------------------------");
+ // println!("{:?}", p);
+}