/// <summary> /// public wrapper for recursion /// try each possible move, find the one with the best score /// </summary> /// <param name="lookahead">how far to look ahead</param> /// <param name="playerX">is this for player x</param> /// <returns>The data on the best move location and score</returns> public MinimaxResult DoMinimax(int lookahead, bool playerX) { // set up inital state DateTime startTime = DateTime.Now; if (lookahead < 1) { throw new Exception("Invalid lookahead of " + lookahead); } this.debugDataItems.Clear(); Occupied player = playerX.ToPlayer(); int alpha = MoveScoreConverter.ConvertWin(player.Opponent(), 0); int beta = MoveScoreConverter.ConvertWin(player, 0); MinimaxResult bestMove = this.ScoreBoard(lookahead, this.ActualBoard, playerX, alpha, beta); if (bestMove.Move != Location.Null) { GoodMoves.AddGoodMove(0, bestMove.Move); } DateTime endTime = DateTime.Now; this.MoveTime = endTime - startTime; return(bestMove); }
public Minimax(HexBoard board, GoodMoves goodMoves, ICandidateMoves candidateMovesFinder) { this.actualBoard = board; this.goodMoves = goodMoves; this.candidateMovesFinder = candidateMovesFinder; this.boardCache = new BoardCache(board.Size); this.pathLengthFactory = new PathLengthAStarFactory(); }
/// <summary> /// Initializes a new instance of the HexGame class, with a board size /// </summary> /// <param name="boardSize">size of the board</param> public HexGame(int boardSize) { this.board = new HexBoard(boardSize); IPathLengthFactory pathLengthFactory = new PathLengthAStarFactory(); this.xPathLength = pathLengthFactory.CreatePathLength(this.board); this.yPathLength = pathLengthFactory.CreatePathLength(this.board); this.goodMoves = new GoodMoves(); this.goodMoves.DefaultGoodMoves(boardSize, 5); }
public Minimax(HexBoard board, GoodMoves goodMoves, ICandidateMoves candidateMovesFinder) { this.board = board; this.depth = 0; this.candidateMovesFinder = candidateMovesFinder; this.pathLengthFactory = new PathLengthAStarFactory(); GoodMoves = goodMoves; }
public void CountOneMoveTest() { GoodMoves goods = new GoodMoves(); CandidateMovesSelective moveFinder = new CandidateMovesSelective(goods, 0); HexBoard testBoard = new HexBoard(BoardSize); testBoard.PlayMove(5, 5, true); IEnumerable<Location> moves = moveFinder.CandidateMoves(testBoard, 0); Assert.Greater(BoardCellCount - 1, moves.Count()); }
public MinimaxResult DoMinimax(int depth, bool isComputer) { // megmondja, hogy ki a jatekos, ha a gep, akkor az isComputerben true van, es akkor a plyaerben xPlayer lesz. Occupied player = isComputer.ToPlayer(); // a gep az alpha this.alpha = MoveScoreConverter.ConvertWin(player.Opponent(), 0); // ember a beta this.beta = MoveScoreConverter.ConvertWin(player, 0); // a minimax algoritmus magaban MinimaxResult bestMove = this.MiniMaxAlg(depth, isComputer, board); if (bestMove.Move != Location.Null) { // killer heurisztika GoodMoves.AddGoodMove(0, bestMove.Move); } return(bestMove); }
public CandidateMovesSelective(GoodMoves goodMoves, int cellsPlayedCount) { this.goodMoves = goodMoves; this.cellsPlayedCount = cellsPlayedCount; }
private static Minimax MakeMinimaxForBoard(HexBoard board) { GoodMoves goodMoves = new GoodMoves(); ICandidateMoves candidateMovesFinder = new CandidateMovesAll(); Minimax result = new Minimax(board, goodMoves, candidateMovesFinder); result.GenerateDebugData = true; return result; }
public void TestCreate() { GoodMoves goods = new GoodMoves(); CandidateMovesSelective moveFinder = new CandidateMovesSelective(goods, 0); Assert.IsNotNull(moveFinder); }
private MinimaxResult MiniMaxAlg(int depth, bool isComputer, HexBoard board) { BoardCache boardCache = new BoardCache(board.Size); MinimaxResult bestResult = null; Location cutOffMove = Location.Null; // az ures cellakat tartalmazza var possibleMoves = candidateMovesFinder.CandidateMoves(board, depth); foreach (Location move in possibleMoves) { if (!move.IsNull()) { // nem az eredetit modositom meg, hanem letrehozok egyet a peldajara HexBoard board1 = new HexBoard(board.Size); board1.CopyStateFrom(board); board1.PlayMove(move, isComputer); // itt szamolja ki az allast PathLengthBase staticAnalysis = this.pathLengthFactory.CreatePathLength(board1); int situationScore = staticAnalysis.SituationScore(); MinimaxResult moveScore = new MinimaxResult(situationScore); if (depth <= 1 || MoveScoreConverter.IsWin(situationScore)) { moveScore = new MinimaxResult(situationScore); } else { if (depth > 1) { // rekurzio moveScore = MiniMaxAlg(depth--, !isComputer, board1); moveScore.MoveWins(); } } moveScore.Move = move; // Itt nezem meg, hogy a minimum kell nekunk, vagy a maximum if (bestResult == null || MoveScoreConverter.MinOrMax(moveScore.Score, bestResult.Score, isComputer)) { bestResult = new MinimaxResult(move, moveScore); } alpha = CheckAlpha(moveScore.Score, isComputer); if (IsAlphaBetaCutoff(isComputer)) { cutOffMove = move; bestResult.Score = alpha; break; } } else { break; } } if (bestResult != null) { GoodMoves.AddGoodMove(depth, bestResult.Move); } if (cutOffMove != Location.Null) { GoodMoves.AddGoodMove(depth, cutOffMove); } return(bestResult); }
public void TearDown() { this.goodMoves = null; }
public void SetUp() { this.goodMoves = new GoodMoves(); }
/// <summary> /// private recursive worker - does the minimax algorithm /// </summary> /// <param name="lookahead">the current ply, counts down to zero</param> /// <param name="stateBoard">the current board</param> /// <param name="isPlayerX">player X or player Y</param> /// <param name="alpha">alpha value used in alpha-beta pruning</param> /// <param name="beta">beta value used in alpha-beta pruning</param> /// <returns>the score of the board and best move location</returns> private MinimaxResult ScoreBoard( int lookahead, HexBoard stateBoard, bool isPlayerX, int alpha, int beta) { this.CountBoards++; MinimaxResult bestResult = null; Location cutoffMove = Location.Null; var possibleMoves = this.candidateMovesFinder.CandidateMoves(stateBoard, lookahead); foreach (Location move in possibleMoves) { // end on null loc if (move.IsNull()) { break; } if (this.GenerateDebugData) { this.AddDebugDataItem(lookahead, move, isPlayerX, alpha, beta); } // make a speculative board, like the current, but with this cell played HexBoard testBoard = this.boardCache.GetBoard(); testBoard.CopyStateFrom(stateBoard); testBoard.PlayMove(move, isPlayerX); MinimaxResult moveScore; PathLengthBase staticAnalysis = this.pathLengthFactory.CreatePathLength(testBoard); int situationScore = staticAnalysis.SituationScore(); if (lookahead <= 1) { // we have reached the limits of lookahead - return the situation score moveScore = new MinimaxResult(situationScore); } else if (MoveScoreConverter.IsWin(situationScore)) { // stop - someone has won moveScore = new MinimaxResult(situationScore); } else { // recurse moveScore = this.ScoreBoard(lookahead - 1, testBoard, !isPlayerX, beta, alpha); moveScore.MoveWins(); } moveScore.Move = move; this.boardCache.Release(testBoard); // higher scores are good for player x, lower scores for player y if (bestResult == null || MoveScoreConverter.IsBetterFor(moveScore.Score, bestResult.Score, isPlayerX)) { bestResult = new MinimaxResult(move, moveScore); } // do the alpha-beta pruning alpha = CheckAlpha(alpha, moveScore.Score, isPlayerX); if (IsAlphaBetaCutoff(isPlayerX, alpha, beta)) { cutoffMove = move; bestResult.Score = alpha; break; } // end a-b pruning } if (bestResult != null) { GoodMoves.AddGoodMove(lookahead, bestResult.Move); } if (cutoffMove != Location.Null) { GoodMoves.AddGoodMove(lookahead, cutoffMove); } return(bestResult); }
public void TestCreate() { GoodMoves goods = new GoodMoves(); CandidateMovesAllSorted moves = new CandidateMovesAllSorted(goods, 0); Assert.IsNotNull(moves); }
public CandidateMovesAllSorted(GoodMoves goodMoves, int cellsPlayedCount) { this.goodMoves = goodMoves; this.cellsPlayedCount = cellsPlayedCount; }