Rename State to Board.
This commit is contained in:
parent
5af626e443
commit
92d5bd13f0
60
src/pegp/board.d
Normal file
60
src/pegp/board.d
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
module pegp.main;
|
module pegp.main;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import pegp.state;
|
import pegp.board;
|
||||||
|
|
||||||
int main(string[] args)
|
int main(string[] args)
|
||||||
{
|
{
|
||||||
State s = new State(3);
|
Board s = new Board(3);
|
||||||
writeln("Peg Puzzle");
|
writeln("Peg Puzzle");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user