Rename State to Board.

This commit is contained in:
Josh Holtrop 2021-02-21 19:52:11 -05:00
parent 5af626e443
commit 92d5bd13f0
3 changed files with 62 additions and 62 deletions

60
src/pegp/board.d Normal file
View File

@ -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));
}
}

View File

@ -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;
}

View File

@ -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));
}
}