Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.72 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 ArrayList<Move> PossibleMoves = new ArrayList<>();
  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 ArrayList<ArrayList<Move>> moves = new ArrayList<ArrayList<Move>>();
  27.     public static ArrayList<Move> new_array = new ArrayList<>();
  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.         createPanelList();
  93.         linkcoordinates();
  94.         recognizeCars();    
  95.         MoveWhichWay();
  96.         CanMoveWhere(); //Initial search to get the first level of moves this writes to PossibleMoves
  97.         for (Move move : PossibleMoves){
  98.             ArrayList<Move> newlist = new ArrayList<>();
  99.             newlist.add(move);  
  100.             moves.add(newlist); //Save those moves an an integer
  101.         }  
  102.         int counter = 0;    //This counter is used to get through the moves array
  103.         while (true){ //Keep going untill the red car is out, then a break is used
  104.             resetWindow();  //Make sure every car is in the begin position
  105.             for(Move c : moves.get(counter)){   //Redo the old steps first
  106.                 createPanelList();
  107.                 linkcoordinates();
  108.                 recognizeCars();
  109.                 MoveWhichWay();
  110.                 MakeMove(c);
  111.             }
  112.            
  113.             createPanelList();
  114.             linkcoordinates();
  115.             recognizeCars(); //Group panels to define what a car is
  116.             MoveWhichWay(); //See if each car can move left/right or up/down
  117.             CanMoveWhere(); //Create new steps into PossibleMoves()
  118.            
  119.             for (Move next_move: PossibleMoves){
  120.                 new_array = (ArrayList<Move>) moves.get(counter).clone();
  121.                 new_array.add(next_move);    //Add the new step and put it into the moves array
  122.                 moves.add(new_array);
  123.                
  124.             }
  125.             if(panelByCoordinates.get(new Coordinates(6, 3).coordinates).color.equals(Color.RED)){  //Check if the red car is at the exit.
  126.                 break;
  127.             }
  128.             counter++;
  129.         }
  130.     }
  131.  
  132.     public void info() {
  133.         System.out.println("Number of cars: " + carsByColor.size());
  134.         for (Car car : carsByColor.values()) {
  135.             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 ");
  136.             for (Panel panel : car.getPanels()) {
  137.                 System.out.print(panel.x + ":" + panel.y + " ");
  138.             }
  139.             System.out.println(" can move " + car.movement);
  140.         }
  141.  
  142.     }
  143.  
  144.     public void createPanelList() {
  145.         panelList.clear();
  146.         int counter = 0;
  147.         for (Component component : mainframe.jPanel1.getComponents()) {
  148.             Panel panel = new Panel(component, component.getBackground(), Character.getNumericValue(component.getName().charAt(0)), Character.getNumericValue(component.getName().charAt(1)));
  149.             if (reset_set == false){
  150.                 reset_color[counter] = component.getBackground();
  151.                 counter++;
  152.             }
  153.             panelList.add(panel);
  154. //            System.out.println("x" + panel.x + ":" + "y" + panel.y + panel.color);
  155.         }
  156.         reset_set = true;
  157. //        Coordinates newcod = new Coordinates(1, 1);
  158. //        System.out.println("Panel at 5:5 = " + panelByCoordinates.get(new Coordinates(5, 5).coordinates));
  159.     }
  160.  
  161.     public void linkcoordinates() {
  162.         panelByCoordinates.clear();
  163.         for (Panel panel : panelList) {
  164.             panelByCoordinates.put(new Coordinates(panel.x, panel.y).coordinates, panel);
  165.         }
  166.     }
  167.  
  168.     public static class Panel {
  169.  
  170.         private Component name;
  171.         private Color color;
  172.         private int x, y;
  173.  
  174.         public Panel(Component name, Color color, int y, int x) {
  175.             this.name = name;
  176.             this.color = color;
  177.             this.x = x;
  178.             this.y = y;
  179.         }
  180.     }
  181.  
  182.     public static class Coordinates {
  183.  
  184.         private int coordinates;
  185.  
  186.         public Coordinates(Integer x, Integer y) {
  187.             String a = String.valueOf(x) + String.valueOf(y);
  188.             Integer b = Integer.parseInt(a);
  189.             this.coordinates = b;
  190.         }
  191.     }
  192.  
  193.     public static class Car {
  194.  
  195.         private Panel[] panels;
  196.         private String movement;
  197.  
  198.         public Car(String movement, Panel... panels) {
  199.             this.panels = panels;
  200.             this.movement = movement;
  201.         }
  202.  
  203.         public Panel[] getPanels() {
  204.             return this.panels;
  205.         }
  206.  
  207.         public void replacePanel(Integer index, Panel replacewith) {
  208.             this.panels[index] = replacewith;
  209.         }
  210.     }
  211.  
  212.     public static class Move {
  213.  
  214.         private Car car;
  215.         private String direction;
  216.  
  217.         public Move(Car car, String direction) {
  218.             this.car = car;
  219.             this.direction = direction;
  220.         }
  221.     }
  222.  
  223.     public void recognizeCars() {
  224.         carsByColor.clear();
  225.         for (Panel panel : panelList) {
  226.             if (!panel.color.equals(Color.white)) {
  227.                 if (carsByColor.get(panel.color) == null) {
  228.                     carsByColor.put(panel.color, new Car(null, panel));
  229.                 } else {
  230.                     if (carsByColor.get(panel.color).getPanels().length == 1) {
  231.                         Panel temp = carsByColor.get(panel.color).panels[0];
  232.                         carsByColor.remove(panel.color);
  233.                         Car newcar = new Car(null, temp, panel);
  234.                         carsByColor.put(panel.color, newcar);
  235.  
  236.                     } else {
  237.                         Panel temp = carsByColor.get(panel.color).panels[0];
  238.                         Panel temp2 = carsByColor.get(panel.color).panels[1];
  239.                         carsByColor.remove(panel.color);
  240.                         Car newcar = new Car(null, temp, temp2, panel);
  241.                         carsByColor.put(panel.color, newcar);
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.     }
  247.  
  248.     public void MoveWhichWay() {
  249.         String b;
  250.         for (Car car : carsByColor.values()) {
  251.             if (car.panels[0].x == car.panels[1].x) {
  252.                 car.movement = "vertical";
  253.  
  254.             } else {
  255.                 car.movement = "horizontal";
  256.             }
  257.         }
  258.     }
  259.  
  260.     public void CanMoveWhere() {
  261.         PossibleMoves.clear();
  262.         int posmoves = PossibleMoves.size();
  263.         for (Car car : carsByColor.values()) {
  264.             if (car.movement.equals("horizontal")) {
  265.  
  266.                 for (Panel panel : car.getPanels()) {
  267.                     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)) {
  268.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move to the right.");
  269.                         PossibleMoves.add(new Move(car, "right"));
  270.                     }
  271.                     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)) {
  272.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move to the left.");
  273.                         PossibleMoves.add(new Move(car, "left"));
  274.                     } else {
  275.                     }
  276.                 }
  277.  
  278.             }
  279.             if (car.movement.equals("vertical")) {
  280.                 for (Panel panel : car.getPanels()) {
  281.                     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)) {
  282.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move up.");
  283.                         PossibleMoves.add(new Move(car, "up"));
  284.                     }
  285.                     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)) {
  286.                         System.out.println("Car at " + panel.x + ":" + panel.y + " can move down.");
  287.                         PossibleMoves.add(new Move(car, "down"));
  288.                     } else {
  289.                     }
  290.                 }
  291.             }
  292.         }
  293.         System.out.println("Done");
  294.         System.out.println("Pos moves: ");
  295.         for (Move string : PossibleMoves) {
  296.             System.out.print(" " + string.direction);
  297.         }
  298.         System.out.println(".");
  299.     }
  300.  
  301.     public void MakeMove(Move takeMove) {
  302.        
  303.         Car car = takeMove.car;
  304.         Color color = takeMove.car.panels[0].color;
  305.         String directions = takeMove.direction;
  306.         mainframe.jTextArea1.append("Car color " + color + " moves "+ directions+"\n");
  307.         for (Panel panel : car.panels) {
  308.             panel.name.setBackground(Color.WHITE);
  309.             panel.color = Color.WHITE;
  310.         }
  311.         switch (directions) {
  312.             case "left":
  313.                 for (int i = 0; i < car.panels.length; i++) {
  314.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates((car.panels[i].x - 1), car.panels[i].y).coordinates);
  315.                     car.replacePanel(i, replacementpanel);
  316.  
  317.                 }
  318.                 break;
  319.             case "right":
  320.                 for (int i = 0; i < car.panels.length; i++) {
  321.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates((car.panels[i].x + 1), car.panels[i].y).coordinates);
  322.                     car.replacePanel(i, replacementpanel);
  323.                 }
  324.                 break;
  325.             case "up":
  326.                 for (int i = 0; i < car.panels.length; i++) {
  327.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates(car.panels[i].x, (car.panels[i].y - 1)).coordinates);
  328.                     car.replacePanel(i, replacementpanel);
  329.  
  330.                 }
  331.                 break;
  332.             case "down":
  333.                 for (int i = 0; i < car.panels.length; i++) {
  334.                     Panel replacementpanel = panelByCoordinates.get(new Coordinates(car.panels[i].x, (car.panels[i].y + 1)).coordinates);
  335.                     car.replacePanel(i, replacementpanel);
  336.  
  337.                 }
  338.                 break;
  339.             default:
  340.                 break;
  341.         }
  342.         System.out.println("Lenth: " + car.panels.length);
  343.         for (Panel panel : car.panels) {
  344.             panel.name.setBackground(color);
  345.             panel.color = color;
  346.             System.out.println("Paint: " + panel.x + ":" + panel.y);
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement