Exemple #1
0
        private void Solve_Wave(int delay = 0)
        {
            wave[] legacy = new wave[1];
            wave[] switcher; // ised to increase legacy.Length
            wave add2legacy;
            Point[] Storage = new Point[length_x * length_y];
            int length = 0;
            legacy[0] = new wave(current_position);
            bool step_done = false;
            int step;
            Point ghost_step = new Point();
            byte[] bit_counter = new byte[4];
            int ban_way = 8; // first run - not banning any way
            bool dead_end = false;
            Random rnd = new Random();
            int sum = 0;
            int finder = -1;
            while (true) // it will break as soon as "length" == 0
            {
                // --- Forking wave
                dead_end = false;
                while (!dead_end)
                {
                    System.Threading.Thread.Sleep(delay);

                    if ((maze[current_position.X, current_position.Y].get_rank() != 2) && (maze[current_position.X, current_position.Y].get_rank() != 3))
                        maze[current_position.X, current_position.Y].set_rank(4);

                    bit_counter = new byte[4];
                    if (animationCheckBox.Checked)
                        draw_cell(current_position);
                    sum = 0;
                    for (int i = 0; i < 4; i++)
                    {
                        bit_counter[i] = maze[current_position.X, current_position.Y].get_side(i);
                        if (bit_counter[i] != 1)
                        {
                            ghost_step.X = current_position.X + step_maker[i].X;
                            ghost_step.Y = current_position.Y + step_maker[i].Y;
                            step = maze[ghost_step.X, ghost_step.Y].get_rank(); // just didnt want to create new variable for this
                            if ((step == 2) || (step == 4))
                                bit_counter[i] = 1;
                            if (maze[ghost_step.X, ghost_step.Y].get_side(all_steps[i + 2]) == 1)
                                bit_counter[i] = 1; // This aint gonna happen, just making sure to cover it
                            // "maze_update()" completely removes this situations
                        }
                        sum += bit_counter[i];
                    }

                    if (ban_way >= 0 && ban_way < 4)
                    {
                        bit_counter[ban_way] = 1;
                        legacy[legacy.Length - 1].add(current_position);
                        //sum++;
                    }

                    if (sum < 3)
                        Storage[length++] = current_position;

                    if (sum < 4)
                    {
                        step_done = false;
                        while (!step_done)
                        {
                            step = rnd.Next(0, 4);
                            if (bit_counter[step] != 1)
                            {
                                ghost_step.X = current_position.X + step_maker[step].X;
                                ghost_step.Y = current_position.Y + step_maker[step].Y;
                                sum = maze[ghost_step.X, ghost_step.Y].get_rank(); // just didnt want to create new variable for this
                                if (sum == 3)
                                {
                                    current_position = ghost_step;
                                    step_done = true;
                                    ban_way = all_steps[step + 2];
                                    legacy[legacy.Length - 1].winner = true;
                                    legacy[legacy.Length - 1].add(current_position);
                                    dead_end = true;
                                }
                                else
                                {
                                    current_position = ghost_step;
                                    step_done = true;
                                    ban_way = all_steps[step + 2];
                                }
                            }
                        }
                        if (animationCheckBox.Checked)
                            draw_cell(current_position);
                    }
                    else
                    {
                        dead_end = true;
                    }
                }
                // --- creating new wave
                if (length == 0)
                    break;
                ban_way = 6;
                current_position = Storage[length-- - 1];
                add2legacy = null; // if something goes wrong - it will throw an error
                for (int i = legacy.Length - 1; i >= 0; i--)
                {
                    finder = legacy[i].find_element(current_position);
                    if (finder >= 0)
                    {
                        add2legacy = legacy[i];
                        break;
                    }
                }
                switcher = legacy;
                legacy = new wave[switcher.Length + 1];
                for (int i = 0; i < switcher.Length; i++)
                    legacy[i] = switcher[i];
                if (finder >= 0)
                    legacy[switcher.Length] = new wave(add2legacy, finder);
            }
            // --- seacrhing for a shortes wave that hit exit cell
            int min_length = length_x*length_y;
            int min_index = -1;
            for (int i = 0; i < legacy.Length; i++)
            {
                if (legacy[i].winner)
                    if (legacy[i].get_length() < min_length)
                    {
                        min_length = legacy[i].get_length();
                        min_index = i;
                    }
            }
            for (int i = 0; i < min_length; i++)
            {
                current_position = legacy[min_index].get_element(i);
                maze[current_position.X, current_position.Y].set_rank(5);
                draw_cell(current_position);
            }
        }
Exemple #2
0
 public wave(wave source, int index)
 {
     int length = source.get_length();
     if (length != 0)
     {
         legacy = new Point[index + 1];
         for (int i = 0; i <= index; i++)
             legacy[i] = source.get_element(i);
     }
 }