Last active
August 29, 2015 14:04
-
-
Save MrP123/0dd6c04bbf62a01d2e61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Challenge_173_Intermediate; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import javax.imageio.ImageIO; | |
public class AdvancedLangtonsAnt{ | |
public static int steps; | |
public static final Color[] colors = { Color.WHITE, Color.BLACK, Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.GRAY }; | |
public static char[] turns; | |
public static void main(String[] args){ | |
System.out.println("Enter 9 turning conditions and the amount of steps"); | |
Scanner s = new Scanner(System.in); | |
char[] c = s.nextLine().toCharArray(); | |
if(c.length < colors.length || c.length > colors.length){ | |
System.err.println("Too few/many turning condidtions for colors!\nUsing already defined turns!"); | |
turns = new char[] { 'L', 'R', 'L', 'R', 'L', 'R', 'L', 'R', 'L' }; | |
}else{ | |
turns = c; | |
} | |
steps = s.nextInt(); | |
s.close(); | |
Ant ant = new Ant(500, 500, 0); | |
for(int i = 0; i < steps; i++){ | |
//this is here so my ant wraps around the grid (stil doesn't work correctly) | |
if(ant.x >= Ant.grid.length - 1) ant.x = 100; | |
if(ant.x <= 1) ant.x = Ant.grid.length - 100; | |
if(ant.y >= Ant.grid[0].length - 1) ant.x = 100; | |
if(ant.y <= 1) ant.y = Ant.grid[0].length - 100; | |
try{ | |
ant.step(); | |
}catch(ArrayIndexOutOfBoundsException e){ | |
saveImage(); | |
System.err.println("ArrayIndexOutOfBoundsException caught at step " + (i + 1) + ". Exiting now!"); | |
System.exit(1); | |
} | |
} | |
saveImage(); | |
System.out.println("Done"); | |
} | |
private static void saveImage(){ | |
BufferedImage img = new BufferedImage(Ant.grid.length, Ant.grid[0].length, BufferedImage.TYPE_INT_RGB); | |
for(int x = 0; x < Ant.grid.length; x++){ | |
for(int y = 0; y < Ant.grid[0].length; y++){ | |
img.setRGB(x, y, Ant.grid[x][y].getRGB()); | |
} | |
} | |
File path = new File(System.getProperty("user.home") + "/Desktop/AdvancedLangtonnsAnt.png"); | |
try{ | |
ImageIO.write(img, "png", path); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Challenge_173_Intermediate; | |
import java.awt.Color; | |
public class Ant{ | |
public int x, y; | |
public int direction = 0; //up=0, right=1, down=2, left=3 | |
public static Color[][] grid = new Color[1000][1000]; | |
public Ant(int x, int y, int direction){ | |
this.x = x; | |
this.y = y; | |
this.direction = direction; | |
initGrid(); | |
} | |
private void initGrid(){ | |
for(int x = 0; x < grid.length; x++){ | |
for(int y = 0; y < grid[0].length; y++){ | |
grid[x][y] = Color.WHITE; | |
} | |
} | |
} | |
public void move(){ | |
if(direction == 0){ | |
move(0, 1); | |
}else if(direction == 1){ | |
move(1, 0); | |
}else if(direction == 2){ | |
move(0, -1); | |
}else if(direction == 3){ | |
move(-1, 0); | |
} | |
} | |
public void move(int x, int y){ | |
this.x += x; | |
this.y += y; | |
} | |
public void step(){ | |
Color current = grid[this.x][this.y]; | |
for(int i = 0; i < AdvancedLangtonsAnt.colors.length; i++){ | |
if(AdvancedLangtonsAnt.colors[i].equals(current)){ | |
turn(AdvancedLangtonsAnt.turns[i]); | |
break; | |
} | |
} | |
changeColor(); | |
move(); | |
} | |
public void changeColor(){ | |
Color current = grid[this.x][this.y]; | |
for(int i = 0; i < AdvancedLangtonsAnt.colors.length; i++){ | |
if(AdvancedLangtonsAnt.colors[i].equals(current)){ | |
if(i < AdvancedLangtonsAnt.colors.length - 1) grid[this.x][this.y] = AdvancedLangtonsAnt.colors[i + 1]; | |
else grid[this.x][this.y] = AdvancedLangtonsAnt.colors[0]; | |
break; | |
} | |
} | |
} | |
public void turn(char turn){ | |
if(turn == 'L') turnLeft(); | |
if(turn == 'R') turnRight(); | |
} | |
public void turnLeft(){ | |
this.direction--; | |
if(this.direction < 0) this.direction = 3; | |
} | |
public void turnRight(){ | |
this.direction++; | |
if(this.direction > 3) this.direction = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment