Buscar

RomaniaMap1

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

RomaniaMap/.classpath
 
	 
		 
			 
		
	
	 
	 
RomaniaMap/.project
 
	 RomaniaMap
	 
	 
	
	 
		 
			 org.eclipse.jdt.core.javabuilder
			 
			
		
	
	 
		 org.eclipse.jdt.core.javanature
	
RomaniaMap/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=10
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=10
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=10
RomaniaMap/bin/RomaniaMAp/Action.class
RomaniaMap/bin/RomaniaMAp/Main.class
RomaniaMap/bin/RomaniaMAp/Menu.class
RomaniaMap/bin/RomaniaMAp/Model.class
RomaniaMap/bin/RomaniaMAp/Node.class
RomaniaMap/bin/RomaniaMAp/Node.java
package RomaniaMAp;
public class Node 
{
	private State state;
	private Node pai;
	private Action acdc;
	private int cust;
	
	public Node(){}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	public Node getPai() {
		return pai;
	}
	public void setPai(Node pai) {
		this.pai = pai;
	}
	public Action getAcdc() {
		return acdc;
	}
	public void setAcdc(Action acdc) {
		this.acdc = acdc;
	}
	public int getCust() {
		return cust;
	}
	public void setCust(int cust) {
		this.cust = cust;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if(o == null)
			return false;
		
		if(!(o instanceof Node))
			return false;
		
		if(o == this)
			return true;
		
		Node no = (Node) o;
		
		if(state.equals(no.getState()))
		{
			if(pai.equals(no.pai))
			{
				if(acdc.equals(no.getAcdc()))
				{
					if(cust == no.getCust())
						return true;
				
				}
			}
		}
		
		return false;
	}
}
RomaniaMap/bin/RomaniaMAp/State.class
RomaniaMap/src/RomaniaMAp/Action.java
	package RomaniaMAp;
public class Action 
{
	private State start;
	private State finish;
	private int custo;
	private int custoHeuristico;
	public Action(State ori, State dest, int custo, int ch)
	{
		this.start 	= ori;
		this.finish = dest;
		this.custo = custo;
		this.custoHeuristico = ch;
	}
	
	public int getCustoHeuristico() {
		return custoHeuristico;
	}
	public void setCustoHeuristico(int custoHeuristico) {
		this.custoHeuristico = custoHeuristico;
	}
	
	public void setStart(State s) 
	{
		this.start = s;
	}
	
	public State getStart()
	{
		return start;
	}
	
	public void setFinish(State f)
	{
		this.finish = f;
	}
	
	public State getFinish()
	{
		return finish;
	}
	
	public int getCusto() {
		return custo;
	}
	public void setCusto(int custo) {
		this.custo = custo;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(obj == null)
			return false;
		
		if(!(obj instanceof Action))
			return false
					;
		if(obj == this)
			return true;
		
		Action action = (Action) obj;
		
		if(this.start.equals(action.start))
		{
			if(this.finish.equals(action.finish) && action.getCusto() == this.custo)
				return true;
			else
				return false;
		}
		
		return false;
	}
	
	@Override
	public String toString()
	{
		String r = "(";
		
		r += start.getName() + ", " + finish.getName() + ", " + this.custo;
		r += ")";
		
		return r;
	}
	
}
RomaniaMap/src/RomaniaMAp/Main.java
package RomaniaMAp;
import java.util.ArrayList;
import java.util.Scanner;
public class Main 
{
	
	public static void main(String args[])
	{
		
		
	}
	
}
RomaniaMap/src/RomaniaMAp/Menu.java
package RomaniaMAp;
public class Menu 
{
	private int option;
	
	
	public Menu()
	{
		System.out.println("\t\t MENU");
		System.out.println("{1} - Adicionar cidade ao mapa.");
		System.out.println("{2} - Adicionar Cidade Vizinha.");
		System.out.println("{3} - Remover Ligação Entre Cidade.");
		System.out.println("{4} - Verificar se duas cidades são vizinhas.");
		System.out.println("{5} - Mostrar Mapa.");
		System.out.println("{6} - Listar Vizinhos");
		System.out.println("{7} - Sair");
		System.out.println("Operação: ");
	}
	
	public void clearScream()
	{
		for(int i = 0; i <= 10; i++)
		{
			System.out.println("\n");
		}
	}
	
	public int getOption()
	{
		return option;
	}
	
	public void setoOption(int op)
	{
		this.option = op;
	}
}
RomaniaMap/src/RomaniaMAp/Model.java
package RomaniaMAp;
import java.util.ArrayList;
import java.util.Stack;
public class Model 
{
	private ArrayList<Action> actions;
	private ArrayList<State> map;
	
	public Model()
	{
		this.actions = new ArrayList<Action>();
		this.map = new ArrayList<State>();
	}
	
	public void setActions(Action ac)
	{
		this.actions.add(ac);
	}
	
	public ArrayList<Action> getActions()
	{
		return this.getActions();
	}
	public ArrayList<State> getMap() {
		return map;
	}
	public void setMap(State m) {
		map.add(m);
	}
	
	public boolean hasState(State x)
	{
		if(x == null)
			return false;
		
		for(int i = 0; i < this.map.size(); i++)
		{
			if(map.get(i).equals(x))
			{
				return true;
			}
		}
		
		return false;
	}
	
	public void addState(String s)
	{
		
		State city = new State(s);
		if(this.hasState(city))
		{
			System.out.println("Erro: Cidade já existente!!!");
			return;
		}
		
		this.setMap(city);
		System.out.println("Cidade adicionada ao registro do mapa com sucesso!!!");
		
	}
	
	public void addAction(State v, State u, int custo)
	{
		
		
		for(Action c : this.actions)
		{
			if(c.equals(new Action(v, u, custo, 0)))
			{
				System.out.println("Erro: Ação ja existente!!!");
				return;
			}
		}
		
		Action act = new Action(v, u, custo, 0);
		actions.add(act);
		actions.add(new Action(u, v, custo, 0));
		
		v.setAdj(new Action(v, u, custo, 0));
		u.setAdj(new Action(u, v, custo, 0));
		
	}
	
	public void erase(State v, State u, int custo)
	{	
		for(int i = 0; i < actions.size(); i++)
		{
			if(actions.get(i).equals(new Action(v, u, custo, 0)))
			{
				actions.remove(actions.get(i));
			}
		}
		
	}
	
	
	
	//Busca em profundidade
	public ArrayList<Node> DepthFirstSearch(State ini, State f)
	{
		ArrayList<Node> soluction = new ArrayList<Node>();
		Node no = new Node();
		no.setState(ini);
		no.setCust(0);
		
		if(no.getState().equals(f))
		{
			soluction.add(no);
			return soluction;
		}
		
		Stack<Node> borda = new Stack<Node>();
		borda.push(no);
		
		ArrayList<State> explorados = new ArrayList<State>();
		
		while(true)
		{
			if(borda.empty())
				return null;
			no = borda.pop();
			explorados.add(no.getState());
			
			for(int i = 0; i < no.getState().getAdj().size(); i++)
			{
				Node filho = new Node(no.getState().getAdj().get(i).getFinish(), no, no.getCust() + no.getState().getAdj().get(i).getCusto(), no.getAcdc());
				
				boolean existe = false;
				
				for(State x : explorados) 
				{
					if(filho.getState().equals(x))
					{
						existe = true;
						break;
					}
				}
				
				if(existe == false)
				{
					if(filho.getState().equals(f))
					{
						soluction.add(filho);
						return soluction;
					}
					borda.push(filho);
				}
			}
			
		}
	}
	
	public void makeMap()
	{
		this.map.add(new State("ARAD"));
		this.map.add(new State("ZERIND"));
		this.map.add(new State("ORADEA"));
		this.map.add(new State("SIBIU"));
		this.map.add(new State("TIMISOARA"));
		this.map.add(new State("LUGOJ"));
		this.map.add(new State("MEHADIA"));
		this.map.add(new State("DROBETA"));
this.map.add(new State("CRAIOVA"));
		this.map.add(new State("FAGARAS"));
		this.map.add(new State("RIMNIEU VILCEA"));
		this.map.add(new State("PITESTI"));
		this.map.add(new State("BUCHAREST"));
		this.map.add(new State("GIURGIU"));
		this.map.add(new State("URZICENI"));
		this.map.add(new State("VASLUI"));
		this.map.add(new State("IASI"));
		this.map.add(new State("NEAMT"));
		this.map.add(new State("HIROVA"));
		this.map.add(new State("EFORIE"));
		
		this.actions.add(new Action(new State("ARAD"), new State("ZERIND"), 75));
		
		
		
	}
	//Busca Heurística
	
	
}
RomaniaMap/src/RomaniaMAp/Node.java
package RomaniaMAp;
public class Node 
{
	private State state;
	private Node pai;
	private Action acdc;
	private int cust;
	
	public Node(){}
	
	public Node(State s, Node p, int c, Action e)
	{
		this.state = s;
		this.pai = p;
		this.acdc = e;
		this.cust = c;
	}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	public Node getPai() {
		return pai;
	}
	public void setPai(Node pai) {
		this.pai = pai;
	}
	public Action getAcdc() {
		return acdc;
	}
	public void setAcdc(Action acdc) {
		this.acdc = acdc;
	}
	public int getCust() {
		return cust;
	}
	public void setCust(int cust) {
		this.cust = cust;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if(o == null)
			return false;
		
		if(!(o instanceof Node))
			return false;
		
		if(o == this)
			return true;
		
		Node no = (Node) o;
		
		if(state.equals(no.getState()))
		{
			if(pai.equals(no.pai))
			{
				if(acdc.equals(no.getAcdc()))
				{
					if(cust == no.getCust())
						return true;
				
				}
			}
		}
		
		return false;
	}
}
RomaniaMap/src/RomaniaMAp/State.java
package RomaniaMAp;
import java.util.ArrayList;
public class State 
{
	private String name;
	private ArrayList<Action> adj;
	
	public State(String n )
	{
		this.name = n;
		this.adj = new ArrayList<Action>();
	}
	
	public void setName(String n)
	{
		this.name = n;
	}
	
	public String getName()
	{
		return name;
	}
	
	//Adiciona um novo State vizinho
	public void setAdj(Action a)
	{
		this.adj.add(a);
	}
	
	public ArrayList<Action> getAdj()
	{
		return adj;
	}
	
	//Exibe todos os vizinhos
	public void showAdj()
	{
		System.out.print(name);
		System.out.print(" = [");
		
		for(int i = 0; i < adj.size(); i++)
		{
			if(i == adj.size() - 1)
				System.out.print(adj.get(i).getFinish().getName() + "]" );
			else
				System.out.print(adj.get(i).getFinish().getName() + ", ");
		}
		
		System.out.println();
		
	}
	
	
	
	//Remove Vizinho
	public boolean eraseAdj(State dead)
	{
		if(adj.isEmpty())
			return false;
			
		for(int i = 0; i < adj.size(); i++)
		{
			if(dead.equals(adj.get(i).getFinish()))
			{
				adj.remove(adj.get(i));
				return true;
			}
		}
		
		return false;
	}
	
	
	
	@Override
	public boolean equals(Object obj)
	{
		if(!(obj instanceof State))
			return false;
		
		if(this == obj)
			return true;
		
		State s = (State) obj;
		
		if(s.name == this.name)
			return true;
		else
			return false;
	}
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando