Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. void Living::moveTilBlocked(int x, int y, int spacesToMove) {
  2. // use moveTo a certain x y that you find is ok to move to (blocked -1)
  3. int pebbleAt = 0;
  4.  
  5. switch(getDirection()) {
  6. case right:
  7. for(int i = getX(); i <= getX() + spacesToMove; i++) {
  8. if(getWorld()->pebbleThere(i,y)) {
  9. pebbleAt = i;
  10. moveTo(i,y);
  11. break;
  12. }
  13. }
  14. moveTo(x+spacesToMove,y); // if no pebble in the way, move all the way
  15. break;
  16. case left:
  17. for(int i = getX(); i <= getX() - spacesToMove; i--) {
  18. if(getWorld()->pebbleThere(i,y)) {
  19. pebbleAt = i;
  20. moveTo(i,y);
  21. break;
  22. }
  23.  
  24. }
  25. moveTo(x-spacesToMove,y);
  26. break;
  27. case up:
  28. for(int i = getY(); i <= getY() + spacesToMove; i++) {
  29. if(getWorld()->pebbleThere(x,i)) {
  30. pebbleAt = i;
  31. moveTo(x,i);
  32. break;
  33. }
  34. }
  35. moveTo(x,y+spacesToMove);
  36. break;
  37. case down:
  38. for(int i = getY(); i <= getY() - spacesToMove; i--) {
  39. if(getWorld()->pebbleThere(x,i)) {
  40. pebbleAt = i;
  41. moveTo(x,i);
  42. break;
  43. }
  44. }
  45. moveTo(x,y-spacesToMove);
  46. break;
  47. case none:
  48. break;
  49.  
  50. }
  51. }
  52.  
  53. BabyGrasshopper::BabyGrasshopper(int startX, int startY,
  54. StudentWorld* world)
  55. : Grasshopper(IID_BABY_GRASSHOPPER, startX, startY, none, world, 500)
  56. {
  57. cerr << "BABYGRASSHOPPA CONSTRUCTED" << endl;
  58. }
  59.  
  60. void BabyGrasshopper::doSomething() {
  61. // IF BLOCKED, SET DIR TO 0, (SET # OF TICKS TO SLEEP TO 2)
  62. // IF NOT BLOCKED, MOVE
  63.  
  64. int numOfSpaces = randInt(2,8);
  65.  
  66.  
  67.  
  68. //if it meets a pebble from (x,y) to (x+num,y) or (x,y+num)
  69.  
  70. switch(getDirection()) {
  71. case right:
  72. if(getWorld()->pebbleThere(getX()+1, getY()))
  73. setDirection(none);
  74. else {
  75. moveTilBlocked(getX(), getY(), numOfSpaces);
  76. break;
  77. }
  78. case left:
  79. if(getWorld()->pebbleThere(getX()-1, getY()))
  80. setDirection(none);
  81. else {
  82. moveTilBlocked(getX(), getY(), numOfSpaces);
  83. break;
  84. }
  85. case up:
  86. if(getWorld()->pebbleThere(getX(), getY()+1))
  87. setDirection(none);
  88. else {
  89. moveTilBlocked(getX(), getY(), numOfSpaces);
  90. break;
  91. }
  92. case down:
  93. if(getWorld()->pebbleThere(getX(), getY()-1))
  94. setDirection(none);
  95. else {
  96. moveTilBlocked(getX(), getY(), numOfSpaces);
  97. break;
  98. }
  99. case none:
  100. //changeDir();
  101. break;
  102. }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement