Add Board.peg_count.
This commit is contained in:
parent
22a8a3b94a
commit
d6a3de2fd0
@ -3,6 +3,7 @@ module pegp.board;
|
|||||||
class Board
|
class Board
|
||||||
{
|
{
|
||||||
private int m_size;
|
private int m_size;
|
||||||
|
private int m_peg_count;
|
||||||
private bool[] m_pegs;
|
private bool[] m_pegs;
|
||||||
|
|
||||||
enum Direction
|
enum Direction
|
||||||
@ -42,12 +43,19 @@ class Board
|
|||||||
m_pegs = new bool[n_pegs];
|
m_pegs = new bool[n_pegs];
|
||||||
m_pegs[] = true;
|
m_pegs[] = true;
|
||||||
m_pegs[0] = false;
|
m_pegs[0] = false;
|
||||||
|
m_peg_count = n_pegs - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
this(Board b)
|
this(Board b)
|
||||||
{
|
{
|
||||||
m_size = b.m_size;
|
m_size = b.m_size;
|
||||||
m_pegs = b.m_pegs.dup;
|
m_pegs = b.m_pegs.dup;
|
||||||
|
m_peg_count = b.m_peg_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property int peg_count()
|
||||||
|
{
|
||||||
|
return m_peg_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int peg_index(int row, int col)
|
private int peg_index(int row, int col)
|
||||||
@ -78,11 +86,13 @@ class Board
|
|||||||
void remove_peg(Position p)
|
void remove_peg(Position p)
|
||||||
{
|
{
|
||||||
m_pegs[peg_index(p.row, p.col)] = false;
|
m_pegs[peg_index(p.row, p.col)] = false;
|
||||||
|
m_peg_count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_peg(Position p)
|
void add_peg(Position p)
|
||||||
{
|
{
|
||||||
m_pegs[peg_index(p.row, p.col)] = true;
|
m_pegs[peg_index(p.row, p.col)] = true;
|
||||||
|
m_peg_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Board move(Position p, Direction d)
|
Board move(Position p, Direction d)
|
||||||
@ -103,6 +113,7 @@ class Board
|
|||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
Board b = new Board(5);
|
Board b = new Board(5);
|
||||||
|
assert(b.peg_count == 14);
|
||||||
assert(b.position_valid(Position(0, 0)));
|
assert(b.position_valid(Position(0, 0)));
|
||||||
assert(b.position_valid(4, 0));
|
assert(b.position_valid(4, 0));
|
||||||
assert(b.position_valid(4, 4));
|
assert(b.position_valid(4, 4));
|
||||||
@ -155,5 +166,6 @@ class Board
|
|||||||
assert(!b2.peg_present(2, 0));
|
assert(!b2.peg_present(2, 0));
|
||||||
assert(!b2.peg_present(1, 0));
|
assert(!b2.peg_present(1, 0));
|
||||||
assert(b2.peg_present(0, 0));
|
assert(b2.peg_present(0, 0));
|
||||||
|
assert(b2.peg_count == 13);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user