Esempio n. 1
0
        public Tuple <String, String> ComputeIntentions(Node node, int depth, int earns)
        {
            ulong[] positions = { };
            int[]   values    = { };
            int     maxEarns  = Int32.MinValue + 1;
            Tuple <String, String> bestMove = new Tuple <string, string>("", "");
            Random rnd = new Random();

            while (positions.Length <= 0)
            {
                bestMove = new Tuple <String, String>(node.PiecesPositions[rnd.Next(node.PiecesPositions.Count)], "");
                node.BO.GetPossiblePositions(node.BO.ConvertPositionStringToLong(bestMove.Item1), out positions, out values);
            }
            int randomMove = rnd.Next(positions.Length);

            maxEarns = values[randomMove];
            bestMove = new Tuple <string, string>(bestMove.Item1, node.BO.ConvertPositionLongToString(positions[randomMove]));

            List <int> earnsList = new List <int>();
            List <Tuple <String, String> > moveList = new List <Tuple <String, String> >();

            foreach (var piece in node.PiecesPositions)
            {
                node.BO.GetPossiblePositions(node.BO.ConvertPositionStringToLong(piece), out positions, out values);
                int index = 0;
                foreach (var movement in positions)
                {
                    if (depth == 1)
                    {
                        if (Math.Abs(values[index]) > maxEarns)
                        {
                            bestMove = new Tuple <string, string>(piece, node.BO.ConvertPositionLongToString(movement));
                            maxEarns = Math.Abs(values[index]);
                        }
                    }
                    else
                    {
                        earnsList.Add(Math.Abs(values[index]));
                        moveList.Add(new Tuple <string, string>(piece, node.BO.ConvertPositionLongToString(movement)));
                        BoardStruct newBoardStruct = node.BO.GetNewBoard(node.BO.ConvertPositionStringToLong(piece), movement);
                        earnsList[earnsList.Count - 1] = earnsList[earnsList.Count - 1] - RecursiveMinMax(newBoardStruct, depth - 1, false);
                        index++;
                    }
                }
            }
            if (depth == 1)
            {
                return(bestMove);
            }
            int maxIndex = earnsList.IndexOf(earnsList.Max());

            if (earnsList.Max() == 0)
            {
                maxIndex = rnd.Next(earnsList.Count);
            }
            return(moveList[maxIndex]);
        }
        public BoardStruct GetNewBoard(ulong start, ulong arrive)
        {
            BoardStruct bo = this.board;

            if (board.WhiteTrait)
            {
                bo.BlackPieces = bo.BlackPieces ^ arrive;
                bo.WhitePieces = (bo.WhitePieces | arrive) ^ start;
            }
            else
            {
                bo.WhitePieces = bo.WhitePieces ^ arrive;
                bo.BlackPieces = (bo.BlackPieces | arrive) ^ start;
            }
            bo.WhiteTrait = !bo.WhiteTrait;
            return(bo);
        }
 /* constructors */
 public BoardOpt(BoardStruct bo)
 {
     this.board     = bo;
     bo.WhitePieces = 0;
     bo.BlackPieces = 0;
 }
Esempio n. 4
0
        public int RecursiveMinMax(BoardStruct boardStruct, int depth, bool player)
        {
            BoardOpt      newBoardOptions = new BoardOpt(boardStruct);
            int           maxAdvEarns     = Int32.MinValue + 1;
            List <String> advPieces       = new List <string>();

            if (boardStruct.WhiteTrait)
            {
                //White ones
                foreach (var type in boardStruct.TypesMap)
                {
                    if (type.Value > 0)
                    {
                        advPieces.Add(newBoardOptions.ConvertPositionLongToString(type.Key));
                    }
                }
            }
            else
            {
                //Black ones
                foreach (var type in boardStruct.TypesMap)
                {
                    if (type.Value < 0)
                    {
                        advPieces.Add(newBoardOptions.ConvertPositionLongToString(type.Key));
                    }
                }
            }
            List <int> earnsList = new List <int>();

            foreach (var advPiece in advPieces)
            {
                ulong[] advPositions = { };
                int[]   advValues    = { };
                newBoardOptions.GetPossiblePositions(newBoardOptions.ConvertPositionStringToLong(advPiece), out advPositions, out advValues);
                int advIndex = 0;
                foreach (var advMovement in advPositions)
                {
                    if (depth == 1 && Math.Abs(advValues[advIndex]) > maxAdvEarns)
                    {
                        maxAdvEarns = Math.Abs(advValues[advIndex]);
                    }
                    else
                    {
                        earnsList.Add(Math.Abs(advValues[advIndex]));
                        if (depth != 1 && advValues[advIndex] != 0)
                        {
                            BoardStruct newBoardStruct = newBoardOptions.GetNewBoard(newBoardOptions.ConvertPositionStringToLong(advPiece), advMovement);
                            if (player)
                            {
                                earnsList[earnsList.Count - 1] = earnsList[earnsList.Count - 1] + RecursiveMinMax(newBoardStruct, depth - 1, !player);
                            }
                            if (!player)
                            {
                                earnsList[earnsList.Count - 1] = earnsList[earnsList.Count - 1] - RecursiveMinMax(newBoardStruct, depth - 1, !player);
                            }
                        }
                        advIndex++;
                    }
                }
            }
            if (depth == 1)
            {
                return(maxAdvEarns);
            }
            return(earnsList.Max());
        }