예제 #1
0
        public void TestCalculateMove2MinimaxPlayerY()
        {
            HexBoard board = new HexBoard(3);

            PlayTwoMoves(board);

            Minimax       minimax            = MakeMinimaxForBoard(board);
            MinimaxResult secondPlayerResult = minimax.DoMinimax(2, false);

            Location expectedPlay = new Location(0, 2);

            Assert.AreEqual(expectedPlay, secondPlayerResult.Move, "Wrong play location");
            Assert.IsFalse(MoveScoreConverter.IsWin(secondPlayerResult.Score));
        }
예제 #2
0
        public void TestMinimax3()
        {
            HexBoard board   = new HexBoard(5);
            Minimax  minimax = MakeMinimaxForBoard(board);

            /*
             *  on a 5 * 5 board, red(playerx) has 3, 0 and 1, 4
             *  needs to play 2,2 to win - should know this at look ahead 5
             */

            board.PlayMove(3, 0, true);
            board.PlayMove(1, 4, true);

            MinimaxResult bestMove     = minimax.DoMinimax(5, true);
            Location      expectedMove = new Location(2, 2);

            Assert.IsTrue(MoveScoreConverter.IsWin(bestMove.Score), "No win " + bestMove.Score);
            Assert.AreEqual(expectedMove, bestMove.Move, "Wrong expected move");
        }
예제 #3
0
        private Location GetBestMove(MinimaxResult playResult)
        {
            Location result = playResult.Move;

            int moveScore = playResult.Score;

            this.IsGameWon = MoveScoreConverter.IsWin(moveScore) && MoveScoreConverter.WinDepth(moveScore) == 1;

            Occupied opponent   = (!this.hexGame.PlayerX).ToPlayer();
            bool     losingMove = MoveScoreConverter.Winner(playResult.Score) == opponent;

            if (losingMove)
            {
                Location losingLocation = this.MakeLosingMove();
                if (losingLocation != Location.Null)
                {
                    result = losingLocation;
                }
            }

            return(result);
        }
예제 #4
0
 private static void AssertWinner(int score, Occupied winner)
 {
     Assert.IsTrue(MoveScoreConverter.IsWin(score), "Should have winner");
     Assert.AreEqual(winner, MoveScoreConverter.Winner(score), "Wrong winner");
 }
예제 #5
0
        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);
        }
예제 #6
0
        /// <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);
        }