Nombre de clase: SolverStarter
Formato de entrada => 123456780, no es permitida una entrada de longitud menor a 9 o mayor a 9. Si 0 no es encontrado, es negado el derecho a usar el software
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author 176995637
*/
class Node{
public Node(int[][] state){
this.state = state;
this.children = new ArrayList<Node>();
}
//get and set for state and children
public int[][] getState(){
return state;
}
public void setState(int[][] state){
this.state = state;
}
public void prependChild(Node node){
this.children.add(0, node);
}
public void appendChild(Node node){
this.children.add(node);
}
public ArrayList<Node> getChildren(){
return this.children;
}
public void setChildren(ArrayList<Node> children){
this.children = children;
}
public String toString(){
String result = "╔═════╗\n";
for(int[] y : state){
result += "║ ";
for(int x : y){
result += x + " ";
}
result += " ║\n";
}
result += "╚═════╝";
return result;
}
private int[][] state;
private ArrayList<Node> children;
}
class Solver{
public Solver(String initState) throws Exception{
in = new Scanner(System.in);
int[][] init_state = toArray(initState);
Node a = new Node(init_state);
this.root = a;
node_children = new ArrayList<Node>();
}
public final int[][] toArray(String initstate) throws Exception{
int [][] result = new int[3][3];
int i;
int j;
int cont = 0;
char digit;
boolean zeroFound = false;
for(i=0; i < 3; i++){
for(j=0; j < 3 ; j++){
digit = initstate.charAt(cont);
result[i][j] = Character.getNumericValue(digit);
cont++;
if(result[i][j] == 0)
zeroFound= true;
}
}
if(!zeroFound)
throw new Exception("Zero not found");
return result;
}
public int[][] clone_state(int[][] state){
int [][] clone = new int[state.length][];
for(int i = 0; i < state.length; i++)
{clone[i] = state[i].clone();}
return clone;
}
public ArrayList<Node> generateSuccessors(){
int[] empty_index = new int[2];
Node child;
int[][] state = root.getState();
for(int empty_row=0; empty_row < state.length; empty_row++){
for(int empty_column=0;empty_column < state[empty_row].length; empty_column++){
if(state[empty_row][empty_column] == 0){
empty_index[0] = empty_row;
empty_index[1] = empty_column;
}
}
}
int row = empty_index[0];
int column = empty_index[1];
//System.out.println("x is " +empty_index[1] +"\ny is " + empty_index[0]);
int actual_up_value, actual_down_value, actual_left_value, actual_right_value;
if(row!=0){
child = new Node(clone_state(state));
actual_up_value = child.getState()[row-1][column];
child.getState()[row][column] = actual_up_value;
child.getState()[row-1][column] = 0;
node_children.add(child);
}
if(row!=2){
child = new Node(clone_state(state));
actual_down_value = child.getState()[row+1][column];
child.getState()[row][column] = actual_down_value;
child.getState()[row+1][column] = 0;
node_children.add(child);
}
if(column!=0){
child = new Node(clone_state(state));
actual_left_value = child.getState()[row][column-1];
child.getState()[row][column] = actual_left_value;
child.getState()[row][column-1] = 0;
node_children.add(child);
}
if(column!=2){
child = new Node(clone_state(state));
actual_right_value = child.getState()[row][column+1];
child.getState()[row][column] = actual_right_value;
child.getState()[row][column+1] = 0;
node_children.add(child);
}
return node_children;
}
public Node getRoot(){
return root;
}
public void printNode(Node aNode){
System.out.println(aNode);
}
public void printChildren(ArrayList<Node> nodes){
for(Node e : nodes){
System.out.println(e);
}
}
private Node root;
private int[][] goal_state;
private ArrayList<Node> node_children;
private Scanner in;
private int states_nuevos;
}
public class SolverStarter{
public static void main(String args[]) throws Exception{
ArrayList<Node> successors;
Node root;
Scanner in = new Scanner(System.in);
int contador = 0;
String initState;
initState = in.nextLine();
if(initState.length()>9 || initState.length()<9){
throw new Exception("Input fuera de rango");
}
Solver solver = new Solver(initState);
root = solver.getRoot();
successors = solver.generateSuccessors();
System.out.println("Printing init state:");
solver.printNode(root);
System.out.println("Printing children:");
solver.printChildren(successors);
}
}
Integrantes:
Iván Barrera
José Sáez
Rodrigo Lagos
Cesar Adriazola
Link al tema del grupo:
https://groups.google.com/forum/?hl=es&fromgroups=#!topic/si-181/7RVmIuYYZtQ
No hay comentarios:
Publicar un comentario