From 6add626470333fec017a580cc8fdcc8a489359de Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 28 Feb 2021 14:41:26 -0500 Subject: [PATCH] Flesh out Solver a bit more. Something is definitely broken though... --- src/main.d | 9 +++++ src/pegp/board.d | 2 + src/pegp/solver.d | 101 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/main.d b/src/main.d index 6f84c44..450cb4f 100644 --- a/src/main.d +++ b/src/main.d @@ -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; } diff --git a/src/pegp/board.d b/src/pegp/board.d index 4bd59b0..9d60e5b 100644 --- a/src/pegp/board.d +++ b/src/pegp/board.d @@ -9,11 +9,13 @@ class Board enum Direction { NW, + FIRST = NW, NE, W, E, SW, SE, + COUNT, } struct Position diff --git a/src/pegp/solver.d b/src/pegp/solver.d index cca4068..eb7603a 100644 --- a/src/pegp/solver.d +++ b/src/pegp/solver.d @@ -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); + } + } } }