private Point ConseguirUbicacionEnLaMatriz(Celda celda)
 {
     for (int i = 0; i < columnas; i++)
     {
         for (int j = 0; j < filas; j++)
         {
             if (celda == celdas[i, j])
             {
                 return(new Point(i, j));
             }
         }
     }
     return(new Point(0, 0));
 }
예제 #2
0
        public FormAgente(Celda[][] laberinto, Celda entrada, Celda salida)
        {
            agente = new MiAgente();

            this.entrada   = entrada;
            jugador        = entrada;
            this.salida    = salida;
            this.laberinto = laberinto;

            InitializeComponent();

            altoDeCelda      = (panel1.Height / laberinto.Length) - 1;
            anchoDeCelda     = (panel1.Width / laberinto.Length) - 1;
            panel1.BackColor = Color.Black;
        }
        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            Celda siguiente = null;

            switch (e.KeyCode)
            {
            case Keys.Right: siguiente = new Celda(jugador.y, jugador.x + 1); break;

            case Keys.Left: siguiente = new Celda(jugador.y, jugador.x - 1); break;

            case Keys.Up: siguiente = new Celda(jugador.y - 1, jugador.x); break;

            case Keys.Down: siguiente = new Celda(jugador.y + 1, jugador.x); break;

            default: return;
            }


            if ((siguiente.y >= 0 && siguiente.y < laberinto.Length) && (siguiente.x >= 0 && siguiente.x < laberinto.Length))
            {
                siguiente = laberinto[siguiente.y][siguiente.x];

                if (jugador.vecinos.Contains(siguiente) || siguiente.vecinos.Contains(jugador))
                {
                    Rectangle r = new Rectangle((jugador.x * anchoDeCelda) + 3, (jugador.y * altoDeCelda) + 3, altoDeCelda - 6, altoDeCelda - 6);
                    Graphics  g = panel1.CreateGraphics();

                    g.DrawEllipse(Pens.Black, r);
                    jugador = siguiente;
                    r       = new Rectangle((jugador.x * anchoDeCelda) + 3, (jugador.y * altoDeCelda) + 3, altoDeCelda - 6, altoDeCelda - 6);
                    g.DrawEllipse(Pens.Red, r);

                    if (salida.y == siguiente.y && salida.x == siguiente.x)
                    {
                        timer1.Enabled = false;
                        if (!esContrarreloj)
                        {
                            fa.Close();
                        }
                        MessageBox.Show("ganaste");

                        this.Dispose();
                    }
                }
            }
        }
        private static void GenerarLaberinto(Celda actual, Celda[][] laberinto)
        {
            actual.visitado = true;

            while (actual.adyacente.Count > 0)
            {
                int   siguiente = r.Next(0, actual.adyacente.Count);
                Celda c         = actual.adyacente[siguiente];
                actual.adyacente.RemoveAt(siguiente);

                if (!c.visitado)
                {
                    actual.vecinos.Add(c);
                    c.vecinos.Add(actual);
                    c.adyacente.Remove(actual);
                    GenerarLaberinto(c, laberinto);
                }
            }
        }
        public void DefinirArregloDeCeldasSegunTamanio(int opcionDeTamanio, Size panel)
        {
            this.opcionDeTamanio = opcionDeTamanio;
            switch (opcionDeTamanio)
            {
            case 0:
                filas    = 3;
                columnas = 8;
                break;

            case 1:
                filas    = 5;
                columnas = 11;
                break;

            case 2:
                filas    = 8;
                columnas = 18;
                break;

            default:
                break;
            }
            int width  = (int)Math.Floor((double)panel.Width / columnas);
            int height = (int)Math.Floor((double)panel.Height / filas);

            this.tamanio = new Size(width, height);
            this.margen  = new Size((panel.Width % (columnas * tamanio.Width)) / 2,
                                    (panel.Height % (filas * tamanio.Height)) / 2);
            celdas = new Celda[columnas, filas];
            for (int i = 0; i < columnas; i++)
            {
                for (int j = 0; j < filas; j++)
                {
                    celdas[i, j] = new Celda(graphics, lapiz, new Point(((i * tamanio.Width) + this.margen.Width),
                                                                        ((j * tamanio.Height) + this.margen.Height)), tamanio);
                }
            }
            pila = new PilaDeCeldas(celdas.Length);
        }
        private static void AñadirAdyacentes(Celda c, Celda[][] laberinto)
        {
            if (c.y > 0 && c.y < dimension - 1)
            {
                c.adyacente.Add(laberinto[c.y + 1][c.x]);
                c.adyacente.Add(laberinto[c.y - 1][c.x]);
            }
            else
            {
                if (c.y == 0)
                {
                    c.adyacente.Add(laberinto[c.y + 1][c.x]);
                }
                else
                {
                    c.adyacente.Add(laberinto[c.y - 1][c.x]);
                }
            }

            if (c.x > 0 && c.x < dimension - 1)
            {
                c.adyacente.Add(laberinto[c.y][c.x + 1]);
                c.adyacente.Add(laberinto[c.y][c.x - 1]);
            }
            else
            {
                if (c.x == 0)
                {
                    c.adyacente.Add(laberinto[c.y][c.x + 1]);
                }
                else
                {
                    c.adyacente.Add(laberinto[c.y][c.x - 1]);
                }
            }
        }
예제 #7
0
        public Form1(Celda[][] laberinto)
        {
            InitializeComponent();

            this.laberinto = laberinto;
            tableLayoutPanel1.SuspendLayout();
            tableLayoutPanel1.ColumnCount = laberinto.Length;
            tableLayoutPanel1.RowCount    = laberinto.Length;

            tableLayoutPanel1.ColumnStyles.Clear();
            tableLayoutPanel1.RowStyles.Clear();

            for (int i = 0; i < laberinto.Length; i++)
            {
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, tableLayoutPanel1.Width / laberinto.Length));
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, tableLayoutPanel1.Height / laberinto.Length));
            }

            for (int y = 0; y < laberinto.Length; y++)
            {
                for (int x = 0; x < laberinto.Length; x++)
                {
                    Celda actual = laberinto[y][x];


                    Panel p = new Panel();
                    p.Margin = new Padding(0);

                    Label arriba    = LabelArriba();
                    Label abajo     = LabelAbajo();
                    Label izquierda = LabelIzquierda();
                    Label derecha   = LabelDerecha();

                    p.Controls.Add(arriba);
                    p.Controls.Add(abajo);
                    p.Controls.Add(izquierda);
                    p.Controls.Add(derecha);

                    foreach (Celda v in actual.vecinos)
                    {
                        if (v.x == actual.x)
                        {
                            if (v.y < actual.y)
                            {
                                arriba.Dispose();
                            }
                            else
                            {
                                abajo.Dispose();
                            }
                        }

                        if (v.y == actual.y)
                        {
                            if (v.x < actual.x)
                            {
                                izquierda.Dispose();
                            }
                            else
                            {
                                derecha.Dispose();
                            }
                        }
                    }
                    tableLayoutPanel1.Controls.Add(p, x, y);
                }
            }
            tableLayoutPanel1.ResumeLayout();
        }