Advertisement
Guest User

Untitled

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