6
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Recorrido_de_Hormiga { public partial class Form1 : Form { //Label[,] matriz = new Label[100,100]; int c = 0; int op; Label[,] matriz=new Label[100,100]; private int acumuladorgiros; private int acumulamovi; int XH;// posicionx de la hormiga int YH;// int x1;//dimensiones del area int y1;// public Form1() { InitializeComponent(); } private void btnGenerar_Click(object sender, EventArgs e)//genera el esp acio matricial { x1 = Convert.ToInt32(txtX.Text); y1 = Convert.ToInt32(txtY.Text); for(int i=0;i< x1; i++) { for(int j=0;j< y1; j++) { CrearCelda(i, j); c++; } } btnGenerar.Enabled = false; btnBorrar.Enabled = true; } private void CrearCelda(int x,int y)//creamos cada label en el espacio m atricial { matriz[x,y] = new Label(); matriz[x, y].Name = "p" + c; matriz[x, y].BorderStyle = BorderStyle.FixedSingle; matriz[x, y].Location = new Point(x*20, y*20); matriz[x, y].Size = new Size(20, 20); matriz[x, y].Text = ""; matriz[x, y].Tag =0;// sin feromona matriz[x, y].Image = global::Recorrido_de_Hormiga.Properties.Resourc es.Vacio;

Recorrido de La Hormiga

Embed Size (px)

DESCRIPTION

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;

Citation preview

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;

namespace Recorrido_de_Hormiga{ public partial class Form1 : Form { //Label[,] matriz = new Label[100,100]; int c = 0; int op; Label[,] matriz=new Label[100,100]; private int acumuladorgiros; private int acumulamovi; int XH;// posicionx de la hormiga int YH;// int x1;//dimensiones del area int y1;//

public Form1() { InitializeComponent(); }

private void btnGenerar_Click(object sender, EventArgs e)//genera el espacio matricial { x1 = Convert.ToInt32(txtX.Text); y1 = Convert.ToInt32(txtY.Text); for(int i=0;i< x1; i++) { for(int j=0;j< y1; j++) { CrearCelda(i, j); c++; } } btnGenerar.Enabled = false; btnBorrar.Enabled = true; } private void CrearCelda(int x,int y)//creamos cada label en el espacio matricial { matriz[x,y] = new Label(); matriz[x, y].Name = "p" + c; matriz[x, y].BorderStyle = BorderStyle.FixedSingle; matriz[x, y].Location = new Point(x*20, y*20); matriz[x, y].Size = new Size(20, 20); matriz[x, y].Text = ""; matriz[x, y].Tag =0;// sin feromona matriz[x, y].Image = global::Recorrido_de_Hormiga.Properties.Resources.Vacio;

matriz[x, y].Image.Tag = 0; //sin recorrido pnlMatriz.Controls.Add(matriz[x, y]);

}

private void btnBorrar_Click(object sender, EventArgs e) { pnlMatriz.Dispose(); btnGenerar.Enabled = true; btnBorrar.Enabled = false; txtX.Text = txtY.Text = "0"; c = 0; }

private void btnPosHormiga_Click(object sender, EventArgs e) { }

private void btnAgregarH_Click(object sender, EventArgs e)// agregamos a la hormiga { XH = Convert.ToInt32(txtXH.Text) - 1; YH = Convert.ToInt32(txtYH.Text) - 1; matriz[XH, YH].Image = down(); matriz[XH, YH].Tag = 3;

} private void MoverHormiga(int x, int y) { op = Convert.ToInt32(matriz[x,y].Tag); int vr=0; switch (op) { case 2://Cuando la hormiga mira al este {

vr = ObtenerPeso(x + 1, y); if (acumuladorgiros == 4) { vr = 1; } if (vr==0) { matriz[x, y].Image = down(); matriz[x, y].Tag = 3; acumuladorgiros += 1; } else if (vr==1) { acumuladorgiros = 0; moverEste(); }

acumulamovi += 1; break; } case 3://Cuando la hormiga mira hacia abajo. { vr = ObtenerPeso(x , y+1); if (acumuladorgiros == 4) { vr = 1; } if (vr == 0) { matriz[x, y].Image = oeste(); matriz[x, y].Tag = 4; acumuladorgiros += 1; } else if (vr == 1) { moverAbajo(); acumuladorgiros = 0; }

acumulamovi += 1; break; } case 4: {//Cuando la hormiga mira al oeste. vr = ObtenerPeso(x - 1, y); if (acumuladorgiros == 4) { vr = 1; } if (vr == 0) { matriz[x, y].Image = norte(); matriz[x, y].Tag = 5; acumuladorgiros += 1; } else if (vr == 1) { moverOeste(); acumuladorgiros = 0; }

acumulamovi += 1; break; } case 5: {//Cuando la hormiga mira al norte. vr = ObtenerPeso(x, y-1); if (acumuladorgiros == 4) { vr = 1; } if (vr == 0) { matriz[x, y].Image = este();

matriz[x, y].Tag = 2; acumuladorgiros += 1; } else if (vr == 1) { moverNorte(); acumuladorgiros = 0; } acumulamovi += 1; break; }

} }

private void moverNorte() { matriz[XH, YH].Tag = 1; matriz[XH, YH].Image = uso(); matriz[XH, YH].Image.Tag = 1; XH = XH; YH = YH - 1; matriz[XH, YH].Tag = 5; // MoverHormiga(XH, YH); matriz[XH, YH].Image = norte(); }

private Image este() { Image g = global::Recorrido_de_Hormiga.Properties.Resources.HDerecha; return g; }

private void moverOeste() { matriz[XH, YH].Tag = 1; matriz[XH, YH].Image = uso(); matriz[XH, YH].Image.Tag = 1; XH = XH-1; YH = YH; matriz[XH, YH].Tag = 4; //MoverHormiga(XH, YH); matriz[XH, YH].Image = oeste(); }

private Image norte() { Image g = global::Recorrido_de_Hormiga.Properties.Resources.HArriba; return g; }

private void moverAbajo() {

matriz[XH, YH].Tag = 1; matriz[XH, YH].Image = uso(); matriz[XH, YH].Image.Tag = 1; XH = XH; YH = YH+1; matriz[XH, YH].Tag = 3; //MoverHormiga(XH, YH); matriz[XH, YH].Image = down(); }

private Image oeste() { Image g = global::Recorrido_de_Hormiga.Properties.Resources.HIzquierda; return g; }

private void moverEste() { matriz[XH, YH].Tag = 1; matriz[XH, YH].Image = uso(); matriz[XH, YH].Image.Tag = 1; XH = XH+1; YH = YH; //MoverHormiga(XH, YH); matriz[XH, YH].Tag = 2; matriz[XH, YH].Image = este(); }

private Image down() { Image g = global::Recorrido_de_Hormiga.Properties.Resources.HAbajo; return g; }

private Image uso() { Image g = global::Recorrido_de_Hormiga.Properties.Resources.usado; return g; }

private void btnMover_Click(object sender, EventArgs e) { MoverHormiga(XH, YH); }

private void btnAgregarF_Click(object sender, EventArgs e)// agregamos feromonas random { int h; int j; Random r = new Random(); for(int i=0; i <= Convert.ToInt32(txtCantidadF.Text); i++) { h = r.Next(1, x1-1); j = r.Next(1, y1-1); matriz[h, j].Tag = 1; matriz[h, j].Text = "F";

} } private int ObtenerPeso(int x, int y) //obtenemos el peso de la decision, 0 gira y 1 avanza { int F = Convert.ToInt32(matriz[x , y].Tag); int R; if (matriz[x , y].Image.Tag.ToString() == "1") R = 0; else R = 1; int rn = F * R; return rn; } }}