Advertisement
Guest User

YuriKahn Lanton's Ant

a guest
Aug 1st, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. package ant;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6.  
  7. import javax.imageio.ImageIO;
  8.  
  9. public class Ant {
  10.  
  11.     private class Cell {
  12.        
  13.         private int colorValue;
  14.        
  15.         public Cell(int colorValue) {
  16.             this.colorValue = colorValue;
  17.         }
  18.        
  19.         public int getColorValue() {
  20.             return colorValue;
  21.         }
  22.        
  23.         public void setColorValue(int colorValue) {
  24.             this.colorValue = colorValue;
  25.         }
  26.     }
  27.    
  28.     private class ColorEntry {
  29.        
  30.        
  31.         private int R;
  32.         private int G;
  33.         private int B;
  34.         private int value;
  35.         private int rotate;
  36.        
  37.         public ColorEntry(int r, int g, int b, int value, int rotate) {
  38.             super();
  39.             R = r;
  40.             G = g;
  41.             B = b;
  42.             this.value = value;
  43.             this.rotate = rotate;
  44.         }
  45.        
  46.         public int getR() {
  47.             return R;
  48.         }
  49.        
  50.         public void setR(int r) {
  51.             R = r;
  52.         }
  53.        
  54.         public int getG() {
  55.             return G;
  56.         }
  57.        
  58.         public void setG(int g) {
  59.             G = g;
  60.         }
  61.        
  62.         public int getB() {
  63.             return B;
  64.         }
  65.        
  66.         public void setB(int b) {
  67.             B = b;
  68.         }
  69.        
  70.         public int getValue() {
  71.             return value;
  72.         }
  73.        
  74.         public void setValue(int value) {
  75.             this.value = value;
  76.         }
  77.        
  78.         public int getRotate() {
  79.             return rotate;
  80.         }
  81.        
  82.         public void setRotate(int rotate) {
  83.             this.rotate = rotate;
  84.         }
  85.        
  86.     }
  87.    
  88.     private final static int DIRECTION_NORTH = 90;
  89.     private final static int DIRECTION_SOUTH = 270;
  90.     private final static int DIRECTION_EAST = 0;
  91.     private final static int DIRECTION_WEST = 180;
  92.     private final static int ROTATE_COUNTER = 0;
  93.     private final static int ROTATE_CLOCKWISE = 1;
  94.     private final static int X_SIZE = 500;
  95.     private final static int Y_SIZE = 500;
  96.    
  97.     private ColorEntry[] colorArray;
  98.     private Cell[][] cellArray;
  99.     private int numberOfTurns;
  100.    
  101.     private int antX;
  102.     private int antY;
  103.     private int antDirection;
  104.    
  105.     public Ant(String colorString, int numberOfTurns) {
  106.         this.numberOfTurns = numberOfTurns;
  107.        
  108.         /* init colors */
  109.         colorArray = new ColorEntry[colorString.length()];
  110.         for(int i=0; i<colorArray.length; i++) {
  111.             int hueValue = (i-1) * (511/(colorArray.length-1));
  112.             int thisColorDirection = (colorString.charAt(i) == 'L') ? ROTATE_COUNTER : ROTATE_CLOCKWISE;
  113.             ColorEntry thisColor = new ColorEntry(0, 0, 0, i, thisColorDirection);
  114.            
  115.             /* make white always equal color 0 (starting condition) */
  116.             if(i == 0) {
  117.                 colorArray[i] = thisColor;
  118.                 continue;
  119.             }
  120.            
  121.             /* color selection mechanism 1 */
  122.             /*
  123.             if(hueValue<256) {
  124.                 thisColor.setR(hueValue);
  125.                 thisColor.setB(255-hueValue);
  126.             }else{
  127.                 hueValue -= 256;
  128.                 thisColor.setB(hueValue);
  129.                 thisColor.setG(255-hueValue);
  130.             }*/
  131.            
  132.             /* color selection mechanism 2 */
  133.             thisColor.setR(hueValue/2);
  134.            
  135.             colorArray[i] = thisColor;
  136.         }
  137.        
  138.         /* init array */
  139.        
  140.         cellArray = new Cell[X_SIZE][Y_SIZE];
  141.         for(int x=0; x<X_SIZE; x++) {
  142.             for(int y=0; y<Y_SIZE; y++) {
  143.                 cellArray[x][y] = new Cell(0);
  144.             }
  145.         }
  146.        
  147.         /* init ant */
  148.         antX = X_SIZE/2;
  149.         antY = Y_SIZE/2;
  150.         antDirection = DIRECTION_NORTH;
  151.     }
  152.    
  153.     public void run() {
  154.         /* Main loop: run ant */
  155.         for(int i=0; i<numberOfTurns; i++) {
  156.             /* Rotate ant */
  157.             int value = cellArray[antX][antY].getColorValue();
  158.             int rotate = colorArray[value].getRotate();
  159.             if(colorArray[cellArray[antX][antY].getColorValue()].getRotate() == ROTATE_COUNTER) {
  160.                 antDirection = (antDirection + 90) % 360;
  161.             }else{
  162.                 antDirection -= 90;
  163.                 if(antDirection < 0) {
  164.                     antDirection += 360;
  165.                 }
  166.             }
  167.            
  168.             /* Increase color */
  169.             cellArray[antX][antY].setColorValue((cellArray[antX][antY].getColorValue() + 1) % colorArray.length);
  170.            
  171.             /* move ant */
  172.             switch(antDirection) {
  173.                 case DIRECTION_NORTH:
  174.                     antY--;
  175.                     break;
  176.                 case DIRECTION_SOUTH:
  177.                     antY++;
  178.                     break;
  179.                 case DIRECTION_EAST:
  180.                     antX--;
  181.                     break;
  182.                 case DIRECTION_WEST:
  183.                     antX++;
  184.                     break;
  185.             }
  186.            
  187.             if(antX < 0) {
  188.                 antX = X_SIZE -1;
  189.             }
  190.             if(antX >= X_SIZE) {
  191.                 antX = 0;
  192.             }
  193.             if(antY < 0) {
  194.                 antY = Y_SIZE -1;
  195.             }
  196.             if(antY >= Y_SIZE) {
  197.                 antY = 0;
  198.             }
  199.         }
  200.        
  201.         /* print to .bmp image file */
  202.         writeImage();
  203.     }
  204.    
  205.     private int hexifyColor(int colorID) {
  206.         int R = colorArray[colorID].getR();
  207.         int G = colorArray[colorID].getG();
  208.         int B = colorArray[colorID].getB();
  209.         int hex = 0;
  210.         hex += R << 16;
  211.         hex += G << 8;
  212.         hex += B;
  213.         return hex;
  214.     }
  215.    
  216.     private void writeImage() {
  217.         File outputFile = new File("antOutput.png");
  218.         BufferedImage theImage = new BufferedImage(X_SIZE, Y_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
  219.        
  220.         for(int x=0; x<X_SIZE; x++) {
  221.             for(int y=0; y<Y_SIZE; y++) {
  222.                 theImage.setRGB(x, y, hexifyColor(cellArray[x][y].getColorValue()));
  223.             }
  224.         }
  225.        
  226.         try{
  227.             ImageIO.write(theImage, "PNG", outputFile);
  228.         }catch(IOException e) {
  229.             System.out.println("Error: Could not write file.");
  230.             System.exit(1);
  231.         }
  232.     }
  233.    
  234.     public static void main(String[] args) {
  235.         if(args.length != 2) {
  236.             System.out.println("Error: Invalid number of arguments.");
  237.             System.exit(1);
  238.         }
  239.        
  240.        
  241.         int numberOfTurns = 0;
  242.         try{
  243.             numberOfTurns = Integer.parseInt(args[1]);
  244.         }catch(Exception e) {
  245.             System.out.println("Error: Invalid entry for number of turns.");
  246.             System.exit(1);
  247.         }
  248.        
  249.         Ant instance = new Ant(args[0], numberOfTurns);
  250.         instance.run();
  251.        
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement