void NewPosition(MazewarInstance::Ptr m) //void NewPosition(MazewarInstance *m) { Loc newX(0); Loc newY(0); Direction dir(0); /* start on occupied square */ while (M->maze_[newX.value()][newY.value()]) { /* MAZE[XY]MAX is a power of 2 */ newX = Loc(random() & (MAZEXMAX - 1)); newY = Loc(random() & (MAZEYMAX - 1)); /* In real game, also check that square is unoccupied by another rat */ } /* prevent a blank wall at first glimpse */ if (!m->maze_[(newX.value())+1][(newY.value())]) dir = Direction(NORTH); if (!m->maze_[(newX.value())-1][(newY.value())]) dir = Direction(SOUTH); if (!m->maze_[(newX.value())][(newY.value())+1]) dir = Direction(EAST); if (!m->maze_[(newX.value())][(newY.value())-1]) dir = Direction(WEST); m->xlocIs(newX); m->ylocIs(newY); m->dirIs(dir); }
void _backward() { register int tx = MY_X_LOC; register int ty = MY_Y_LOC; switch(MY_DIR) { case NORTH: if (!M->maze_[tx-1][ty]) tx--; break; case SOUTH: if (!M->maze_[tx+1][ty]) tx++; break; case EAST: if (!M->maze_[tx][ty-1]) ty--; break; case WEST: if (!M->maze_[tx][ty+1]) ty++; break; default: MWError("bad direction in Backward"); } if ((MY_X_LOC != tx) || (MY_Y_LOC != ty)) { M->xlocIs(Loc(tx)); M->ylocIs(Loc(ty)); updateView = TRUE; } }
/* ----------------------------------------------------------------------- */ void forward(void) { bool allow = true; register int tx = MY_X_LOC; register int ty = MY_Y_LOC; switch(MY_DIR) { case NORTH: { for (int i = 0; i < 8 ; i++) { if(rat_array[i] != NULL) { if (M->mazeRats_[i].x.value() == MY_X_LOC+1 && MY_Y_LOC == M->mazeRats_[i].y.value()) { allow = false; break; } } } break; } case SOUTH: { for (int i = 0; i < 8 ; i++) { if(rat_array[i] != NULL) { if (M->mazeRats_[i].x.value() == MY_X_LOC-1 && MY_Y_LOC == M->mazeRats_[i].y.value()) { allow = false; break; } } } break; } case EAST: { for (int i = 0; i < 8 ; i++) { if(rat_array[i] != NULL) { if (M->mazeRats_[i].x.value() == MY_X_LOC && MY_Y_LOC+1 == M->mazeRats_[i].y.value()) { allow = false; break; } } } break; } case WEST: { for (int i = 0; i < 8 ; i++) { if(rat_array[i] != NULL) { if (M->mazeRats_[i].x.value() == MY_X_LOC && MY_Y_LOC-1 == M->mazeRats_[i].y.value()) { allow = false; break; } } } break; } default: exit(0); } if (allow){ switch(MY_DIR) { case NORTH: if (!M->maze_[tx+1][ty]) tx++; break; case SOUTH: if (!M->maze_[tx-1][ty]) tx--; break; case EAST: if (!M->maze_[tx][ty+1]) ty++; break; case WEST: if (!M->maze_[tx][ty-1]) ty--; break; default: MWError("bad direction in Forward"); } if ((MY_X_LOC != tx) || (MY_Y_LOC != ty)) { M->xlocIs(Loc(tx)); M->ylocIs(Loc(ty)); updateView = TRUE; } } }