-
Notifications
You must be signed in to change notification settings - Fork 58
Luiz solução #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luizfernandodag
wants to merge
1
commit into
bempaggo:main
Choose a base branch
from
luizfernandodag:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Luiz solução #47
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Trabalho Jogo dos 8 e Metodos de busca | ||
* | ||
* Inteligencia Artificial - GCC128 | ||
* | ||
* Sistemas de Informação | ||
* Universidade Federal de Lavras | ||
* | ||
* Autor: Alfredo | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
|
||
|
||
public class AStar { | ||
State estadoInicial; | ||
State estadoFinal; | ||
PriorityQueue<State> pq; | ||
Queue<State> visitados; | ||
|
||
//construtor recebe o array atual e o final para ser utilizado no calculo do custo | ||
public AStar(ArrayList ini, ArrayList fin){ | ||
estadoInicial = new State(ini,fin); | ||
estadoFinal = new State(fin,fin); | ||
pq = new PriorityQueue<>(); | ||
visitados = new LinkedList<>(); | ||
pq.add(estadoInicial); | ||
} | ||
|
||
//resolve a instancia do problema | ||
public void solve(){ | ||
State estadoAtual; | ||
ArrayList<State> children; | ||
boolean flag; | ||
int countMovimentos = 0; | ||
int countAbertos = 0; | ||
|
||
while (!pq.isEmpty()){ | ||
estadoAtual = pq.poll(); | ||
this.visitados.add(estadoAtual); | ||
|
||
//imprime para teste | ||
countMovimentos++; // | ||
System.out.println("Movimento: " + countMovimentos); // | ||
estadoAtual.printState(); // | ||
System.out.println("Cost: " + estadoAtual.getCost()); // | ||
System.out.println(); // | ||
|
||
|
||
|
||
//compara o estado atual com a solução do problema | ||
if (estadoAtual.equals(this.estadoFinal)){ | ||
System.out.println("Resolucao encontrada:"); | ||
estadoAtual.printState(); | ||
System.out.println("Nos abertos repetidos: " + (countAbertos - countMovimentos)); // | ||
return; | ||
} | ||
|
||
|
||
//gera filhos e caso seja diferente de todos visitados, adiciona na fila de prioridade pq | ||
children = estadoAtual.genChildren(); | ||
for (State it : children){ | ||
flag = true; | ||
for(State it2 : this.visitados){ | ||
if (it.equals(it2)){ | ||
flag = false; | ||
break; | ||
} | ||
} | ||
if (flag){ | ||
State auxState = new State(it.n,estadoFinal.n); | ||
pq.add(auxState); | ||
countAbertos++; // | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Trabalho Jogo dos 8 e Metodos de busca | ||
* | ||
* Inteligencia Artificial - GCC128 | ||
* | ||
* Sistemas de Informação | ||
* Universidade Federal de Lavras | ||
* | ||
* | ||
* Autor: Alfredo | ||
* | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class BFS { | ||
|
||
State estadoInicial; | ||
State estadoFinal; | ||
Queue<State> q; | ||
Queue<State> visitados; | ||
|
||
|
||
//construtor | ||
public BFS(ArrayList ini, ArrayList fin){ | ||
estadoInicial = new State(ini); | ||
estadoFinal = new State(fin); | ||
q = new LinkedList<>(); | ||
visitados = new LinkedList<>(); | ||
q.add(estadoInicial); | ||
} | ||
|
||
//resolve essa instancia do problema | ||
public void solve(){ | ||
State estadoAtual; | ||
ArrayList<State> children; | ||
boolean flag; | ||
int countMovimentos = 0; | ||
int countAbertos = 0; | ||
|
||
while (!q.isEmpty()){ | ||
estadoAtual = q.remove(); | ||
this.visitados.add(estadoAtual); | ||
|
||
|
||
|
||
//imprime para teste | ||
countMovimentos++; // | ||
System.out.println("Movimento: " + countMovimentos); // | ||
estadoAtual.printState(); // | ||
System.out.println(); // | ||
|
||
|
||
|
||
//compara o estado atual com a solução do problema | ||
if (estadoAtual.equals(this.estadoFinal)){ | ||
System.out.println("Resolucao encontrada:"); | ||
estadoAtual.printState(); | ||
System.out.println("Nos abertos repetidos: " + (countAbertos - countMovimentos)); // | ||
return; | ||
} | ||
|
||
|
||
//gera filhos e caso seja diferente de todos visitados, adiciona na fila q | ||
children = estadoAtual.genChildren(); | ||
for (State it : children){ | ||
flag = true; | ||
for(State it2 : this.visitados){ | ||
if (it.equals(it2)){ | ||
flag = false; | ||
break; | ||
} | ||
} | ||
if (flag){ | ||
q.add(it); | ||
countAbertos++; // | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Trabalho Jogo dos 8 e Metodos de busca | ||
* | ||
* Inteligencia Artificial - GCC128 | ||
* | ||
* Sistemas de Informação | ||
* Universidade Federal de Lavras | ||
* | ||
* | ||
* Autor: Alfredo | ||
* | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
// TODO code application logic here | ||
ArrayList<Integer> estadoInicial = new ArrayList<>(); | ||
ArrayList<Integer> estadoFinal = new ArrayList<>(); | ||
|
||
|
||
estadoInicial.add(1); | ||
estadoInicial.add(2); | ||
estadoInicial.add(3); | ||
estadoInicial.add(5); | ||
estadoInicial.add(0); | ||
estadoInicial.add(6); | ||
estadoInicial.add(4); | ||
estadoInicial.add(7); | ||
estadoInicial.add(8); | ||
|
||
|
||
estadoFinal.add(1); | ||
estadoFinal.add(2); | ||
estadoFinal.add(3); | ||
estadoFinal.add(4); | ||
estadoFinal.add(5); | ||
estadoFinal.add(6); | ||
estadoFinal.add(7); | ||
estadoFinal.add(8); | ||
estadoFinal.add(0); | ||
|
||
State si = new State(estadoInicial); | ||
State sf = new State(estadoFinal); | ||
|
||
Scanner scanner; | ||
Scanner scanner2; | ||
int option; | ||
int qtdMoves; | ||
|
||
do{ | ||
System.out.println("Estado inicial:"); | ||
si.printState(); | ||
System.out.println(); | ||
|
||
System.out.println("Solucao a ser procurada:"); | ||
sf.printState(); | ||
System.out.println(); | ||
|
||
System.out.println("Digite:"); | ||
System.out.println("1 - Resolver por busca em largura (BFS)"); | ||
System.out.println("2 - Resolver por busca A*"); | ||
System.out.println("3 - Randomizar tabuleiro"); | ||
|
||
scanner = new Scanner(System.in); | ||
|
||
option = scanner.nextInt(); | ||
|
||
switch (option) { | ||
case 1 -> { | ||
BFS bfs = new BFS(si.n,sf.n); | ||
bfs.solve(); | ||
} | ||
case 2 -> { | ||
AStar astar = new AStar(si.n,sf.n); | ||
astar.solve(); | ||
} | ||
|
||
case 3 -> { | ||
System.out.println("Quantos movimentos aleatorios deseja realizar?"); | ||
scanner2 = new Scanner(System.in); | ||
qtdMoves = scanner2.nextInt(); | ||
si.randomize(qtdMoves); | ||
} | ||
|
||
default -> System.out.println("Digite uma opção va3lida"); | ||
} | ||
}while (!(option==1 || option==2)); | ||
|
||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boa tarde,
Não estamos buscando um resolvedor de jogo de oito automático e sim uma solução onde podemo jogá-lo.
Att