Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion jogo-oito/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions jogo-oito/.settings/org.eclipse.core.resources.prefs

This file was deleted.

8 changes: 0 additions & 8 deletions jogo-oito/.settings/org.eclipse.jdt.core.prefs

This file was deleted.

4 changes: 0 additions & 4 deletions jogo-oito/.settings/org.eclipse.m2e.core.prefs

This file was deleted.

84 changes: 84 additions & 0 deletions jogo-oito/src/main/java/AStar.java
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++; //
}
}
}
}



}
84 changes: 84 additions & 0 deletions jogo-oito/src/main/java/BFS.java
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 added jogo-oito/src/main/java/Enunciado.pdf
Binary file not shown.
96 changes: 96 additions & 0 deletions jogo-oito/src/main/java/Main.java
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();
Copy link
Contributor

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

}
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));


}

}
Loading