Guest User

Untitled

a guest
Feb 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. int[][] grid;
  2. int row;
  3. int col;
  4. int dir = 0;
  5. final int DIR_UP = 0;
  6. final int DIR_RIGHT = 1;
  7. final int DIR_DOWN = 2;
  8. final int DIR_LEFT = 3;
  9.  
  10. void setup()
  11. {
  12. size(500, 500);
  13. background(255);
  14.  
  15. grid = new int[width][height];
  16. col = width / 2;
  17. row = height / 2;
  18. }
  19.  
  20. void draw()
  21. {
  22. go();
  23. }
  24.  
  25. void go()
  26. {
  27. int pix = col + row * width;
  28. int state = grid[row][col];
  29.  
  30. loadPixels();
  31.  
  32. if(state == 0)
  33. {
  34. turn(1);
  35. grid[row][col] = 1;
  36.  
  37. pixels[pix] = color(255);
  38. }
  39. else
  40. {
  41. turn(-1);
  42. grid[row][col] = 0;
  43.  
  44. pixels[pix] = color(0);
  45. }
  46.  
  47. updatePixels();
  48.  
  49. move();
  50. }
  51.  
  52. void turn(int rotation)
  53. {
  54. dir += rotation;
  55.  
  56. if(dir < 0)
  57. {
  58. dir = 3;
  59. }
  60. else if(dir > 3)
  61. {
  62. dir = 0;
  63. }
  64.  
  65. // Does not work, can return negative values
  66. //dir = (dir + rotation) % 4;
  67. }
  68.  
  69. void move()
  70. {
  71. switch(dir)
  72. {
  73. case DIR_UP:
  74. row--;
  75. case DIR_RIGHT:
  76. col++;
  77. case DIR_LEFT:
  78. col--;
  79. case DIR_DOWN:
  80. row++;
  81. }
  82.  
  83. if(col < 0)
  84. col = width - 1;
  85. else if(col >= width)
  86. col = 0;
  87.  
  88. if(row < 0)
  89. row = height - 1;
  90. else if(row >= height)
  91. row = 0;
  92. }
Add Comment
Please, Sign In to add comment