public NextMove GetNextMoveForAutomatedPlayer(GetNextMove getNextMove)
        {
            List <NextMove> results = FindMove.FindAvailableMoves(getNextMove.Board, getNextMove.Player);

            List <NextMove> takeMoves = results.Where(m => m.Takes != null).ToList();

            Random random = new Random();

            if (takeMoves.Count > 0)
            {
                return(takeMoves[random.Next(0, takeMoves.Count)]);
            }
            else if (results.Count > 0)
            {
                return(results[random.Next(0, results.Count)]);
            }
            else
            {
                return(new NextMove
                {
                    CurrentHeight = -1,
                    CurrentWidth = -1,
                    NextHeight = -1,
                    NextWidth = -1,
                });
            }
        }
        private List <AvailableBoard> GetAvailableBoards(int[,] board, int player)
        {
            List <AvailableBoard> results = new List <AvailableBoard>();

            //Get all possible moves for a board, this is used it 2 ways
            //1. This method will use the information to create a board for every possible move
            //2. This list of possible moves is used by the evaluation method to not redo work is massively improve efficiency

            List <NextMove> moves = FindMove.FindAvailableMoves(board);

            foreach (NextMove move in moves.Where(m => m.Piece == player))
            {
                //Copy board
                int[,] moveBoard = (int[, ])board.Clone();

                //Remove taken pieces form the board
                if (move.Takes != null)
                {
                    foreach (Take take in move.Takes)
                    {
                        moveBoard[take.Height, take.Width] = 5;
                    }
                }

                //Add kings
                if (player == 1 && move.NextHeight == 0)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 3;
                }
                else if (player == 2 && move.NextHeight == 7)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 4;
                }
                else
                {
                    int tempValue = moveBoard[move.CurrentHeight, move.CurrentWidth];
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = tempValue;
                }

                results.Add(new AvailableBoard
                {
                    Board         = moveBoard,
                    CurrentHeight = move.CurrentHeight,
                    CurrentWidth  = move.CurrentWidth,
                    NextHeight    = move.NextHeight,
                    NextWidth     = move.NextWidth,
                    Takes         = move.Takes,
                    moves         = moves
                });
            }

            return(results);
        }
        public List <PotentialNextMove> GetAvailableBoards(Int64[,] board, Int64 player)
        {
            List <PotentialNextMove> results = new List <PotentialNextMove>();

            List <NextMove> moves = FindMove.FindAvailableMoves(board, player);

            foreach (NextMove move in moves)
            {
                //Copy board
                Int64[,] moveBoard = (Int64[, ])board.Clone();

                //Remove taken pieces form the board
                if (move.Takes.Count > 0)
                {
                    foreach (Piece take in move.Takes)
                    {
                        moveBoard[take.Height, take.Width] = 5;
                    }
                }

                //Add kings
                if (player == 1 && move.NextHeight == 0)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 3;
                }
                else if (player == 2 && move.NextHeight == 7)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 4;
                }
                else
                {
                    Int64 tempValue = moveBoard[move.CurrentHeight, move.CurrentWidth];
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = tempValue;
                }

                results.Add(new PotentialNextMove
                {
                    Board         = moveBoard,
                    CurrentHeight = move.CurrentHeight,
                    CurrentWidth  = move.CurrentWidth,
                    NextHeight    = move.NextHeight,
                    NextWidth     = move.NextWidth,
                    Takes         = move.Takes
                });
            }

            return(results);
        }
        private List <AvailableBoard> GetAvailableBoards(int[,] board, int player)
        {
            List <AvailableBoard> results = new List <AvailableBoard>();

            List <NextMove> moves = FindMove.FindAvailableMoves(board, player);

            foreach (NextMove move in moves)
            {
                //Copy board
                int[,] moveBoard = (int[, ])board.Clone();

                //Remove taken pieces form the board
                if (move.Takes != null)
                {
                    foreach (Take take in move.Takes)
                    {
                        moveBoard[take.Height, take.Width] = 5;
                    }
                }

                //Add kings
                if (player == 1 && move.NextHeight == 0)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 3;
                }
                else if (player == 2 && move.NextHeight == 7)
                {
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = 4;
                }
                else
                {
                    int tempValue = moveBoard[move.CurrentHeight, move.CurrentWidth];
                    moveBoard[move.CurrentHeight, move.CurrentWidth] = 5;
                    moveBoard[move.NextHeight, move.NextWidth]       = tempValue;
                }

                results.Add(new AvailableBoard
                {
                    Board         = moveBoard,
                    CurrentHeight = move.CurrentHeight,
                    CurrentWidth  = move.CurrentWidth,
                    NextHeight    = move.NextHeight,
                    NextWidth     = move.NextWidth,
                    Takes         = move.Takes
                });
            }

            return(results);
        }
        public List <Piece> GetPotentialMoves(GetPlayerTips getPlayerTips)
        {
            List <Piece> potentialMoves = FindMove.FindAvailableMoves(getPlayerTips.Board, getPlayerTips.TipFor)
                                          .Select(m =>
            {
                return(new Piece
                {
                    Height = m.NextHeight,
                    Width = m.NextWidth
                });
            })
                                          .ToList();

            return(potentialMoves.Distinct(new PieceComparer()).ToList());
        }
        public void FindMoveFindAvailableMoves_BasicTest()
        {
            //Associate
            int[,] board = new int[, ] {
                { 0, 2, 0, 2, 0, 2, 0, 2 },
                { 2, 0, 2, 0, 2, 0, 2, 0 },
                { 0, 2, 0, 2, 0, 2, 0, 2 },
                { 5, 0, 5, 0, 5, 0, 5, 0 },
                { 0, 5, 0, 5, 0, 5, 0, 5 },
                { 1, 0, 1, 0, 1, 0, 1, 0 },
                { 0, 1, 0, 1, 0, 1, 0, 1 },
                { 1, 0, 1, 0, 1, 0, 1, 0 },
            };
            int player = 1;

            //Act
            List <NextMove> results = FindMove.FindAvailableMoves(board, player);

            //Assert
            Assert.AreEqual(7, results.Count);
        }
