Beispiel #1
0
        public int Minimax(GameBoard board, int depth, int alpha, int beta, bool maximizingPlayer, CancellationToken cancellationToken)
        {
            int eval;
            int maxEval;
            int minEval;

            if (Rules.EndGame(board) || depth == 0)
            {
                return(HeuristicEvaluation(board, maximizingPlayer));
            }
            else if (maximizingPlayer)
            {
                List <List <int> > allPlayerMoves = AllPlayerMoves(board, (int)GameConstants.PlayerColor.Black);
                maxEval = Min;

                foreach (List <int> move in allPlayerMoves)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(0);
                    }

                    board.MakeMove(move, board);
                    eval    = Minimax(board, depth - 1, alpha, beta, false, cancellationToken);
                    board   = UndoMove(board, move);
                    maxEval = Math.Max(maxEval, eval);
                    alpha   = Math.Max(alpha, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(maxEval);
            }
            else
            {
                minEval = Max;
                List <List <int> > allPlayerMoves = AllPlayerMoves(board, (int)GameConstants.PlayerColor.White);

                foreach (List <int> move in allPlayerMoves)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(0);
                    }

                    board.MakeMove(move, board);
                    eval    = Minimax(board, depth - 1, alpha, beta, true, cancellationToken);
                    board   = UndoMove(board, move);
                    minEval = Math.Min(minEval, eval);
                    beta    = Math.Min(beta, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(minEval);
            }
        }
Beispiel #2
0
 public async Task <bool> TryToPlay(GameBoard board, CancellationTokenSource source)
 {
     if (IsAI)
     {
         var myTask = Task.Run(() => AIPlay(board, source));
         PlayerMove = await myTask;
         if (!source.IsCancellationRequested)
         {
             board.MakeMove(PlayerMove, board);
             if (PlayerMove[(int)GameConstants.MoveParts.result] != (int)GameConstants.MoveResult.Fail)
             {
                 Finished = true;
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
     else
     {
         // zahraje tah a pokud výsledkem tahu není FAIL ukončí tah hráče
         board.MakeMove(PlayerMove, board);
         if (PlayerMove[(int)GameConstants.MoveParts.result] != (int)GameConstants.MoveResult.Fail)
         {
             Finished = true;
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Beispiel #3
0
        public List <int> SmartMove(GameBoard board, int playerColor, int IQ, CancellationToken cancellationToken)
        {
            int       depth     = IQ;
            GameBoard boardCopy = board;

            List <List <int> > allPlayerMoves = AllPlayerMoves(board, playerColor);

            int bestEval;

            if (MaximizingPlayer(playerColor))
            {
                bestEval = Min;
            }
            else
            {
                bestEval = Max;
            }

            Shuffle(allPlayerMoves);

            List <int> bestMove = allPlayerMoves[0];

            foreach (List <int> move in allPlayerMoves)
            {
                boardCopy.MakeMove(move, boardCopy);
                int eval = Minimax(boardCopy, depth, Min, Max, MaximizingPlayer(playerColor), cancellationToken);

                if (MaximizingPlayer(playerColor) && eval > bestEval)
                {
                    bestEval = eval;
                    bestMove = move;
                }
                if (!MaximizingPlayer(playerColor) && eval < bestEval)
                {
                    bestEval = eval;
                    bestMove = move;
                }

                UndoMove(board, move);
            }

            //Console.WriteLine();
            //Console.WriteLine("Nejlepší ohodnocení: " + bestEval);

            return(bestMove);
        }