Exemplo n.º 1
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);
        }