diff --git a/src/pegp/board.d b/src/pegp/board.d new file mode 100644 index 0000000..c0a750d --- /dev/null +++ b/src/pegp/board.d @@ -0,0 +1,60 @@ +module pegp.board; + +class Board +{ + 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(Board b) + { + m_size = b.m_size; + m_pegs = b.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)]; + } + + unittest + { + Board b = new Board(5); + assert(b.valid_position(0, 0)); + assert(b.valid_position(4, 0)); + assert(b.valid_position(4, 4)); + assert(b.valid_position(2, 2)); + assert(b.valid_position(3, 1)); + assert(!b.valid_position(0, 1)); + assert(!b.valid_position(0, -1)); + assert(!b.valid_position(5, 0)); + assert(!b.valid_position(5, 4)); + assert(!b.valid_position(4, 5)); + assert(!b.valid_position(2, 3)); + assert(!b.valid_position(0, 4)); + + assert(b.peg_present(1, 0)); + assert(b.peg_present(1, 1)); + assert(b.peg_present(4, 4)); + assert(!b.peg_present(0, 0)); + } +} diff --git a/src/pegp/main.d b/src/pegp/main.d index 1ce868a..699507b 100644 --- a/src/pegp/main.d +++ b/src/pegp/main.d @@ -1,10 +1,10 @@ module pegp.main; import std.stdio; -import pegp.state; +import pegp.board; int main(string[] args) { - State s = new State(3); + Board s = new Board(3); writeln("Peg Puzzle"); return 0; } diff --git a/src/pegp/state.d b/src/pegp/state.d deleted file mode 100644 index 9c77dad..0000000 --- a/src/pegp/state.d +++ /dev/null @@ -1,60 +0,0 @@ -module pegp.state; - -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)]; - } - - unittest - { - State s = new State(5); - assert(s.valid_position(0, 0)); - assert(s.valid_position(4, 0)); - assert(s.valid_position(4, 4)); - assert(s.valid_position(2, 2)); - assert(s.valid_position(3, 1)); - assert(!s.valid_position(0, 1)); - assert(!s.valid_position(0, -1)); - assert(!s.valid_position(5, 0)); - assert(!s.valid_position(5, 4)); - assert(!s.valid_position(4, 5)); - assert(!s.valid_position(2, 3)); - assert(!s.valid_position(0, 4)); - - assert(s.peg_present(1, 0)); - assert(s.peg_present(1, 1)); - assert(s.peg_present(4, 4)); - assert(!s.peg_present(0, 0)); - } -}