public static Tuple <int, int> GetBlackBestMove(ReversiBoard board)
        {
            var availableMoves = board.GetAvailableMoves(Player.Black);

            if (!availableMoves.Any())
            {
                return(null);
            }

            var bestMove  = availableMoves.First();
            var bestScore = int.MinValue;
            var depth     = 0;

            foreach (var availableMove in availableMoves)
            {
                var moveRow    = availableMove.Item1;
                var moveColumn = availableMove.Item2;
                var boardCopy  = board.Clone();
                boardCopy.PlayMove(moveRow, moveColumn, Player.Black);
                var score = MaxPlay(boardCopy, depth + 1);
                if (score > bestScore)
                {
                    bestMove  = availableMove;
                    bestScore = score;
                }
            }

            return(new Tuple <int, int>(bestMove.Item1, bestMove.Item2));
        }
        static void Main(string[] args)
        {
            var board = new ReversiBoard();

            board.PrintBoard();
            while (!board.HasGameFinished())
            {
                Console.WriteLine("White, Play one of moves:");
                var whiteMoves = board.GetAvailableMoves(Player.White);
                if (whiteMoves.Any())
                {
                    foreach (var whiteMove in whiteMoves)
                    {
                        Console.WriteLine($"{whiteMove.Item1} {whiteMove.Item2}");
                    }
                    int row    = int.Parse(Console.ReadLine());
                    int column = int.Parse(Console.ReadLine());
                    board.PlayMove(row, column, Player.White);
                    board.PrintBoard();
                }

                var blackMove = MiniMax.GetBlackBestMove(board);
                if (blackMove != null)
                {
                    var blackMoves = board.GetAvailableMoves(Player.Black);
                    Console.WriteLine("Possible black moves:");
                    foreach (var bm in blackMoves)
                    {
                        Console.WriteLine($"{bm.Item1} {bm.Item2}");
                    }
                    Console.WriteLine($"Black played {blackMove.Item1} {blackMove.Item2}");
                    board.PlayMove(blackMove.Item1, blackMove.Item2, Player.Black);
                    board.PrintBoard();
                }
            }

            Console.WriteLine($"GAME ENDED: {board.EvaluateScore()}");
        }
        private static int MinPlay(ReversiBoard board, int depth)
        {
            if (board.HasGameFinished() || depth == MaxDepthPerMove)
            {
                return(board.EvaluateScore());
            }

            var availableMoves = board.GetAvailableMoves(Player.White);
            var bestScore      = int.MaxValue;

            foreach (var availableMove in availableMoves)
            {
                var boardCopy = board.Clone();
                boardCopy.PlayMove(availableMove.Item1, availableMove.Item2, Player.White);
                var score = MaxPlay(boardCopy, depth + 1);
                if (score < bestScore)
                {
                    bestScore = score;
                }
            }

            return(bestScore);
        }
Beispiel #4
0
 private ReversiBoard(ReversiBoard board)
 {
     _state = board._state.Clone() as SquareState[, ];
 }