public void Pathfind(Agente agent, Estado inicial, Estado final, Labirinto maze) { Console.WriteLine("Você quer ver a animação? 0 pra não E 1 pra sim"); mostrarAnimacao = Convert.ToInt32(Console.ReadLine()); if (mostrarAnimacao == 1) { maze.mostraLabirinto(); } inicial.h = distEuclidiana(inicial, final); inicial.g = 0; inicial.f = inicial.g + inicial.h; inicial.pai = null; Estado atual; Estado antigo; Estado auxiliar; atual = inicial; antigo = atual; while (atual.posX != final.posX || atual.posY != final.posY) { Console.Clear(); agent.ir(maze, atual, antigo, mostrarAnimacao); if (mostrarAnimacao == 1) { maze.mostraLabirinto(); System.Threading.Thread.Sleep(150); Console.Clear(); } if (atual.posX == final.posX && atual.posY == final.posY) { break; } Vizinhos = olhaVizinhos(maze, atual, final, agent); Vizinhos.Sort((x, y) => x.f.CompareTo(y.f)); // sort auxiliar = Vizinhos[0]; if (auxiliar.f <= atual.f) { antigo = atual; // pega a posição anterior atual = Vizinhos[0]; } Vizinhos.Clear(); } maze.mostraCaminho(atual, maze); //Console.Clear(); maze.mostraLabirinto(); }
public void mostraCaminho(Estado state, Labirinto maze) { List <Estado> Invertida; Invertida = new List <Estado>(); Estado atual, ondeEstou; while (state.pai != null) { Invertida.Add(state); //m[state.posX, state.posY] = "@"; state = state.pai; } Invertida.Add(state); //m[state.posX, state.posY] = "@"; Invertida.Reverse(); while (Invertida.Any()) { Console.WriteLine("Deseja andar ou ver posição? 0 - Andar 1 - Ver Posição"); acao = Convert.ToInt32(Console.ReadLine()); if (acao == 0) { Console.Clear(); atual = Invertida[0]; Invertida.RemoveAt(0); if (Invertida.Any()) { ondeEstou = Invertida[0]; } else { break; } m[ondeEstou.posX, ondeEstou.posY] = "A"; m[atual.posX, atual.posY] = "@"; maze.mostraLabirinto(); } else { Console.Clear(); atual = Invertida[0]; m[atual.posX, atual.posY] = "A"; Console.WriteLine("Posição: X: {0}, Y: {1}", atual.posX, atual.posY); maze.mostraLabirinto(); } } }
public void Pathfind(Agente agent, Estado inicial, Estado final, Labirinto maze) { Console.WriteLine("Você quer ver a animação? 0 pra não E 1 pra sim"); mostrarAnimacao = Convert.ToInt32(Console.ReadLine()); if (mostrarAnimacao == 1) { maze.mostraLabirinto(); } inicial.h = distEuclidiana(inicial, final); inicial.g = 0; inicial.pai = null; Aberta.Add(inicial); Estado atual; Estado antigo; atual = inicial; while (Aberta.Any()) { Console.Clear(); Aberta.Sort((x, y) => x.f.CompareTo(y.f)); // sort antigo = atual; atual = Aberta[0]; Aberta.RemoveAt(0); Fechada.Add(atual); agent.ir(maze, atual, antigo, mostrarAnimacao); if (mostrarAnimacao == 1) { maze.mostraLabirinto(); System.Threading.Thread.Sleep(150); Console.Clear(); } if (atual.posX == final.posX && atual.posY == final.posY) { break; } adicionaVizinhos(maze, atual, final, agent); } maze.mostraCaminho(atual, maze); //Console.Clear(); maze.mostraLabirinto(); }
static void Main(string[] args) { int linhas, colunas, x1, y1, x2, y2, posxChegada, posyChegada, posxAgente, posyAgente, obstaculo, algoritmo; bool fechou1 = false; bool fechou2 = false; Agente agent = new Agente(); Estado estadoFinal = new Estado(); Estado inicial = new Estado(); AEstrela aestrela = new AEstrela(); HillClimbing hillclimbing = new HillClimbing(); Console.WriteLine("Numero de Linhas: "); linhas = Convert.ToInt32(Console.ReadLine()) + 1; Console.WriteLine("Numero de Colunas: "); colunas = Convert.ToInt32(Console.ReadLine()) + 1; Labirinto maze = new Labirinto(linhas, colunas); maze.preencheLabirinto(); while (!fechou1) { Console.Clear(); maze.mostraLabirinto(); Console.WriteLine("\nDeseja inserir um obstáculo na horizontal ou na vertical? horizontal(1) vertical(0)\n"); Console.WriteLine("\n(1) Horizontal\n"); Console.WriteLine("\n(2) Vertical\n"); obstaculo = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\nInsira as coordenadas do obstaculo, uma de cada vez, no formato (x1,y1), (x2,y2)\n"); Console.WriteLine("X1: "); x1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Y1: "); y1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("X2: "); x2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Y2: "); y2 = Convert.ToInt32(Console.ReadLine()); if (obstaculo == 1) { if (maze.geraObstatuloHoriz(x1, y1, x2, y2) == 1) { Console.Clear(); maze.mostraLabirinto(); Console.WriteLine("Deseja inserir outro obstaculo? sim(1) não(0)\n"); if (Convert.ToInt32(Console.ReadLine()) == 0) { fechou1 = true; } } } else if (obstaculo == 2) { if (maze.geraObstatuloVertical(x1, y1, x2, y2) == 1) { Console.Clear(); maze.mostraLabirinto(); Console.WriteLine("Deseja inserir outro obstaculo? sim(1) não(0)\n"); if (Convert.ToInt32(Console.ReadLine()) == 0) { fechou1 = true; } } } } Console.WriteLine(); while (!fechou2) { Console.WriteLine("Posição de chegada (x,y): "); Console.WriteLine("PosX: "); posxChegada = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("PosY: "); posyChegada = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Posição do Agente (x,y): "); Console.WriteLine("PosX: "); posxAgente = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("PosY: "); posyAgente = Convert.ToInt32(Console.ReadLine()); if (maze.posicionaChegada(posxChegada, posyChegada) && agent.entraLabirinto(maze, posxAgente, posyAgente)) { inicial.posX = posxAgente; inicial.posY = posyAgente; inicial.f = 0; inicial.g = 0; inicial.pai = null; estadoFinal.posX = posxChegada; estadoFinal.posY = posyChegada; estadoFinal.g = 0; estadoFinal.f = 0; estadoFinal.pai = null; fechou2 = true; } } maze.mostraLabirinto(); Console.Clear(); Console.WriteLine("Qual algoritmo irá rodar? 0 - A* 1 - Hill Climbing "); algoritmo = Convert.ToInt32(Console.ReadLine()); if (algoritmo == 0) { aestrela.Pathfind(agent, inicial, estadoFinal, maze); } else if (algoritmo == 1) { hillclimbing.Pathfind(agent, inicial, estadoFinal, maze); } /*bool achou = false; * int k; * * while (!achou) * { * k = agent.deliberar(maze); * if (k == 0) * { * achou = true; * * } * else * { * System.Threading.Thread.Sleep(150); * Console.Clear(); * } * * }*/ System.Threading.Thread.Sleep(200); Console.WriteLine("\n Achou\n"); System.Threading.Thread.Sleep(10000); }