Skip to content

Instantly share code, notes, and snippets.

@theBonesae
Created July 30, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theBonesae/81898e9f855801bf0618 to your computer and use it in GitHub Desktop.
Save theBonesae/81898e9f855801bf0618 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
int size = 256;
bool exit = false;
Color[] colors = { Color.Black, Color.White, Color.Green, Color.Blue, Color.Brown, Color.Fuchsia, Color.Purple, Color.DeepPink, Color.Red};
List<List<Color>> map = new List<List<Color>>();
for (int i = 0; i < size; i++) {
List<Color> row = new List<Color>();
for (int j = 0; j < size; j++) {
row.Add(Color.White);
}
map.Add(row);
}
Ant ant = new Ant(size / 2, size / 2, Ant.Direction.UP);
Console.WriteLine("Please enter Directions:");
string input = Console.ReadLine().ToUpper();
char[] newDirections = input.ToCharArray();
List<AntColor> antColors = new List<AntColor>();
for (int i = 0; i < newDirections.Length; i++) {
antColors.Add(new AntColor(colors[i], newDirections[i]));
}
Console.WriteLine("How many iterations?");
int iterations = int.Parse(Console.ReadLine());
for (int i = 0; i < iterations; i++) {
ant.Step(map, antColors);
}
Bitmap bitMap = new Bitmap(size, size);
Graphics g = Graphics.FromImage(bitMap);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
bitMap.SetPixel(i, j, map[i][j]);
}
}
g.Dispose();
bitMap.Save("ant"+ iterations.ToString() + ".png");
bitMap.Dispose();
}
}
class AntColor {
public Color color;
public char turnDir;
public AntColor(Color newCol, char newChar) {
color = newCol;
turnDir = newChar;
}
}
class Ant {
public enum Direction { UP, DOWN, LEFT, RIGHT }
private Direction direction;
private int xPos;
private int yPos;
public Ant(int xpos, int ypos, Direction initDirection) {
yPos = ypos;
xPos = xpos;
direction = initDirection;
}
void Turn(char toTurn) {
switch (direction) {
case Direction.UP:
if (toTurn.Equals('L')) {
direction = Direction.LEFT;
}
else {
direction = Direction.RIGHT;
}
break;
case Direction.DOWN:
if (toTurn.Equals('L')) {
direction = Direction.RIGHT;
}
else {
direction = Direction.LEFT;
}
break;
case Direction.LEFT:
if (toTurn.Equals('L')) {
direction = Direction.DOWN;
}
else {
direction = Direction.UP;
}
break;
case Direction.RIGHT:
if (toTurn.Equals('L')) {
direction = Direction.UP;
}
else {
direction = Direction.DOWN;
}
break;
}
}
public void Step(List<List<Color>> map, List<AntColor> colorDirections) {
Color currentColor = map[xPos][yPos];
int index = -1;
for (int i = 0; i < colorDirections.Count; i++) {
if (currentColor.Name.Equals(colorDirections[i].color.Name)) {
index = i;
}
}
Turn(colorDirections[index].turnDir);
index++;
if (index >= colorDirections.Count) {
index = 0;
}
map[xPos][yPos] = colorDirections[index].color;
StepForward();
}
void StepForward() {
switch (direction) {
case Direction.UP:
yPos += 1;
break;
case Direction.DOWN:
yPos -= 1;
break;
case Direction.LEFT:
xPos += 1;
break;
case Direction.RIGHT:
xPos -= 1;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment