Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- public class FlexibleBot extends Bot{
- public static int UP_RIGHT=4, DOWN_RIGHT=5, DOWN_LEFT=6, UP_LEFT=7;
- /**
- * initialize all instance variables.
- * The Bot has a Location, id, direction, and moveSpeed
- *
- * @param l
- * @param id
- * @param dir
- * @param speed
- */
- public FlexibleBot(Location l, int id, int dir, int speed) {
- super(l, id, dir, speed);
- }
- public void counterTurn(){
- if(getDirection()==0){
- setDirection(LEFT);
- }
- else{
- setDirection(getDirection()-1);
- }
- }
- public void speedUp(){
- setMoveSpeed(getMoveSpeed()+1);
- }
- public void speedDown(){
- setMoveSpeed(getMoveSpeed()-1);
- }
- @Override
- public boolean move(Map m){
- boolean moveMade=super.move(m);
- while(!moveMade){
- if(botDistanceFromEdge()<getMoveSpeed()){
- putBotOnEdge();
- moveMade=true;
- }
- else{
- turn();
- moveMade=super.move(m);
- }
- }
- return moveMade;
- }
- public boolean teleport(Map m, int row, int col){
- boolean moveMade=validMove(m, row, col);
- if(moveMade){
- getLoc().setRow(row);
- getLoc().setCol(col);
- }
- return moveMade;
- }
- private int botDistanceFromEdge(){
- int distance=0;
- switch(getDirection()){
- case UP:
- distance=getLoc().getRow();
- break;
- case LEFT:
- distance=getLoc().getCol();
- break;
- case RIGHT:
- distance=Map.NUM_COLS-1-getLoc().getCol();
- break;
- case DOWN:
- distance=Map.NUM_ROWS-1-getLoc().getRow();
- break;
- }
- return distance;
- }
- private void putBotOnEdge(){
- switch(getDirection()){
- case UP:
- getLoc().setRow(0);
- break;
- case LEFT:
- getLoc().setCol(0);
- break;
- case RIGHT:
- getLoc().setCol(Map.NUM_COLS-1);
- break;
- case DOWN:
- getLoc().setRow(Map.NUM_ROWS-1);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement