Beispiel #1
0
        /// <summary>
        /// Wykonujemy symulacje
        /// </summary>
        /// <returns></returns>
        private double Rollout(MctsNode node, int numberOfLastKingMovesWithoutBeat)
        {
            int           result = 0;
            CheckersBoard board  = node.Board;
            PieceColor    color  = node.Color;

            while (true)
            {
                var moves = board.GetAllPossibleMoves(color);
                if (moves.Count > 0)
                {
                    var move = moves[RandomGenerator.Next(0, moves.Count - 1)];
                    board = board.GetBoardAfterMove(move);
                    if (move.OldPiece.IsKing && (move.BeatedPieces == null || move.BeatedPieces.Count == 0))
                    {
                        numberOfLastKingMovesWithoutBeat++;
                    }
                    else
                    {
                        numberOfLastKingMovesWithoutBeat = 0;
                    }
                    if (numberOfLastKingMovesWithoutBeat > 49)
                    {
                        return(0.5);
                    }
                }
                else
                {
                    result = color == PieceColor.Black ? 1 : -1;
                    break;
                }
                color = color == PieceColor.Black ? PieceColor.White : PieceColor.Black;
            }
            return(result);
        }
Beispiel #2
0
        public Move MakeMove(CheckersBoard currentBoard, GameVariant variant, List <Move> gameMoves)
        {
            Random      random           = new Random();
            List <Move> allPossibleMoves = currentBoard.GetAllPossibleMoves(Color);
            int         count            = allPossibleMoves.Count;

            if (count == 0)
            {
                throw new NotAvailableMoveException(Color);
            }
            int elemIndex = MakeMoveGpu((char)currentBoard.Size, Color == PieceColor.White ? 0 : 1, currentBoard.GetBoardArray(), GetPossibleMovesArray(allPossibleMoves), MctsIterationCount, GridSize, BlockSize, variant == GameVariant.Checkers ? 0 : 1);          return(allPossibleMoves[elemIndex]);
        }
Beispiel #3
0
        public void Test4()
        {
            CheckersBoard board = new CheckersBoard(10, new List <Piece>()
            {
                new Piece(3, 3, PieceColor.White, 10, true),
                new Piece(1, 1, PieceColor.Black, 10, false),
                new Piece(8, 8, PieceColor.Black, 10, false)
            });
            var moves = board.GetAllPossibleMoves(PieceColor.White);

            Assert.IsTrue(moves.Where(m => m.BeatedPieces?.Count == 1).Count() == 2);
        }
Beispiel #4
0
        public Move MakeMove(CheckersBoard currentBoard, GameVariant variant, List <Move> gameMoves)
        {
            List <Move> allPossibleMoves = currentBoard.GetAllPossibleMoves(Color);
            int         count            = allPossibleMoves.Count;

            if (count == 0)
            {
                throw new NotAvailableMoveException(Color);
            }
            int elemIndex = randomGenerator.Next(count);

            return(allPossibleMoves[elemIndex]);
        }
Beispiel #5
0
        public void Test1()
        {
            CheckersBoard board = new CheckersBoard(10, new List <Piece>()
            {
                new Piece(4, 4, PieceColor.White, 10, true),
                new Piece(6, 6, PieceColor.Black, 10, false),
                new Piece(7, 7, PieceColor.Black, 10, false),
                new Piece(8, 8, PieceColor.Black, 10, false)
            });
            var moves = board.GetAllPossibleMoves(PieceColor.White);;

            Assert.IsTrue(moves.Where(m => m.BeatedPieces == null).Count() > 0);
        }
Beispiel #6
0
        public void Test10()
        {
            CheckersBoard board = new CheckersBoard(10, new List <Piece>()
            {
                new Piece(9, 5, PieceColor.White, 10, true),
                new Piece(8, 4, PieceColor.Black, 10, true),
                new Piece(6, 4, PieceColor.Black, 10, true),
                new Piece(8, 6, PieceColor.Black, 10, true),
                new Piece(6, 6, PieceColor.Black, 10, true)
            });
            var moves = board.GetAllPossibleMoves(PieceColor.White);

            Assert.IsTrue(moves.Where(m => m.BeatedPieces?.Count == 4).Count() > 0);
        }
Beispiel #7
0
        public void Test5()
        {
            CheckersBoard board = new CheckersBoard(10, new List <Piece>()
            {
                new Piece(1, 1, PieceColor.White, 10, true),
                new Piece(3, 3, PieceColor.Black, 10, false),
                new Piece(2, 4, PieceColor.Black, 10, false),
                new Piece(2, 6, PieceColor.Black, 10, false),
                new Piece(2, 8, PieceColor.Black, 10, false),
                new Piece(4, 8, PieceColor.Black, 10, false),
                new Piece(8, 2, PieceColor.Black, 10, false),
                new Piece(8, 4, PieceColor.Black, 10, false),
                new Piece(6, 6, PieceColor.Black, 10, false)
            });
            var moves = board.GetAllPossibleMoves(PieceColor.White);

            Assert.IsTrue(moves.Where(m => m.BeatedPieces?.Count == 7).Count() > 0 && moves.Where(m => m.BeatedPieces.Last().Row == 4 || m.BeatedPieces.Last().Column == 8).Count() == 0);
        }
