Flesh out Solver a bit more.
Something is definitely broken though...
This commit is contained in:
parent
cf730e93a8
commit
6add626470
@ -1,6 +1,15 @@
|
||||
import std.stdio;
|
||||
import pegp.solver;
|
||||
|
||||
int main(string[] args)
|
||||
{
|
||||
version (unittest)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
Solver s = new Solver(3);
|
||||
s.solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -9,11 +9,13 @@ class Board
|
||||
enum Direction
|
||||
{
|
||||
NW,
|
||||
FIRST = NW,
|
||||
NE,
|
||||
W,
|
||||
E,
|
||||
SW,
|
||||
SE,
|
||||
COUNT,
|
||||
}
|
||||
|
||||
struct Position
|
||||
|
@ -1,17 +1,112 @@
|
||||
module pegp.solver;
|
||||
|
||||
import pegp.board;
|
||||
import std.stdio;
|
||||
|
||||
class Solver
|
||||
{
|
||||
Board m_board;
|
||||
class Move
|
||||
{
|
||||
Board.Position m_source;
|
||||
Board.Position m_dest;
|
||||
|
||||
this(Board.Position source, Board.Position dest)
|
||||
{
|
||||
m_source = source;
|
||||
m_dest = dest;
|
||||
}
|
||||
|
||||
@property Board.Position source()
|
||||
{
|
||||
return m_source;
|
||||
}
|
||||
|
||||
@property Board.Position dest()
|
||||
{
|
||||
return m_dest;
|
||||
}
|
||||
}
|
||||
|
||||
int m_size;
|
||||
int m_game_count;
|
||||
int m_1_peg_game_count;
|
||||
int m_1_peg_corner_game_count;
|
||||
Move[] m_1_peg_corner_moves;
|
||||
|
||||
this(int size)
|
||||
{
|
||||
m_board = new Board(size);
|
||||
m_size = size;
|
||||
}
|
||||
|
||||
static void solve()
|
||||
void solve()
|
||||
{
|
||||
Board board = new Board(m_size);
|
||||
Move[] moves;
|
||||
eval_board(board, moves);
|
||||
report();
|
||||
}
|
||||
|
||||
void eval_board(Board board, Move[] moves)
|
||||
{
|
||||
bool found_valid_move = false;
|
||||
for (int row = 0; row < m_size; row++)
|
||||
{
|
||||
for (int col = 0; col < row; col++)
|
||||
{
|
||||
for (Board.Direction direction = Board.Direction.FIRST;
|
||||
direction < Board.Direction.COUNT;
|
||||
direction++)
|
||||
{
|
||||
Board.Position position = Board.Position(row, col);
|
||||
Board new_board = board.move(position, direction);
|
||||
if (new_board !is null)
|
||||
{
|
||||
found_valid_move = true;
|
||||
Move m = new Move(position, position.move(direction, 2));
|
||||
Move[] new_moves = moves.dup;
|
||||
new_moves ~= m;
|
||||
eval_board(new_board, new_moves);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found_valid_move)
|
||||
{
|
||||
end_game(board, moves);
|
||||
}
|
||||
}
|
||||
|
||||
void end_game(Board board, Move[] moves)
|
||||
{
|
||||
m_game_count++;
|
||||
if (board.peg_count == 1)
|
||||
{
|
||||
m_1_peg_game_count++;
|
||||
if (board.peg_present(0, 0) ||
|
||||
board.peg_present(m_size - 1, 0) ||
|
||||
board.peg_present(m_size - 1, m_size - 1))
|
||||
{
|
||||
m_1_peg_corner_game_count++;
|
||||
if (m_1_peg_corner_moves is null)
|
||||
{
|
||||
m_1_peg_corner_moves = moves;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void report()
|
||||
{
|
||||
writeln("Played ", m_game_count, " games.");
|
||||
writeln(m_1_peg_game_count, " of them had 1 peg left.");
|
||||
writeln(m_1_peg_corner_game_count, " of them had the 1 peg left in a corner.");
|
||||
if (m_1_peg_corner_moves !is null)
|
||||
{
|
||||
writeln("Here is one such move sequence:");
|
||||
foreach (move; m_1_peg_corner_moves)
|
||||
{
|
||||
writeln(" ", move.source, " -> ", move.dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user