Exemplo n.º 1
0
        public IEnumerable <Move> Shuffle(int count)
        {
            var moves      = new List <Move>();
            var gameStates = new List <Board>();
            var legalMoves = new List <Move>(LegalMoves());

            ShuffleMoves(legalMoves);
            Move moveToApply = null;

            gameStates.Add(this);

            for (int i = 0; i < count; i++)
            {
                foreach (var move in legalMoves)
                {
                    Board board = new Board(this);
                    board.ApplyMove(move);

                    bool hasBoard = false;

                    foreach (var gameState in gameStates)
                    {
                        if (gameState.IsSameBoard(board))
                        {
                            hasBoard = true;
                            break;
                        }
                    }

                    if (hasBoard)
                    {
                        gameStates.Add(board);
                        moveToApply = move;
                        break;
                    }
                }

                ApplyMove(moveToApply);
                moves.Add(moveToApply);
            }

            return(moves);
        }
Exemplo n.º 2
0
        public List <Move> Solution()
        {
            var root = new SolutionNode(null, Game.Current);

            if (Game.Current.IsSameBoard(Game.Solved))
            {
                var moves = new List <Move>();
                moves.Add(new Move(Move.Moves.Start));
                return(moves);
            }

            var legalMoves = Game.Current.LegalMoves();
            var visited    = new List <Tuple <SolutionNode, Move> >();

            visited.Add(new Tuple <SolutionNode, Move>(root,
                                                       new Move(Move.Moves.Start)));

            var nodeQueue = new Queue <Tuple <SolutionNode, Move> >();

            foreach (var move in legalMoves)
            {
                var board = new Board(Game.Current);
                board.ApplyMove(move);

                var node = new SolutionNode(root, board);
                root.Children.Add(move, node);

                if (board.IsSameBoard(Game.Solved))
                {
                    var n = new Tuple <SolutionNode, Move>(node, move);
                    visited.Add(n);
                    return(SolutionPath(n, visited));
                }
                else
                {
                    nodeQueue.Enqueue(
                        new Tuple <SolutionNode, Move>(node, move));
                }
            }

            return(BreadthFirstSearch(nodeQueue, visited));
        }
Exemplo n.º 3
0
        public List<Move> Solution()
        {
            var root = new SolutionNode(null, Game.Current);
            if (Game.Current.IsSameBoard(Game.Solved))
            {
                var moves = new List<Move>();
                moves.Add(new Move(Move.Moves.Start));
                return moves;
            }

            var legalMoves = Game.Current.LegalMoves();
            var visited = new List<Tuple<SolutionNode, Move>>();

            visited.Add(new Tuple<SolutionNode, Move>(root,
                new Move(Move.Moves.Start)));

            var nodeQueue = new Queue<Tuple<SolutionNode, Move>>();

            foreach (var move in legalMoves)
            {
                var board = new Board(Game.Current);
                board.ApplyMove(move);

                var node = new SolutionNode(root, board);
                root.Children.Add(move, node);

                if (board.IsSameBoard(Game.Solved))
                {
                    var n = new Tuple<SolutionNode, Move>(node, move);
                    visited.Add(n);
                    return SolutionPath(n, visited);
                }
                else
                {
                    nodeQueue.Enqueue(
                        new Tuple<SolutionNode, Move>(node, move));
                }
            }

            return BreadthFirstSearch(nodeQueue, visited);
        }
Exemplo n.º 4
0
        public IEnumerable<Move> Shuffle(int count)
        {
            var moves = new List<Move>();
            var gameStates = new List<Board>();
            var legalMoves = new List<Move>(LegalMoves());
            ShuffleMoves(legalMoves);
            Move moveToApply = null;

            gameStates.Add(this);

            for (int i = 0; i < count; i++)
            {
                foreach (var move in legalMoves)
                {
                    Board board = new Board(this);
                    board.ApplyMove(move);

                    bool hasBoard = false;

                    foreach (var gameState in gameStates)
                    {
                        if (gameState.IsSameBoard(board))
                        {
                            hasBoard = true;
                            break;
                        }
                    }

                    if (hasBoard)
                    {
                        gameStates.Add(board);
                        moveToApply = move;
                        break;
                    }
                }

                ApplyMove(moveToApply);
                moves.Add(moveToApply);
            }

            return moves;
        }