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));
        }
Example #10
0
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (m_playerTurn)
            {
                var element = Mouse.DirectlyOver;
                if (element != null)
                {
                    // Si un pion est déjà selectionné
                    if (m_selectedToken != null && m_playerPossibleMove.ContainsKey(m_selectedToken))
                    {
                        // On recupere la cellule selctionner et on recupere le mouvement associe
                        Vector2   selectedCell = new Vector2(Grid.GetColumn((UIElement)element), Grid.GetRow((UIElement)element));
                        FindMove  move         = new FindMove(selectedCell);
                        TokenMove tokenMove    = m_selectedTokenPossibleMove.Find(move.Match);

                        // Si la cellule correspond a un deplacement l'effectuer
                        if (tokenMove != null)
                        {
                            // Move the token and reset the colors of the board
                            ChangeColorOfCells(m_selectedTokenPossibleMove, Colors.Khaki);
                            if (m_selectedToken != null)
                            {
                                m_board.ExecuteTokenMove(m_selectedToken, tokenMove);
                                DisplayBoard();
                                //Calcul les captures possible restante
                                if (m_sequenceEngaged.Count > 0)
                                {
                                    List <TokenMoveSequence> toRemove = new List <TokenMoveSequence>();
                                    foreach (TokenMoveSequence sequence in m_sequenceEngaged)
                                    {
                                        TokenMove movePlayed = sequence.PlayMove();
                                        if (sequence.Empty() || !movePlayed.Equals(tokenMove))
                                        {
                                            toRemove.Add(sequence);
                                        }
                                    }
                                    foreach (TokenMoveSequence sequence in toRemove)
                                    {
                                        m_sequenceEngaged.Remove(sequence);
                                    }
                                }
                                else
                                {
                                    foreach (TokenMoveSequence sequence in m_playerPossibleMove[m_selectedToken])
                                    {
                                        TokenMove movePlayed = sequence.PlayMove();
                                        if (!sequence.Empty() && movePlayed.Equals(tokenMove))
                                        {
                                            m_sequenceEngaged.Add(sequence);
                                        }
                                    }
                                }


                                // Fini le tour du joueur ou affiche les prochains mouvement possible
                                if (m_sequenceEngaged.Count == 0)
                                {
                                    if (m_selectedToken.Position.Y == 0)
                                    {
                                        m_board.Crown(m_selectedToken);
                                    }
                                    DisplayBoard();
                                    m_playerTurn                = false;
                                    m_selectedToken             = null;
                                    m_selectedTokenPossibleMove = null;
                                    m_sequenceEngaged           = null;
                                }
                                else
                                {
                                    m_selectedTokenPossibleMove = ComputeCells(m_sequenceEngaged);
                                    ChangeColorOfCells(m_selectedTokenPossibleMove, Colors.CadetBlue);
                                }
                            }
                        }
                        // Sinon on deselectionne le pion
                        else if (m_sequenceEngaged.Count == 0)
                        {
                            if (m_selectedTokenPossibleMove == null)
                            {
                                return;
                            }
                            // Reset selected token
                            ChangeColorOfCells(m_selectedTokenPossibleMove, Colors.Khaki);
                            m_selectedToken = null;
                        }
                    }
                    else
                    {
                        // Si on a pas deja commence une sequence de mouvement
                        if (m_sequenceEngaged != null && m_sequenceEngaged.Count == 0)
                        {
                            // Select the token and mark the possible destinations with a color
                            m_selectedToken = m_board.Tokens[Grid.GetRow((UIElement)element), Grid.GetColumn((UIElement)element)];
                            if (m_selectedToken != null && m_playerPossibleMove.ContainsKey(m_selectedToken))
                            {
                                m_selectedTokenPossibleMove = ComputeCells(m_playerPossibleMove[m_selectedToken]);
                                ChangeColorOfCells(m_selectedTokenPossibleMove, Colors.CadetBlue);
                            }
                        }
                        else
                        {
                            if (m_board.Tokens[Grid.GetRow((UIElement)element), Grid.GetColumn((UIElement)element)] == m_selectedToken)
                            {
                                ChangeColorOfCells(m_selectedTokenPossibleMove, Colors.CadetBlue);
                            }
                        }
                    }
                }
            }
        }