Example #7
0
        public NextMove GetNextMoveForAutomatedPlayer(GetNextMove getNextMove)
        {
            List <NextMove> results = FindMove.FindAvailableMoves(getNextMove.Board, getNextMove.Player);

            List <NextMove> takeMoves = results.Where(m => m.Takes.Count > 0).ToList();

            Random random = new Random();

            if (takeMoves.Count > 0)
            {
                return(takeMoves[random.Next(0, takeMoves.Count)]);
            }
            else if (results.Count > 0)
            {
                return(results[random.Next(0, results.Count)]);
            }
            else
            {
                throw new Exception($"Board contains no moves for player {getNextMove.Player}");
            }
        }
        public void FindAvailableMovesTests_KingsAdvanced()
        {
            //Associate
            Int64[,] board = new Int64[, ]
            {
                { 0, 2, 0, 2, 0, 2, 0, 2 },
                { 2, 0, 2, 0, 2, 0, 2, 0 },
                { 0, 2, 0, 5, 0, 2, 0, 2 },
                { 5, 0, 5, 0, 5, 0, 5, 0 },
                { 0, 4, 0, 2, 0, 5, 0, 5 },
                { 5, 0, 5, 0, 5, 0, 5, 0 },
                { 0, 5, 0, 2, 0, 5, 0, 5 },
                { 5, 0, 3, 0, 5, 0, 5, 0 },
            };
            Int64 player = 1;

            //Act
            List <NextMove> moves = FindMove.FindAvailableMoves(board, player);

            //Assert
            Assert.AreEqual(4, moves.Count);
        }
        public void FindAvailableMovesTests_Takes()
        {
            //Associate
            Int64[,] board = new Int64[, ]
            {
                { 0, 2, 0, 2, 0, 2, 0, 2 },
                { 2, 0, 2, 0, 2, 0, 2, 0 },
                { 0, 2, 0, 5, 0, 2, 0, 2 },
                { 5, 0, 5, 0, 5, 0, 5, 0 },
                { 0, 5, 0, 2, 0, 5, 0, 5 },
                { 1, 0, 1, 0, 1, 0, 1, 0 },
                { 0, 1, 0, 1, 0, 1, 0, 1 },
                { 1, 0, 1, 0, 1, 0, 1, 0 },
            };
            Int64 player = 1;

            //Act
            List <NextMove> moves = FindMove.FindAvailableMoves(board, player);

            //Assert
            Assert.AreEqual(2, moves.Count(m => m.Takes.Count > 0));
        }