Beispiel #1
0
 internal static void FindMotions(Board board, bool isWhite, int x, int y,
                                  ref List <List <Point> > moves,
                                  ref KillItem kills)
 {
     FindMoves(board, isWhite, x, y, ref moves);
     FindKills(board, isWhite, x, y, ref kills);
 }
Beispiel #2
0
        public static List <Motion> GetAllMotions(Board board, bool isWhite)
        {
            List <List <Point> > moves = new List <List <Point> >();
            List <KillItem>      kills = new List <KillItem>();

            Func <int, int, bool> colorValidator;

            if (isWhite)
            {
                colorValidator = board.IsWhite;
            }
            else
            {
                colorValidator = board.IsBlack;
            }

            for (int i = 0; i < Board.SIZE; i++)
            {
                for (int j = 0; j < Board.SIZE; j++)
                {
                    if (colorValidator(i, j))
                    {
                        var kill = new KillItem();

                        if (board.IsSenior(i, j))
                        {
                            Senior.FindMotions(board, isWhite, i, j, ref moves, ref kill);
                        }
                        else
                        {
                            Pawn.FindMotions(board, isWhite, i, j, ref moves, ref kill);
                        }

                        if (kill.Move != null) // значит кого-то убили
                        {
                            kills.Add(kill);
                        }
                    }
                }
            }

            var resultKills = new List <List <Point> >();

            kills.ForEach(pk =>
            {
                resultKills.AddRange(pk.SplitToBranches());
                pk.Dispose();
            });

            var list = resultKills.Count > 0 ? resultKills : moves;

            var ret = list.Select(m => new Motion(m.ToArray())).ToList();

            return(ret);
        }
        protected List <Point> ReconstructBranch(KillItem leaf)
        {
            List <Point> ret = new List <Point>();

            for (var iter = leaf; iter != null; iter = iter.Parent)
            {
                ret.Add(iter.Move);
            }

            ret.Reverse();

            return(ret);
        }
Beispiel #4
0
            internal static void FindKills(Board board, bool isWhite, int x, int y, ref KillItem kills)
            {
                for (int i = 0; i < 4; i++)
                {
                    int xN  = x + _moveDirections[i, 0];
                    int yN  = y + _moveDirections[i, 1];
                    int xN2 = x + 2 * _moveDirections[i, 0];
                    int yN2 = y + 2 * _moveDirections[i, 1];

                    if (!InBounds(xN) || !InBounds(yN) || !InBounds(xN2) || !InBounds(yN2))
                    {
                        continue;
                    }

                    if (board[xN2, yN2] != FigureEnum.NONE)
                    {
                        continue;
                    }

                    if (CheckersHasDifferentColor(board, x, y, xN, yN))
                    {
                        var beated = new Point(xN, yN);

                        if (kills.BranchContaintsValue(beated))
                        {
                            continue;
                        }

                        if (kills.Move == null)
                        {
                            kills.Move = new Point(x, y);
                        }

                        var beat = new KillItem
                        {
                            Move   = new Point(xN2, yN2),
                            Killed = beated
                        };

                        kills.AddChild(ref beat);

                        var boardCopy = (Board)board.Clone();

                        boardCopy[xN2, yN2] = boardCopy[x, y];
                        boardCopy[x, y]     = FigureEnum.NONE;

                        FindKills(boardCopy, isWhite, xN2, yN2, ref beat);
                    }
                }
            }
 protected static void FindFinalLeafs(KillItem node, ref List <KillItem> finalLeafs)
 {
     foreach (var child in node._children)
     {
         if (child._children.Count == 0)
         {
             finalLeafs.Add(child);
         }
         else
         {
             FindFinalLeafs(child, ref finalLeafs);
         }
     }
 }
Beispiel #6
0
 internal static void FindMotions(Board board, bool isWhite, int i, int j, ref List <List <Point> > moves, ref KillItem kill)
 {
     FindMoves(board, i, j, ref moves);
     FindKills(board, isWhite, i, j, ref kill);
     kill.FilterForSeniorKills();
 }
Beispiel #7
0
            private static void FindKills(Board board, bool isWhite, int x, int y, ref KillItem kills)
            {
                int[,] _whiteDirections = new int[, ]  {
                    { -1, -1 }, { 1, -1 }
                };
                int[,] _blackDirections = new int[, ] {
                    { 1, 1 }, { -1, 1 }
                };

                var dir = isWhite ? _whiteDirections : _blackDirections;

                for (int i = 0; i < 2; i++)
                {
                    int xN  = x + dir[i, 0];
                    int yN  = y + dir[i, 1];
                    int xN2 = x + 2 * dir[i, 0];
                    int yN2 = y + 2 * dir[i, 1];

                    if (!InBounds(xN) || !InBounds(yN) || !InBounds(xN2) || !InBounds(yN2))
                    {
                        continue;
                    }

                    if (board[xN2, yN2] != FigureEnum.NONE)
                    {
                        continue;
                    }

                    if (CheckersHasDifferentColor(board, x, y, xN, yN) && !board.IsSenior(xN, yN))
                    {
                        var killed = new Point(xN, yN);

                        if (kills.BranchContaintsValue(killed))
                        {
                            continue;
                        }

                        if (kills.Move == null)
                        {
                            kills.Move = new Point(x, y);
                        }

                        var beat = new KillItem
                        {
                            Move   = new Point(xN2, yN2),
                            Killed = killed
                        };

                        kills.AddChild(ref beat);

                        var boardCopy = (Board)board.Clone();

                        boardCopy[xN2, yN2] = boardCopy[x, y];
                        boardCopy[x, y]     = FigureEnum.NONE;

                        if (ShouldBecomeSenior(yN2, isWhite))
                        {
                            Senior.FindKills(boardCopy, isWhite, xN2, yN2, ref beat);
                        }
                        else
                        {
                            FindKills(boardCopy, isWhite, xN2, yN2, ref beat);
                        }
                    }
                }
            }
 public void AddChild(ref KillItem node)
 {
     _children.Add(node);
     node.Parent = this;
 }