Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.96 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package rushhour;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Component;
  10. import java.awt.Graphics;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Vector;
  14. import javax.swing.JFrame;
  15.  
  16. public class RushHour {
  17.     public boolean keep_going = true;
  18.     public ArrayList<Panel> panelList = new ArrayList();
  19.     public HashMap<Color, Car> carsByColor = new HashMap<>();
  20.     public HashMap<Integer, Panel> panelByCoordinates = new HashMap<>();
  21.     public static Vector<Move> PossibleMoves = new Vector<>();
  22.     public static RushHourFrame1 mainframe = new RushHourFrame1();
  23.     public int choise;
  24.     public boolean reset_set = false;
  25.     public static Color [] reset_color;
  26.     public static Vector<Vector<Integer>> moves = new Vector<Vector<Integer>>();
  27.     public static Vector<Integer> new_array = new Vector<>();
  28.     /**
  29.      * @param args the command line arguments
  30.      */
  31.     public static void main(String[] args) {
  32. // TODO code application logic here
  33.         reset_color = new Color[36];
  34.         Picker newpicker = new Picker();
  35.         newpicker.setVisible(true);
  36.  
  37.     }
  38.  
  39.     public void startapp() {
  40.         if (choise == -1) {
  41.             mainframe.dispose();
  42.         }
  43.         if (choise == 0) {
  44.             RushHourFrame2 newframe = new RushHourFrame2();
  45.             for (Component panel : mainframe.jPanel1.getComponents()) {
  46.                 for (Component panelcompare : newframe.jPanel1.getComponents()) {
  47.                     if (panel.getName().equals(panelcompare.getName())) {
  48.                         panel.setBackground(panelcompare.getBackground());
  49.                     }
  50.                 }
  51.             }
  52.             newframe.dispose();
  53.             mainframe.setVisible(true);
  54.         }
  55.         if (choise == 1) {
  56.             RushHourFrame3 newframe = new RushHourFrame3();
  57.             for (Component panel : mainframe.jPanel1.getComponents()) {
  58.                 for (Component panelcompare : newframe.jPanel1.getComponents()) {
  59.                     if (panel.getName().equals(panelcompare.getName())) {
  60.                         panel.setBackground(panelcompare.getBackground());
  61.                     }
  62.                 }
  63.             }
  64.             newframe.dispose();
  65.             mainframe.setVisible(true);
  66.         }
  67.         if (choise == 2) {
  68.             RushHourFrame4 newframe = new RushHourFrame4();
  69.             for (Component panel : mainframe.jPanel1.getComponents()) {
  70.                 for (Component panelcompare : newframe.jPanel1.getComponents()) {
  71.                     if (panel.getName().equals(panelcompare.getName())) {
  72.                         panel.setBackground(panelcompare.getBackground());
  73.                     }
  74.                 }
  75.             }
  76.             newframe.dispose();
  77.             mainframe.setVisible(true);
  78.         }
  79.     }
  80.     public void resetWindow(){
  81.         int counter = 0;
  82.         for (Panel panel : panelList){
  83.             panel.name.setBackground(reset_color[counter]);
  84.             counter++;
  85.         }
  86.     }
  87.     public void stop_searching(){
  88.         keep_going = false;
  89.     }
  90.  
  91.     public void breadthfirstsearch() {
  92.         Vector<Integer> temp_arraylist = new Vector<>();
  93.         createPanelList();
  94.         linkcoordinates();
  95.         recognizeCars();    
  96.         MoveWhichWay();
  97.         CanMoveWhere(); //Initial search to get the first level of moves
  98.         for (Move move : PossibleMoves){
  99.             Vector<Integer> newlist = new Vector<Integer>();
  100.             newlist.add(PossibleMoves.indexOf(move));
  101.             moves.add(newlist); //Save those moves an an integer
  102.         }  
  103.         int counter = 0;
  104.         while (true){ //Keep going untill the red car is out.
  105.             temp_arraylist.clear();
  106.             temp_arraylist = moves.get(counter);    //Go through all the first steps
  107.             resetWindow();  //Make sure evry car is in the begin position
  108.             for(Integer c : temp_arraylist){   //Redo the old steps first
  109.                 createPanelList();
  110.                 linkcoordinates();
  111.                 recognizeCars();
  112.                 MoveWhichWay();
  113.                 MakeMove(c);
  114.             }
  115.            
  116.             createPanelList();
  117.             linkcoordinates();
  118.             recognizeCars();
  119.             MoveWhichWay(); //See if each car can move left/right or up/down
  120.             CanMoveWhere(); //Create new steps into PossibleMoves()
  121.  
  122.             for (Move next_move: PossibleMoves){    //For each of these new steps we create a new array
  123.                 new_array.clear();  //Make sure it's empty before adding moves
  124.                 new_array = moves.get(counter); //Get the old steps to get here
  125.                 new_array.add(PossibleMoves.indexOf(next_move));    //Add the new step and put it into the moves array
  126.                 moves.add(new_array);
  127.             }
  128.             if(panelByCoordinates.get(new Coordinates(6, 3).coordinates).color.equals(Color.RED)){  //Check if the red car is at the exit.
  129.                 break;
  130.             }
  131.             counter++;
  132.         }
  133.     }
  134.  
  135.     public void info() {
  136.         System.out.println("Number of cars: " + carsByColor.size());
  137.         for (Car car : carsByColor.values()) {
  138.             System.out.println("Car " + car.getPanels().length + car.panels[0].color.getRed() + " " + car.getPanels().length + car.panels[0].color.getGreen() + " " + car.getPanels().length + car.panels[0].color.getBlue() + " at position ");
  139.             for (Panel panel : car.getPanels()) {
  140.                 System.out.print(panel.x + ":" + panel.y + " ");
  141.             }
  142.             System.out.println(" can move " + car.movement);
  143.         }
  144.  
  145.     }
  146.  
  147.     public void createPanelList() {
  148.         panelList.clear();
  149.         int counter = 0;
  150.         for (Component component : mainframe.jPanel1.getComponents()) {
  151.             Panel panel = new Panel(component, component.getBackground(), Character.getNumericValue(component.getName().charAt(0)), Character.getNumericValue(component.getName().charAt(1)));
  152.             if (reset_set == false){
  153.                 reset_color[counter] = component.getBackground();
  154.                 counter++;
  155.             }
  156.             panelList.add(panel);
  157. //            System.out.println("x" + panel.x + ":" + "y" + panel.y + panel.color);
  158.         }
  159.         reset_set = true;
  160. //        Coordinates newcod = new Coordinates(1, 1);
  161. //        System.out.println("Panel at 5:5 = " + panelByCoordinates.get(new Coordinates(5, 5).coordinates));
  162.     }
  163.  
  164.     public void linkcoordinates() {
  165.         panelByCoordinates.clear();
  166.         for (Panel panel : panelList) {
  167.             panelByCoordinates.put(new Coordinates(panel.x, panel.y).coordinates, panel);
  168.         }
  169.     }
  170.  
  171.     public static class Panel {
  172.  
  173.         private Component name;
  174.         private Color color;
  175.         private int x, y;
  176.  
  177.         public Panel(Component name, Color color, int y, int x) {
  178.             this.name = name;
  179.             this.color = color;
  180.             this.x = x;
  181.             this.y = y;
  182.         }
  183.     }
  184.  
  185.     public static class Coordinates {
  186.  
  187.         private int coordinates;
  188.  
  189.         public Coordinates(Integer x, Integer y) {
  190.             String a = String.valueOf(x) + String.valueOf(y);
  191.             Integer b = Integer.parseInt(a);
  192.             this.coordinates = b;
  193.         }
  194.     }
  195.  
  196.     public static class Car {
  197.  
  198.         private Panel[] panels;
  199.         private String movement;
  200.  
  201.         public Car(String movement, Panel... panels) {
  202.             this.panels = panels;
  203.             this.movement = movement;
  204.         }
  205.  
  206.         public Panel[] getPanels() {
  207.             return this.panels;
  208.         }
  209.  
  210.         public void replacePanel(Integer index, Panel replacewith) {
  211.             this.panels[index] = replacewith;
  212.         }
  213.     }
  214.  
  215.     public static class Move {
  216.  
  217.         private Car car;
  218.         private String direction;
  219.  
  220.         public Move(Car car, String direction) {
  221.             this.car = car;
  222.             this.direction = direction;
  223.         }
  224.     }
  225.  
  226.     public void recognizeCars() {
  227.         carsByColor.clear();
  228.         for (Panel panel : panelList) {
  229.             if (!panel.color.equals(Color.white)) {
  230.                 if (carsByColor.get(panel.color) == null) {
  231.                     carsByColor.put(panel.color, new Car(null, panel));
  232.                 } else {
  233.                     if (carsByColor.get(panel.color).getPanels().length == 1) {
  234.                         Panel temp = carsByColor.get(panel.color).panels[0];
  235.                         carsByColor.remove(panel.color);
  236.                         Car newcar = new Car(null, temp, panel);
  237.                         carsByColor.put(panel.color, newcar);
  238.  
  239.                     } else {
  240.                         Panel temp = carsByColor.get(panel.color).panels[0];
  241.                         Panel temp2 = carsByColor.get(panel.color).panels[1];
  242.                         carsByColor.remove(panel.color);
  243.                         Car newcar = new Car(null, temp, temp2, panel);
  244.                         carsByColor.put(panel.color, newcar);
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.     }
  250.  
  251.     public void MoveWhichWay() {
  252.         String b;
  253.         for (Car car : carsByColor.values()) {
  254.             if (car.panels[0].x == car.panels[1].x) {
  255.                 car.movement = "vertical";
  256.  
  257.             } else {
  258.                 car.movement = "horizontal";
  259.             }
  260.         }
  261.     }
  262.  
  263.     public void CanMoveWhere() {
  264.         PossibleMoves.clear();
  265.         int posmoves = PossibleMoves.size();
  266.         for (Car car : carsByColor.values()) {
  267.             if (car.movement.equals("horizontal")) {
  268.  
  269.                 for (Panel panel : car.getPanels()) {
  270.                     if (panelByCoordinates.get(new Coordinates((panel.x + 1), panel.y).coordinates) != null && panelByCoordinates.get(new Coordinates(panel.x + 1, panel.y).coordinates).color.equals(Color.WHITE)) {
  271.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move to the right.");
  272.                         PossibleMoves.add(new Move(car, "right"));
  273.                     }
  274.                     if (panelByCoordinates.get(new Coordinates((panel.x - 1), panel.y).coordinates) != null && panelByCoordinates.get(new Coordinates(panel.x - 1, panel.y).coordinates).color.equals(Color.WHITE)) {
  275.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move to the left.");
  276.                         PossibleMoves.add(new Move(car, "left"));
  277.                     } else {
  278.                     }
  279.                 }
  280.  
  281.             }
  282.             if (car.movement.equals("vertical")) {
  283.                 for (Panel panel : car.getPanels()) {
  284.                     if (panelByCoordinates.get(new Coordinates(panel.x, (panel.y - 1)).coordinates) != null && panelByCoordinates.get(new Coordinates(panel.x, panel.y - 1).coordinates).color.equals(Color.WHITE)) {
  285.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move up.");
  286.                         PossibleMoves.add(new Move(car, "up"));
  287.                     }
  288.                     if (panelByCoordinates.get(new Coordinates(panel.x, (panel.y + 1)).coordinates) != null && panelByCoordinates.get(new Coordinates(panel.x, panel.y + 1).coordinates).color.equals(Color.WHITE)) {
  289.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move down.");
  290.                         PossibleMoves.add(new Move(car, "down"));
  291.                     } else {
  292.                     }
  293.                 }
  294.             }
  295.         }
  296.         System.out.println("Done");
  297.         System.out.println("Pos moves: ");
  298.         for (Move string : PossibleMoves) {
  299.             System.out.print(" " + string.direction);
  300.         }
  301.         System.out.println(".");
  302.     }
  303.  
  304.     public void MakeMove(Integer takeMove) {
  305.        
  306.         Car car = PossibleMoves.get(takeMove).car;
  307.         Color color = PossibleMoves.get(takeMove).car.panels[0].color;
  308.         String directions = PossibleMoves.get(takeMove).direction;
  309.         mainframe.jTextArea1.append("Car color " + color + " moves "+ directions+"\n");
  310.         for (Panel panel : car.panels) {
  311.             panel.name.setBackground(Color.WHITE);
  312.             panel.color = Color.WHITE;
  313.         }
  314.         switch (directions) {
  315.             case "left":
  316.                 for (int i = 0; i < car.panels.length; i++) {
  317.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates((car.panels[i].x - 1), car.panels[i].y).coordinates);
  318.                     car.replacePanel(i, replacementpanel);
  319.  
  320.                 }
  321.                 break;
  322.             case "right":
  323.                 for (int i = 0; i < car.panels.length; i++) {
  324.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates((car.panels[i].x + 1), car.panels[i].y).coordinates);
  325.                     car.replacePanel(i, replacementpanel);
  326.                 }
  327.                 break;
  328.             case "up":
  329.                 for (int i = 0; i < car.panels.length; i++) {
  330.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates(car.panels[i].x, (car.panels[i].y - 1)).coordinates);
  331.                     car.replacePanel(i, replacementpanel);
  332.  
  333.                 }
  334.                 break;
  335.             case "down":
  336.                 for (int i = 0; i < car.panels.length; i++) {
  337.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates(car.panels[i].x, (car.panels[i].y + 1)).coordinates);
  338.                     car.replacePanel(i, replacementpanel);
  339.  
  340.                 }
  341.                 break;
  342.             default:
  343.                 break;
  344.         }
  345.         System.out.println("Lenth: " + car.panels.length);
  346.         for (Panel panel : car.panels) {
  347.             panel.name.setBackground(color);
  348.             panel.color = color;
  349.             System.out.println("Paint: " + panel.x + ":" + panel.y);
  350.         }
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement