Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Step 1: Check current index's color
- * Step 2: Rotate ant either CW or CCW based on index's color
- * Step 3: Change current index's color to next color in list
- * Step 4: Move ant forward based on rotation state. (If facing north, move up an index (curIndex + array width), east (curIndex++))
- */
- void main(){
- var colors = ['!', '@', '#', '%', '&'];
- var colorDir = [-1, 1, -1, 1, -1]; // 1 = RIGHT, -1 = LEFT
- var width = 75;
- var height = 75;
- var grid = new List<String>(width * height);
- grid.fillRange(0, grid.length, colors[0]);
- var antStart = (grid.length / 2).ceil(); // ~~CENTER OF LIST
- var currentPos = antStart;
- var currentDir = 0; // 0 = NORTH, 1 = EAST, 2 = SOUTH, -1 = WEST, < -1, SET 2 > 3, SET -1
- var currentColor = colors.indexOf('!'); // CURRENTCOLOR DEFAULT = 0 [!], CANNOT EXCEED COLORS.LENGTH
- var ticks = 30000;
- for(int i = 0; i < ticks; i++){
- // STEP 1
- currentColor = colors.indexOf(grid[currentPos]);
- // STEP 2
- currentDir = currentDir + colorDir[currentColor];
- if(currentDir < -1){
- currentDir = 2;
- }
- if(currentDir > 2){
- currentDir = -1;
- }
- // STEP 3
- currentColor++;
- if(currentColor > colors.length - 1){
- currentColor = 0;
- }
- grid[currentPos] = colors[currentColor];
- // STEP 4
- switch(currentDir){
- case 0: // NORTH
- currentPos -= width;
- break;
- case 1: // EAST
- currentPos++;
- break;
- case 2: // SOUTH
- currentPos += width;
- break;
- case -1: // WEST
- currentPos--;
- break;
- }
- }
- toString(grid, width);
- }
- void toString(List<String> grid, int width){
- String formattedGrid = '';
- for(int i = 0; i < grid.length; i++){
- formattedGrid += grid[i] + ' ';
- if((i % width) == (width - 1)){
- formattedGrid += '\n';
- }
- }
- print(formattedGrid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement