Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Living::moveTilBlocked(int x, int y, int spacesToMove) {
- // use moveTo a certain x y that you find is ok to move to (blocked -1)
- int pebbleAt = 0;
- switch(getDirection()) {
- case right:
- for(int i = getX(); i <= getX() + spacesToMove; i++) {
- if(getWorld()->pebbleThere(i,y)) {
- pebbleAt = i;
- moveTo(i,y);
- break;
- }
- }
- moveTo(x+spacesToMove,y); // if no pebble in the way, move all the way
- break;
- case left:
- for(int i = getX(); i <= getX() - spacesToMove; i--) {
- if(getWorld()->pebbleThere(i,y)) {
- pebbleAt = i;
- moveTo(i,y);
- break;
- }
- }
- moveTo(x-spacesToMove,y);
- break;
- case up:
- for(int i = getY(); i <= getY() + spacesToMove; i++) {
- if(getWorld()->pebbleThere(x,i)) {
- pebbleAt = i;
- moveTo(x,i);
- break;
- }
- }
- moveTo(x,y+spacesToMove);
- break;
- case down:
- for(int i = getY(); i <= getY() - spacesToMove; i--) {
- if(getWorld()->pebbleThere(x,i)) {
- pebbleAt = i;
- moveTo(x,i);
- break;
- }
- }
- moveTo(x,y-spacesToMove);
- break;
- case none:
- break;
- }
- }
- BabyGrasshopper::BabyGrasshopper(int startX, int startY,
- StudentWorld* world)
- : Grasshopper(IID_BABY_GRASSHOPPER, startX, startY, none, world, 500)
- {
- cerr << "BABYGRASSHOPPA CONSTRUCTED" << endl;
- }
- void BabyGrasshopper::doSomething() {
- // IF BLOCKED, SET DIR TO 0, (SET # OF TICKS TO SLEEP TO 2)
- // IF NOT BLOCKED, MOVE
- int numOfSpaces = randInt(2,8);
- //if it meets a pebble from (x,y) to (x+num,y) or (x,y+num)
- switch(getDirection()) {
- case right:
- if(getWorld()->pebbleThere(getX()+1, getY()))
- setDirection(none);
- else {
- moveTilBlocked(getX(), getY(), numOfSpaces);
- break;
- }
- case left:
- if(getWorld()->pebbleThere(getX()-1, getY()))
- setDirection(none);
- else {
- moveTilBlocked(getX(), getY(), numOfSpaces);
- break;
- }
- case up:
- if(getWorld()->pebbleThere(getX(), getY()+1))
- setDirection(none);
- else {
- moveTilBlocked(getX(), getY(), numOfSpaces);
- break;
- }
- case down:
- if(getWorld()->pebbleThere(getX(), getY()-1))
- setDirection(none);
- else {
- moveTilBlocked(getX(), getY(), numOfSpaces);
- break;
- }
- case none:
- //changeDir();
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement