Board: add remove_peg(), add_peg(), move().
This commit is contained in:
parent
7235cd906f
commit
22a8a3b94a
@ -75,6 +75,31 @@ class Board
|
|||||||
return peg_present(p.row, p.col);
|
return peg_present(p.row, p.col);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void remove_peg(Position p)
|
||||||
|
{
|
||||||
|
m_pegs[peg_index(p.row, p.col)] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_peg(Position p)
|
||||||
|
{
|
||||||
|
m_pegs[peg_index(p.row, p.col)] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Board move(Position p, Direction d)
|
||||||
|
{
|
||||||
|
Position p1 = p.move(d, 1);
|
||||||
|
Position p2 = p.move(d, 2);
|
||||||
|
if (peg_present(p) && position_valid(p2) && peg_present(p1) && !peg_present(p2))
|
||||||
|
{
|
||||||
|
Board b2 = new Board(this);
|
||||||
|
b2.remove_peg(p);
|
||||||
|
b2.remove_peg(p1);
|
||||||
|
b2.add_peg(p2);
|
||||||
|
return b2;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
Board b = new Board(5);
|
Board b = new Board(5);
|
||||||
@ -121,5 +146,14 @@ class Board
|
|||||||
assert(!b.position_valid(s.move(Direction.E, 2)));
|
assert(!b.position_valid(s.move(Direction.E, 2)));
|
||||||
assert(b.position_valid(s.move(Direction.SW, 2)));
|
assert(b.position_valid(s.move(Direction.SW, 2)));
|
||||||
assert(b.position_valid(s.move(Direction.SE, 2)));
|
assert(b.position_valid(s.move(Direction.SE, 2)));
|
||||||
|
|
||||||
|
Board b2;
|
||||||
|
b2 = b.move(Position(0, 0), Direction.SW);
|
||||||
|
assert(b2 is null);
|
||||||
|
b2 = b.move(Position(2, 0), Direction.NE);
|
||||||
|
assert(b2 !is null);
|
||||||
|
assert(!b2.peg_present(2, 0));
|
||||||
|
assert(!b2.peg_present(1, 0));
|
||||||
|
assert(b2.peg_present(0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user