Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. /*
  2. * Step 1: Check current index's color
  3. * Step 2: Rotate ant either CW or CCW based on index's color
  4. * Step 3: Change current index's color to next color in list
  5. * Step 4: Move ant forward based on rotation state. (If facing north, move up an index (curIndex + array width), east (curIndex++))
  6. */
  7.  
  8. void main(){
  9. var colors = ['!', '@', '#', '%', '&'];
  10. var colorDir = [-1, 1, -1, 1, -1]; // 1 = RIGHT, -1 = LEFT
  11.  
  12. var width = 75;
  13. var height = 75;
  14.  
  15. var grid = new List<String>(width * height);
  16. grid.fillRange(0, grid.length, colors[0]);
  17. var antStart = (grid.length / 2).ceil(); // ~~CENTER OF LIST
  18.  
  19. var currentPos = antStart;
  20. var currentDir = 0; // 0 = NORTH, 1 = EAST, 2 = SOUTH, -1 = WEST, < -1, SET 2 > 3, SET -1
  21. var currentColor = colors.indexOf('!'); // CURRENTCOLOR DEFAULT = 0 [!], CANNOT EXCEED COLORS.LENGTH
  22.  
  23. var ticks = 30000;
  24.  
  25. for(int i = 0; i < ticks; i++){
  26.  
  27. // STEP 1
  28.  
  29. currentColor = colors.indexOf(grid[currentPos]);
  30.  
  31. // STEP 2
  32.  
  33. currentDir = currentDir + colorDir[currentColor];
  34.  
  35. if(currentDir < -1){
  36. currentDir = 2;
  37. }
  38.  
  39. if(currentDir > 2){
  40. currentDir = -1;
  41. }
  42.  
  43. // STEP 3
  44.  
  45. currentColor++;
  46.  
  47. if(currentColor > colors.length - 1){
  48. currentColor = 0;
  49. }
  50.  
  51. grid[currentPos] = colors[currentColor];
  52.  
  53. // STEP 4
  54.  
  55. switch(currentDir){
  56. case 0: // NORTH
  57. currentPos -= width;
  58. break;
  59. case 1: // EAST
  60. currentPos++;
  61. break;
  62. case 2: // SOUTH
  63. currentPos += width;
  64. break;
  65. case -1: // WEST
  66. currentPos--;
  67. break;
  68. }
  69.  
  70. }
  71. toString(grid, width);
  72. }
  73.  
  74. void toString(List<String> grid, int width){
  75. String formattedGrid = '';
  76.  
  77. for(int i = 0; i < grid.length; i++){
  78. formattedGrid += grid[i] + ' ';
  79. if((i % width) == (width - 1)){
  80. formattedGrid += '\n';
  81. }
  82. }
  83. print(formattedGrid);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement