Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ant;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class Ant {
- private class Cell {
- private int colorValue;
- public Cell(int colorValue) {
- this.colorValue = colorValue;
- }
- public int getColorValue() {
- return colorValue;
- }
- public void setColorValue(int colorValue) {
- this.colorValue = colorValue;
- }
- }
- private class ColorEntry {
- private int R;
- private int G;
- private int B;
- private int value;
- private int rotate;
- public ColorEntry(int r, int g, int b, int value, int rotate) {
- super();
- R = r;
- G = g;
- B = b;
- this.value = value;
- this.rotate = rotate;
- }
- public int getR() {
- return R;
- }
- public void setR(int r) {
- R = r;
- }
- public int getG() {
- return G;
- }
- public void setG(int g) {
- G = g;
- }
- public int getB() {
- return B;
- }
- public void setB(int b) {
- B = b;
- }
- public int getValue() {
- return value;
- }
- public void setValue(int value) {
- this.value = value;
- }
- public int getRotate() {
- return rotate;
- }
- public void setRotate(int rotate) {
- this.rotate = rotate;
- }
- }
- private final static int DIRECTION_NORTH = 90;
- private final static int DIRECTION_SOUTH = 270;
- private final static int DIRECTION_EAST = 0;
- private final static int DIRECTION_WEST = 180;
- private final static int ROTATE_COUNTER = 0;
- private final static int ROTATE_CLOCKWISE = 1;
- private final static int X_SIZE = 500;
- private final static int Y_SIZE = 500;
- private ColorEntry[] colorArray;
- private Cell[][] cellArray;
- private int numberOfTurns;
- private int antX;
- private int antY;
- private int antDirection;
- public Ant(String colorString, int numberOfTurns) {
- this.numberOfTurns = numberOfTurns;
- /* init colors */
- colorArray = new ColorEntry[colorString.length()];
- for(int i=0; i<colorArray.length; i++) {
- int hueValue = (i-1) * (511/(colorArray.length-1));
- int thisColorDirection = (colorString.charAt(i) == 'L') ? ROTATE_COUNTER : ROTATE_CLOCKWISE;
- ColorEntry thisColor = new ColorEntry(0, 0, 0, i, thisColorDirection);
- /* make white always equal color 0 (starting condition) */
- if(i == 0) {
- colorArray[i] = thisColor;
- continue;
- }
- /* color selection mechanism 1 */
- /*
- if(hueValue<256) {
- thisColor.setR(hueValue);
- thisColor.setB(255-hueValue);
- }else{
- hueValue -= 256;
- thisColor.setB(hueValue);
- thisColor.setG(255-hueValue);
- }*/
- /* color selection mechanism 2 */
- thisColor.setR(hueValue/2);
- colorArray[i] = thisColor;
- }
- /* init array */
- cellArray = new Cell[X_SIZE][Y_SIZE];
- for(int x=0; x<X_SIZE; x++) {
- for(int y=0; y<Y_SIZE; y++) {
- cellArray[x][y] = new Cell(0);
- }
- }
- /* init ant */
- antX = X_SIZE/2;
- antY = Y_SIZE/2;
- antDirection = DIRECTION_NORTH;
- }
- public void run() {
- /* Main loop: run ant */
- for(int i=0; i<numberOfTurns; i++) {
- /* Rotate ant */
- int value = cellArray[antX][antY].getColorValue();
- int rotate = colorArray[value].getRotate();
- if(colorArray[cellArray[antX][antY].getColorValue()].getRotate() == ROTATE_COUNTER) {
- antDirection = (antDirection + 90) % 360;
- }else{
- antDirection -= 90;
- if(antDirection < 0) {
- antDirection += 360;
- }
- }
- /* Increase color */
- cellArray[antX][antY].setColorValue((cellArray[antX][antY].getColorValue() + 1) % colorArray.length);
- /* move ant */
- switch(antDirection) {
- case DIRECTION_NORTH:
- antY--;
- break;
- case DIRECTION_SOUTH:
- antY++;
- break;
- case DIRECTION_EAST:
- antX--;
- break;
- case DIRECTION_WEST:
- antX++;
- break;
- }
- if(antX < 0) {
- antX = X_SIZE -1;
- }
- if(antX >= X_SIZE) {
- antX = 0;
- }
- if(antY < 0) {
- antY = Y_SIZE -1;
- }
- if(antY >= Y_SIZE) {
- antY = 0;
- }
- }
- /* print to .bmp image file */
- writeImage();
- }
- private int hexifyColor(int colorID) {
- int R = colorArray[colorID].getR();
- int G = colorArray[colorID].getG();
- int B = colorArray[colorID].getB();
- int hex = 0;
- hex += R << 16;
- hex += G << 8;
- hex += B;
- return hex;
- }
- private void writeImage() {
- File outputFile = new File("antOutput.png");
- BufferedImage theImage = new BufferedImage(X_SIZE, Y_SIZE, BufferedImage.TYPE_4BYTE_ABGR);
- for(int x=0; x<X_SIZE; x++) {
- for(int y=0; y<Y_SIZE; y++) {
- theImage.setRGB(x, y, hexifyColor(cellArray[x][y].getColorValue()));
- }
- }
- try{
- ImageIO.write(theImage, "PNG", outputFile);
- }catch(IOException e) {
- System.out.println("Error: Could not write file.");
- System.exit(1);
- }
- }
- public static void main(String[] args) {
- if(args.length != 2) {
- System.out.println("Error: Invalid number of arguments.");
- System.exit(1);
- }
- int numberOfTurns = 0;
- try{
- numberOfTurns = Integer.parseInt(args[1]);
- }catch(Exception e) {
- System.out.println("Error: Invalid entry for number of turns.");
- System.exit(1);
- }
- Ant instance = new Ant(args[0], numberOfTurns);
- instance.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement