public void solve(int xTarget, int yTarget, int outputMode)
        {
            isSolved mt_isSolved = new isSolved(false);
            bool lengthCheckX = (xTarget - 1) >= 0;
            bool lengthCheckY = (yTarget - 1) >= 0;
            Vertice dequeue;
            Vertice solvedGame = null;

            while (!mt_isSolved.b && queue.TryDequeue(out dequeue))
            {
                Board next = dequeue.state;
                List<Vertice> moves = new List<Vertice>(allPossibleMoves(next));

                foreach (Vertice v in moves)
                {
                    if (v.state.board[xTarget, yTarget] == 'x')
                    {
                        //Vertical + other lengths
                        bool solveX = false;
                        if (lengthCheckX)
                            solveX = (v.state.board[xTarget - 1, yTarget] != 'x') &&
                                (v.state.board[xTarget + 1, yTarget] == 'x');
                        else
                            solveX = (v.state.board[xTarget + 1, yTarget] == 'x');

                        bool solveY = false;
                        if (lengthCheckY)
                            solveY = (v.state.board[xTarget, yTarget - 1] != 'x') &&
                                (v.state.board[xTarget, yTarget + 1] == 'x');
                        else
                            solveX = (v.state.board[xTarget, yTarget + 1] == 'x');

                        if ((v.state.board[xTarget, yTarget] == 'x') &&
                            (solveX || solveY))
                        {
                            lock (mt_isSolved)
                            {
                                mt_isSolved.b = true;
                            }
                        }
                        if (mt_isSolved.b)
                            solvedGame = v;

                    }

                    dequeue.AddChild(v);
                    queue.Enqueue(v);

                    if (mt_isSolved.b)
                        break;
                }
            }

            if (mt_isSolved.b)
            {
                if (outputMode == 0)
                    Console.WriteLine(solvedGame.countToRoot());
                else
                    Console.WriteLine(solvedGame.movesToRoot());
            }
            else if(queue.IsEmpty)
                Console.WriteLine("Geen oplossing gevonden");
        }
Exemple #2
0
        public void solve(int xTarget, int yTarget, int outputMode)
        {
            isSolved mt_isSolved  = new isSolved(false);
            bool     lengthCheckX = (xTarget - 1) >= 0;
            bool     lengthCheckY = (yTarget - 1) >= 0;
            Vertice  dequeue;
            Vertice  solvedGame = null;

            while (!mt_isSolved.b && queue.TryDequeue(out dequeue))
            {
                Board          next  = dequeue.state;
                List <Vertice> moves = new List <Vertice>(allPossibleMoves(next));

                foreach (Vertice v in moves)
                {
                    if (v.state.board[xTarget, yTarget] == 'x')
                    {
                        //Vertical + other lengths
                        bool solveX = false;
                        if (lengthCheckX)
                        {
                            solveX = (v.state.board[xTarget - 1, yTarget] != 'x') &&
                                     (v.state.board[xTarget + 1, yTarget] == 'x');
                        }
                        else
                        {
                            solveX = (v.state.board[xTarget + 1, yTarget] == 'x');
                        }

                        bool solveY = false;
                        if (lengthCheckY)
                        {
                            solveY = (v.state.board[xTarget, yTarget - 1] != 'x') &&
                                     (v.state.board[xTarget, yTarget + 1] == 'x');
                        }
                        else
                        {
                            solveX = (v.state.board[xTarget, yTarget + 1] == 'x');
                        }

                        if ((v.state.board[xTarget, yTarget] == 'x') &&
                            (solveX || solveY))
                        {
                            lock (mt_isSolved)
                            {
                                mt_isSolved.b = true;
                            }
                        }
                        if (mt_isSolved.b)
                        {
                            solvedGame = v;
                        }
                    }

                    dequeue.AddChild(v);
                    queue.Enqueue(v);

                    if (mt_isSolved.b)
                    {
                        break;
                    }
                }
            }

            if (mt_isSolved.b)
            {
                if (outputMode == 0)
                {
                    Console.WriteLine(solvedGame.countToRoot());
                }
                else
                {
                    Console.WriteLine(solvedGame.movesToRoot());
                }
            }
            else if (queue.IsEmpty)
            {
                Console.WriteLine("Geen oplossing gevonden");
            }
        }