Beispiel #8
0
        public void Test8()
        {
            CheckersBoard board = new CheckersBoard(10, new List <Piece>()
            {
                new Piece(2, 2, PieceColor.White, 10, false),
                new Piece(3, 3, PieceColor.Black, 10, false),
                new Piece(2, 4, PieceColor.Black, 10, false),
                new Piece(3, 5, PieceColor.Black, 10, false),
                new Piece(2, 8, PieceColor.Black, 10, false),
                new Piece(1, 5, PieceColor.Black, 10, false),
                new Piece(3, 7, PieceColor.Black, 10, false),
                new Piece(7, 3, PieceColor.Black, 10, false),
                new Piece(7, 5, PieceColor.Black, 10, false),
                new Piece(5, 7, PieceColor.Black, 10, false)
            });
            var moves = board.GetAllPossibleMoves(PieceColor.White);

            Assert.IsTrue(moves.Where(m => m.BeatedPieces?.Count == 6).Count() > 0);
        }
Beispiel #9
0
        public Move MakeMove(CheckersBoard currentBoard, GameVariant variant, List <Move> gameMoves)
        {
            List <Move> allPossibleMoves = currentBoard.GetAllPossibleMoves(Color);
            int         count            = allPossibleMoves.Count;

            if (count == 0)
            {
                throw new NotAvailableMoveException(Color);
            }
            if (count == 1)
            {
                return(allPossibleMoves.First());
            }
            else
            {
                AlphaBetaTree tree      = new AlphaBetaTree(AlphaBetaTreeDepth, Color, currentBoard);
                int           elemIndex = tree.ChooseBestMove(variant, gameMoves);
                return(allPossibleMoves[elemIndex]);
            }
        }
Beispiel #10
0
        public Move MakeMove(CheckersBoard currentBoard, GameVariant variant, List <Move> gameMoves)
        {
            List <Move> allPossibleMoves = currentBoard.GetAllPossibleMoves(Color);
            int         count            = allPossibleMoves.Count;

            if (count == 0)
            {
                throw new NotAvailableMoveException(Color);
            }
            if (count == 1)
            {
                return(allPossibleMoves.First());
            }
            else
            {
                MctsTree tree      = new MctsTree(NumberOfIterations, UctParameter, randomGenerator, currentBoard, Color);
                int      elemIndex = tree.ChooseBestMove(variant, gameMoves);
                return(allPossibleMoves[elemIndex]);
            }
        }
Beispiel #11
0
        public Move MakeMove(CheckersBoard currentBoard, GameVariant variant, List <Move> gameMoves)
        {
            List <Move> allPossibleMoves = currentBoard.GetAllPossibleMoves(Color);
            int         count            = allPossibleMoves.Count;

            if (count == 0)
            {
                throw new NotAvailableMoveException(Color);
            }
            StringBuilder stringBuilder = new StringBuilder();

            foreach (var move in allPossibleMoves)
            {
                stringBuilder.Append($"{GetPossibility(move)}, ");
            }
            stringBuilder.Remove(stringBuilder.Length - 2, 2);
            int beatedPawns = allPossibleMoves[0].BeatedPieces?.Count ?? 1;

            if (HumanMove.Count == 1 + beatedPawns)
            {
                var possibleMoves = allPossibleMoves.Where(m => m.OldPiece.Position == HumanMove[0].Position && m.NewPiece.Position == HumanMove.Last().Position).ToList();
                HumanMove.Remove(HumanMove.First());
                HumanMove.Remove(HumanMove.Last());
                for (int i = 0; i < HumanMove.Count; i++)
                {
                    possibleMoves = possibleMoves.Where(p => p.BeatedPieces[i + 1].BeatPieceColumn == HumanMove[i].Column && p.BeatedPieces[i + 1].BeatPieceRow == HumanMove[i].Row).ToList();
                }
                var humanMove = possibleMoves.SingleOrDefault();
                if (humanMove == null)
                {
                    throw new WrongMoveException(allPossibleMoves[0].BeatedPieces == null ? 0 : beatedPawns, stringBuilder.ToString());
                }
                return(humanMove);
            }
            else
            {
                throw new WrongMoveException(allPossibleMoves[0].BeatedPieces == null ? 0 : beatedPawns, stringBuilder.ToString());
            }
        }