public static List <NextMove> FindAvailableMoves(Int64[,] board, Int64 player)
        {
            List <NextMove> results = new List <NextMove>();

            for (Int64 row = 0; row < board.GetLength(0); row++)
            {
                // Skip over pieces that cant be played
                for (Int64 column = 1 - (row % 2); column < board.GetLength(1); column += 2)
                {
                    Int64 piece = board[row, column];

                    if (piece == 5)
                    {
                        continue;
                    }
                    else if (piece == 1 && player == 1)
                    {
                        Boolean canMoveUpAndLeft  = CheckMove.CheckMoveUpLeft(board, row, column);
                        Boolean canMoveUpAndRight = CheckMove.CheckMoveUpRight(board, row, column);
                        if (canMoveUpAndLeft)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row - 1,
                                NextWidth     = column - 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveUpAndRight)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row - 1,
                                NextWidth     = column + 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveUpAndLeft == false || canMoveUpAndRight == false)
                        {
                            Tree resultTree = CheckMove.CheckTakeUp(board, row, column, new Int64[] { 2 }, new Tree(new TreeTake
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                            }));
                            if (resultTree.Left != null || resultTree.Right != null)
                            {
                                List <List <TreeTake> > treeArray = Tree.TreeToArray(resultTree);
                                results.AddRange(ProcessTreeArray(treeArray));
                            }
                        }
                    }
                    else if (piece == 2 && player == 2)
                    {
                        Boolean canMoveDownAndLeft  = CheckMove.CheckMoveDownLeft(board, row, column);
                        Boolean canMoveDownAndRight = CheckMove.CheckMoveDownRight(board, row, column);

                        if (canMoveDownAndLeft)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row + 1,
                                NextWidth     = column - 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveDownAndRight)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row + 1,
                                NextWidth     = column + 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveDownAndLeft == false || canMoveDownAndRight == false)
                        {
                            Tree resultTree = CheckMove.CheckTakeDown(board, row, column, new Int64[] { 1 }, new Tree(new TreeTake
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                            }));
                            if (resultTree.Left != null || resultTree.Right != null)
                            {
                                List <List <TreeTake> > treeArray = Tree.TreeToArray(resultTree);
                                results.AddRange(ProcessTreeArray(treeArray));
                            }
                        }
                    }
                    else if ((piece == 3 && player == 1) || (piece == 4 && player == 2))
                    {
                        Boolean canMoveUpAndLeft    = CheckMove.CheckMoveUpLeft(board, row, column);
                        Boolean canMoveUpAndRight   = CheckMove.CheckMoveUpRight(board, row, column);
                        Boolean canMoveDownAndLeft  = CheckMove.CheckMoveDownLeft(board, row, column);
                        Boolean canMoveDownAndRight = CheckMove.CheckMoveDownRight(board, row, column);

                        if (canMoveUpAndLeft)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row - 1,
                                NextWidth     = column - 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveUpAndRight)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row - 1,
                                NextWidth     = column + 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveDownAndLeft)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row + 1,
                                NextWidth     = column - 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveDownAndRight)
                        {
                            results.Add(new NextMove
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                                NextHeight    = row + 1,
                                NextWidth     = column + 1,
                                Takes         = EmptyList
                            });
                        }
                        if (canMoveUpAndLeft == false || canMoveUpAndRight == false || canMoveDownAndLeft == false || canMoveDownAndRight == false)
                        {
                            Int64[]  countersToTake = piece == 3 ? new Int64[] { 2, 4 } : new Int64[] { 1, 3 };
                            KingTree resultTree     = CheckMove.CheckKingTake(board, row, column, row, column, countersToTake, new KingTree(new TreeTake
                            {
                                CurrentHeight = row,
                                CurrentWidth  = column,
                            }));
                            if (resultTree.DownLeft != null || resultTree.DownRight != null || resultTree.UpLeft != null || resultTree.UpRight != null)
                            {
                                List <List <TreeTake> > treeArray = KingTree.KingTreeToArray(resultTree);
                                results.AddRange(ProcessTreeArray(treeArray));
                            }
                        }
                    }
                }
            }

            return(results);
        }
Example #2
0
        public static KingTree CheckKingTake(Int64[,] board, Int64 preHeight, Int64 preWidth, Int64 height, Int64 width, Int64[] playerToTake, KingTree tree)
        {
            try
            {
                //Down left
                if (Array.IndexOf(playerToTake, board[height + 1, width - 1]) > -1)
                {
                    if (board[height + 2, width - 2] == 5 || board[height + 2, width - 2] == 6)
                    {
                        if (preHeight != height + 2 || preWidth != width - 2)
                        {
                            tree.DownLeft = new KingTree(new TreeTake
                            {
                                TakeHeight    = height + 1,
                                TakeWidth     = width - 1,
                                CurrentHeight = height + 2,
                                CurrentWidth  = width - 2
                            });
                            CheckKingTake(board, height, width, height + 2, width - 2, playerToTake, tree.DownLeft);
                        }
                    }
                }
            }
            catch { }

            try
            {
                //Down Right
                if (Array.IndexOf(playerToTake, board[height + 1, width + 1]) > -1)
                {
                    if (board[height + 2, width + 2] == 5 || board[height + 2, width + 2] == 6)
                    {
                        if (preHeight != height + 2 || preWidth != width + 2)
                        {
                            tree.DownRight = new KingTree(new TreeTake
                            {
                                TakeHeight    = height + 1,
                                TakeWidth     = width + 1,
                                CurrentHeight = height + 2,
                                CurrentWidth  = width + 2
                            });
                            CheckKingTake(board, height, width, height + 2, width + 2, playerToTake, tree.DownRight);
                        }
                    }
                }
            }
            catch { }

            try
            {
                //Up left
                if (Array.IndexOf(playerToTake, board[height - 1, width - 1]) > -1)
                {
                    if (board[height - 2, width - 2] == 5 || board[height - 2, width - 2] == 6)
                    {
                        if (preHeight != height - 2 || preWidth != width - 2)
                        {
                            tree.UpLeft = new KingTree(new TreeTake
                            {
                                TakeHeight    = height - 1,
                                TakeWidth     = width - 1,
                                CurrentHeight = height - 2,
                                CurrentWidth  = width - 2
                            });
                            CheckKingTake(board, height, width, height - 2, width - 2, playerToTake, tree.UpLeft);
                        }
                    }
                }
            }
            catch { }

            try
            {
                //Up Right
                if (Array.IndexOf(playerToTake, board[height - 1, width + 1]) > -1)
                {
                    if (board[height - 2, width + 2] == 5 || board[height - 2, width + 2] == 6)
                    {
                        if (preHeight != height - 2 || preWidth != width + 2)
                        {
                            tree.UpRight = new KingTree(new TreeTake
                            {
                                TakeHeight    = height - 1,
                                TakeWidth     = width + 1,
                                CurrentHeight = height - 2,
                                CurrentWidth  = width + 2
                            });
                            CheckKingTake(board, height, width, height - 2, width + 2, playerToTake, tree.UpRight);
                        }
                    }
                }
            }
            catch { }

            return(tree);
        }
Example #3
0
        public static List <List <TreeTake> > KingTreeToArray(KingTree mainTree)
        {
            List <List <TreeTake> > result = new List <List <TreeTake> >();

            iter(mainTree, new List <TreeTake>());

            void iter(KingTree tree, List <TreeTake> list)
            {
                list.Add(tree.Value);

                if (tree.DownLeft != null && tree.DownRight != null && tree.UpLeft != null && tree.UpRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.DownRight, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.DownRight != null && tree.UpLeft != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.DownRight, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.DownRight != null && tree.UpRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.DownRight, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.UpLeft != null && tree.UpRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownRight != null && tree.UpLeft != null && tree.UpRight != null)
                {
                    iter(tree.DownRight, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.DownRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.DownRight, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.UpLeft != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.UpRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownRight != null && tree.UpLeft != null)
                {
                    iter(tree.DownRight, new List <TreeTake>(list));
                    iter(tree.UpLeft, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null && tree.UpRight != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.UpLeft != null && tree.UpRight != null)
                {
                    iter(tree.UpLeft, new List <TreeTake>(list));
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else if (tree.DownLeft != null)
                {
                    iter(tree.DownLeft, new List <TreeTake>(list));
                }
                else if (tree.DownRight != null)
                {
                    iter(tree.DownRight, new List <TreeTake>(list));
                }
                else if (tree.UpLeft != null)
                {
                    iter(tree.UpLeft, new List <TreeTake>(list));
                }
                else if (tree.UpRight != null)
                {
                    iter(tree.UpRight, new List <TreeTake>(list));
                }
                else
                {
                    result.Add(list);
                }
            }

            return(result);
        }