private void buttonGamilton_Click(object sender, EventArgs e)
 {
     //Labirint = new Graf(DataGridViewUtils.GridToArray2<int>(dgvMatrix), ArrayUtils.StrToInt(textBoxEntrance.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)), ArrayUtils.StrToInt(textBoxExit.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)));
     //DrawingGraf = new Draw(Map, Labirint);
     Path = Labirint.Path;
     Node = Labirint.Nodes;
     Map.EndContainer(graphicsContainer);
     Labirint.GamiltonWays();
     DrawingGraf = new Draw(Map, Labirint);
     DrawingGraf.DrawGraf();
     pictureBox.Image = Bitmap;
 }
Exemple #2
0
        public void Move()
        {
            var mousePosition = Mouse.GetPosition(Program.Window);

            if (mousePosition.X > 0 && mousePosition.Y > 0 && mousePosition.X < World.World_Size * Tile.Tile_Size && mousePosition.Y < World.World_Size * Tile.Tile_Size)
            {
                if (Mouse.IsButtonPressed(Mouse.Button.Left))
                {
                    Labirint.CELL Mouse_cell  = GetCell(mousePosition.X, mousePosition.Y, World.labir);
                    Labirint.CELL Curent_cell = GetCell(Position.X, Position.Y, World.labir);
                    Location = Curent_cell;
                    List <Labirint.CELL> list_cell = Labirint.GetNeighbours(World.labir, Curent_cell, World.World_Size, World.World_Size);
                    if (list_cell.Contains(Mouse_cell))
                    {
                        Position    = new Vector2f(Tile.Tile_Size * Mouse_cell.x, Tile.Tile_Size * Mouse_cell.y);
                        Curent_cell = GetCell(Position.X, Position.Y, World.labir);
                    }
                    if ((Curent_cell.x == World.exit_cell.x) && (Curent_cell.y == World.exit_cell.y))
                    {
                        Program.win_end_game = new RenderWindow(new VideoMode((Tile.Tile_Size + 16) * World.World_Size, Tile.Tile_Size * World.World_Size), "endgame", Styles.None);
                        Program.win_end_game.SetVerticalSyncEnabled(true);
                        Surface game_over = new Surface(720, 480, Type_of_surface.Game_over);
                        while (Program.win_end_game.IsOpen)
                        {
                            Program.win_end_game.DispatchEvents();
                            Program.win_end_game.Clear();
                            Program.win_end_game.Draw(game_over);
                            if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                            {
                                Program.win_end_game.Close();
                                Program.win.Close();
                            }
                            Program.win_end_game.Display();
                        }
                    }
                }
            }
        }
Exemple #3
0
        public static Path FindPath(Labirint gameField, Vector2D a, Vector2D b)
        {
            Path shortestPath = new Path();
            int  i            = 1;

            int[,] arr = new int[gameField.Width, gameField.Heigth];

            for (int x = 0; x < gameField.Width; x++)
            {
                for (int y = 0; y < gameField.Heigth; y++)
                {
                    arr[x, y] = gameField[x, y];
                }
            }
            if (!(b.X > 0 && b.X < gameField.Width && b.Y > 0 && b.Y < gameField.Heigth))
            {
                if (b.X < 0)
                {
                    b.X = 0;
                }
                if (b.X > gameField.Width - 1)
                {
                    b.X = gameField.Width - 1;
                }
                if (b.Y < 0)
                {
                    b.Y = 0;
                }
                if (b.Y > gameField.Heigth - 1)
                {
                    b.Y = gameField.Heigth - 1;
                }
            }
            if (arr[b.X, b.Y] != 0)
            {
                int tempX = 0;
                int tempY = 0;
                int x     = b.X;
                int y     = b.Y;
                int c     = 0;
                while (c == 0)
                {
                    if (x > 0 && arr[x - 1, y] == 0)
                    {
                        tempX = x - 1;
                        tempY = y;
                        c++;
                    }
                    if (x < gameField.Width - 1 && arr[x + 1, y] == 0)
                    {
                        tempX = x + 1;
                        tempY = y;
                        c++;
                    }
                    if (y > 0 && arr[x, y - 1] == 0)
                    {
                        tempX = x;
                        tempY = y - 1;
                        c++;
                    }
                    if (y < gameField.Heigth - 1 && arr[x, y + 1] == 0)
                    {
                        tempX = x;
                        tempY = y + 1;
                        c++;
                    }

                    if (c == 0)
                    {
                        if (x > 0)
                        {
                            tempX = x - 1;
                            tempY = y;
                        }
                        if (x < gameField.Width - 1)
                        {
                            tempX = x + 1;
                            tempY = y;
                        }
                        if (y > 0)
                        {
                            tempX = x;
                            tempY = y - 1;
                        }
                        if (y < gameField.Heigth - 1)
                        {
                            tempX = x;
                            tempY = y + 1;
                        }
                    }
                    x = tempX;
                    y = tempY;
                }
                b.X = x;
                b.Y = y;
            }
            arr[b.X, b.Y] = 1;
            int count = 0;

            while (arr[a.X, a.Y] == 0)
            {
                count = 0;
                for (int x = 0; x < gameField.Width; x++)
                {
                    for (int y = 0; y < gameField.Heigth; y++)
                    {
                        if (arr[x, y] == i)
                        {
                            if (x > 0 && (arr[x - 1, y] == 0 || arr[x - 1, y] < -1))
                            {
                                arr[x - 1, y] = i + 1;
                                count++;
                            }
                            if (y > 0 && (arr[x, y - 1] == 0 || arr[x, y - 1] < -1))
                            {
                                arr[x, y - 1] = i + 1;
                                count++;
                            }
                            if (x < gameField.Width - 1 && (arr[x + 1, y] == 0 || arr[x + 1, y] < -1))
                            {
                                arr[x + 1, y] = i + 1;
                                count++;
                            }
                            if (y < gameField.Heigth - 1 && (arr[x, y + 1] == 0 || arr[x, y + 1] < -1))
                            {
                                arr[x, y + 1] = i + 1;
                                count++;
                            }
                        }
                    }
                }

                i++;
            }

            //Console.WriteLine();
            //for (int y = 0; y < gameField.Heigth; y++)
            //{
            //    for (int x = 0; x < gameField.Width; x++)
            //    {
            //        Console.Write(arr[x, y] == -1 ? "#  " : arr[x, y].ToString() + " ");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadKey();

            shortestPath.Add(a);

            int t  = a.X;
            int v  = a.Y;
            int tx = 0;
            int ty = 0;

            i     = arr[a.X, a.Y];
            count = 0;
            while (true)
            {
                count = 0;
                if (t > 0 && arr[t - 1, v] == i - 1 && gameField[t - 1, v] == 0)
                {
                    tx = t - 1;
                    ty = v;
                    count++;
                }
                if (v > 0 && arr[t, v - 1] == i - 1 && gameField[t, v - 1] == 0)
                {
                    tx = t;
                    ty = v - 1;
                    count++;
                }
                if (t < gameField.Width - 1 && arr[t + 1, v] == i - 1 && gameField[t + 1, v] == 0)
                {
                    tx = t + 1;
                    ty = v;
                    count++;
                }
                if (t < gameField.Heigth - 1 && arr[t, v + 1] == i - 1 && gameField[t, v + 1] == 0)
                {
                    tx = t;
                    ty = v + 1;
                    count++;
                }

                if (count == 0)
                {
                    break;
                }
                shortestPath.Add(new Vector2D(tx, ty));
                t = tx;
                v = ty;
                i--;
            }

            if (shortestPath.Count < 2)
            {
                if (t > 0 && gameField[t - 1, v] == 0)
                {
                    tx = t - 1;
                    ty = v;
                }
                if (v > 0 && gameField[t, v - 1] == 0)
                {
                    tx = t;
                    ty = v - 1;
                }
                if (t < gameField.Width - 1 && gameField[t + 1, v] == 0)
                {
                    tx = t + 1;
                    ty = v;
                }
                if (t < gameField.Heigth - 1 && gameField[t, v + 1] == 0)
                {
                    tx = t;
                    ty = v + 1;
                }

                shortestPath.Add(new Vector2D(tx, ty));
            }
            return(shortestPath);
        }