From d53cd1d0ad4f729f50e3e15e330c10e10559c671 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 9 Feb 2020 22:18:15 -0500 Subject: [PATCH] start on State class --- Rsconscript | 1 + src/main.d | 2 ++ src/state.d | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/state.d diff --git a/Rsconscript b/Rsconscript index 433bebf..054b5e0 100644 --- a/Rsconscript +++ b/Rsconscript @@ -4,6 +4,7 @@ end build do Environment.new do |env| + env["D_IMPORT_PATH"] << "src" env.Program("peg-puzzle", glob("src/**/*.d")) end end diff --git a/src/main.d b/src/main.d index ed9b871..e0cdd77 100644 --- a/src/main.d +++ b/src/main.d @@ -1,7 +1,9 @@ import std.stdio; +import state; int main(string[] args) { + State s = new State(3); writeln("Peg Puzzle"); return 0; } diff --git a/src/state.d b/src/state.d new file mode 100644 index 0000000..50a9b5e --- /dev/null +++ b/src/state.d @@ -0,0 +1,36 @@ +class State +{ + private int m_size; + private bool[] m_pegs; + + this(int size) + { + assert(size >= 3); + m_size = size; + const int n_pegs = (size * (size + 1)) / 2; + m_pegs = new bool[n_pegs]; + m_pegs[] = true; + m_pegs[0] = false; + } + + this(State s) + { + m_size = s.m_size; + m_pegs = s.m_pegs.dup; + } + + private int peg_index(int row, int col) + { + return (row * (row + 1)) / 2 + col; + } + + bool valid_position(int row, int col) + { + return (row >= 0) && (row < m_size) && (col >= 0) && (col <= row); + } + + bool peg_present(int row, int col) + { + return m_pegs[peg_index(row, col)]; + } +}