aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortslil clingman <>2020-01-11 22:42:27 -0500
committertslil clingman <>2020-01-11 22:42:27 -0500
commit46e1af1e694f066fd446f7d48b876f6217463818 (patch)
tree7ca278b1e9c5bf827d1758e93ee98ab1ffd99cc3
Init
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock196
-rw-r--r--Cargo.toml11
-rw-r--r--src/board.rs134
-rw-r--r--src/main.rs157
5 files changed, 500 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..53eaa21
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+**/*.rs.bk
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..fc247a3
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,196 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "arrayvec"
+version = "0.4.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "cc"
+version = "1.0.49"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "lexical-core"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "log"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "memchr"
+version = "2.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "ncurses"
+version = "5.99.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cc 1.0.49 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "nodrop"
+version = "0.1.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "nom"
+version = "5.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "pancurses"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ncurses 5.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pdcurses-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "pdcurses-sys"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cc 1.0.49 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "pkg-config"
+version = "0.3.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "rustc_version"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "ryu"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "semver"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "semver-parser"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "static_assertions"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "takwrap"
+version = "0.1.0"
+dependencies = [
+ "nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pancurses 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "version_check"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winreg"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[metadata]
+"checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
+"checksum cc 1.0.49 (registry+https://github.com/rust-lang/crates.io-index)" = "e450b8da92aa6f274e7c6437692f9f2ce6d701fb73bacfcf87897b3f89a4c20e"
+"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+"checksum lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14"
+"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
+"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
+"checksum ncurses 5.99.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15699bee2f37e9f8828c7b35b2bc70d13846db453f2d507713b758fabe536b82"
+"checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
+"checksum nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c618b63422da4401283884e6668d39f819a106ef51f5f59b81add00075da35ca"
+"checksum pancurses 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d3058bc37c433096b2ac7afef1c5cdfae49ede0a4ffec3dfc1df1df0959d0ff0"
+"checksum pdcurses-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "084dd22796ff60f1225d4eb6329f33afaf4c85419d51d440ab6b8c6f4529166b"
+"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
+"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
+"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
+"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
+"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
+"checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3"
+"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
+"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
+"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+"checksum winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a27a759395c1195c4cc5cda607ef6f8f6498f64e78f7900f5de0a127a424704a"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..09711ac
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "takwrap"
+version = "0.1.0"
+authors = ["tslil clingman"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+pancurses = "0.16.1"
+nom = "5.0.1"
diff --git a/src/board.rs b/src/board.rs
new file mode 100644
index 0000000..45c0d49
--- /dev/null
+++ b/src/board.rs
@@ -0,0 +1,134 @@
+use std::fmt;
+
+#[derive(PartialEq)]
+pub enum Player {
+ Black,
+ White,
+}
+
+impl fmt::Display for Player {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match self {
+ Player::Black => "B",
+ Player::White => "W",
+ }
+ )
+ }
+}
+
+#[derive(PartialEq)]
+pub enum Stone {
+ Flat,
+ Standing,
+ Capstone,
+}
+
+impl fmt::Display for Stone {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match self {
+ Stone::Flat => "",
+ Stone::Standing => "S",
+ Stone::Capstone => "C",
+ }
+ )
+ }
+}
+
+pub struct Position {
+ pub x: u8,
+ pub y: u8,
+}
+impl fmt::Display for Position {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:x}{}", 10 + self.x, self.y)
+ }
+}
+
+pub enum Direction {
+ Up,
+ Down,
+ Left,
+ Right,
+}
+impl fmt::Display for Direction {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match self {
+ Direction::Up => "+",
+ Direction::Down => "-",
+ Direction::Left => "<",
+ Direction::Right => ">",
+ }
+ )
+ }
+}
+
+pub enum Move {
+ Place(Player, Position, Stone),
+ Slide(Player, Position, Direction, Vec<u8>),
+}
+
+impl fmt::Display for Move {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Move::Place(player, pos, stone) => write!(f, "{}{}{}", pos, player, stone),
+ Move::Slide(_, pos, direction, drops) => write!(
+ f,
+ "{}{}{}",
+ pos,
+ direction,
+ drops.into_iter().map(|q| q.to_string()).collect::<String>()
+ ),
+ }
+ }
+}
+
+type Stack = Vec<(Stone, Player)>;
+
+pub struct Board {
+ size: u16,
+ board: Vec<Stack>,
+}
+
+impl Board {
+ fn lookup(&self, pos: &Position) -> Option<&Stack> {
+ let y: u16 = pos.y.into();
+ let x: u16 = pos.x.into();
+ // Really?
+ let idx: usize = (x + y * self.size).into();
+ self.board.get(idx)
+ }
+
+ fn is_legal_move(&self, mov: &Move) -> bool {
+ match mov {
+ Move::Place(_, pos, _) => match self.lookup(pos) {
+ Some(stack) => {
+ if stack.len() == 0 {
+ true
+ } else {
+ false
+ }
+ }
+ None => false,
+ },
+ Move::Slide(player, pos, direction, drops) => match self.lookup(pos) {
+ Some(stack) => {
+ if stack.len() == 0 {
+ true
+ } else {
+ false
+ }
+ }
+ None => false,
+ },
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..82b38af
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,157 @@
+extern crate pancurses;
+
+use pancurses::*;
+use pancurses::Input::*;
+
+mod board;
+
+use crate::board::*;
+
+struct BoardGui {
+ win : Window,
+ size : i32,
+ cell_height : i32,
+ cell_width : i32,
+ hoff : i32,
+ cur_cell : Position
+}
+
+#[derive(PartialEq)]
+enum BGAction { Quit , None }
+
+use std::convert::*;
+
+impl BoardGui {
+ fn mv_to_cell(&self, pos : &Position) {
+ let y:i32 = pos.y.into();
+ let x:i32 = pos.x.into();
+ if (y < self.size) && (x < self.size) {
+ let y = y*self.cell_height+1;
+ let x = self.hoff + x*self.cell_width+1;
+ self.win.mv(y,x);
+ self.win.refresh();
+ }
+ }
+
+ fn draw_grid(&self) {
+ for y in 0..self.cell_height*self.size+1 {
+ for x in 0..self.cell_width*self.size+1 {
+ if y % self.cell_height == 0 {
+ if x % self.cell_width == 0 {
+ self.win.mvaddch(y,x+self.hoff,'+');
+ } else {
+ self.win.mvaddch(y,x+self.hoff,'-');
+ }
+ } else if x % self.cell_width == 0 {
+ self.win.mvaddch(y,x+self.hoff,'|');
+ }
+ }
+ }
+ for y in 0..self.size {
+ self.win.mvaddstr(y*self.cell_height+self.cell_height/2,
+ 0,
+ format!("{}.",self.size-y));
+ }
+ for x in 0..self.size {
+ self.win.mvaddstr(self.size*self.cell_height+1,
+ self.cell_width*x+self.cell_width/2+self.hoff,
+ format!("{:x}.",x+10));
+ }
+ self.win.refresh();
+ }
+
+ fn handle_key(&mut self, key : Input) -> BGAction {
+ match key {
+ KeyUp => {
+ if 0 < self.cur_cell.y {
+ self.cur_cell.y -= 1;
+ self.mv_to_cell(&self.cur_cell);
+ }
+ BGAction::None
+ },
+ KeyDown => {
+ let y:i32 = self.cur_cell.y.into();
+ if y + 1 < self.size {
+ self.cur_cell.y += 1;
+ self.mv_to_cell(&self.cur_cell);
+ }
+ BGAction::None
+ },
+ KeyLeft => {
+ if 0 < self.cur_cell.x {
+ self.cur_cell.x -= 1;
+ self.mv_to_cell(&self.cur_cell);
+ }
+ BGAction::None
+ },
+ KeyRight => {
+ let x:i32 = self.cur_cell.x.into();
+ if x + 1< self.size {
+ self.cur_cell.x += 1;
+ self.mv_to_cell(&self.cur_cell);
+ }
+ BGAction::None
+ },
+ Character('q') => BGAction::Quit,
+ x => {
+ self.win.addstr(format!("{:?}",x));
+ self.win.refresh();
+ BGAction::None
+ }
+ }
+ }
+
+ fn init(&self) {
+ self.draw_grid();
+ self.mv_to_cell(&self.cur_cell);
+ }
+}
+
+struct MoveEntry {
+ win : Window,
+}
+
+impl MoveEntry {
+
+ fn init(&self) {
+ self.win.attrset(COLOR_PAIR(3)|A_BOLD); // ????
+ self.win.mvaddstr(0,0,"Enter move: ashtsaht");
+ // self.win.mvchgat(0,12,10,A_NORMAL,COLOR_PAIR(1));
+ self.win.refresh();
+ }
+}
+
+fn main() {
+ let screen = initscr();
+ screen.nodelay(true);
+ screen.keypad(true);
+
+ if has_colors() { start_color(); }
+
+ cbreak();
+ noecho();
+
+ curs_set(2);
+
+ screen.refresh();
+
+ let bgw = newwin(25,40,0,0);
+ let mew = newwin(1,30,25,0);
+
+ let mut bg = BoardGui { win : bgw, size : 5, cell_height : 4,
+ cell_width : 7, hoff : 3,
+ cur_cell : Position { x : 0, y : 0 }
+ };
+
+ let me = MoveEntry { win : mew };
+
+ bg.init();
+ me.init();
+
+ loop {
+ if let Some(inp) = screen.getch() {
+ if bg.handle_key(inp) == BGAction::Quit {break}
+ }
+ }
+ endwin();
+}