Tuesday, October 26, 2010

LabTest#2: Revised Solution -- Section 5 -- Class: Goblin


public class Goblin extends Monster 
{
 private int x;
 private int y;
 
 public Goblin(String n, MonsterType m, int x, int y, Dungeon theD)
 {
  super(n);
  super.setMonsterType(m);
  this.setX(x);
  this.setY(y);
  super.setLiveStatus(Monster.LiveStatus.LIVING);
  
  theD.addMonsters(this, this.getX(), this.getY());
  
 }
 
 /* ************************************************************* */
 
 // Setters and Getters
 
 public int getX() {
  return x;
 }

 public void setX(int x) {
  this.x = x;
 }

 public int getY() {
  return y;
 }

 public void setY(int y) {
  this.y = y;
 }
 
 
 /* ************************************************************** */
 
 // Movements
 
 // move North
 public void moveNorth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes North till it finds an empty space
   do
   {
    this.setY(this.getY() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if(this.getY() > 9)
     this.setY(0);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move South
 public void moveSouth(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setY(this.getY() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getY()) < 0)
     this.setY(9);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move West
 public void moveWest(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setX(this.getX() - 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) < 0)
     this.setX(9);
    
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 // move West
 public void moveEast(Dungeon theD)
 {
  if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
  {
  
   // Removes itself from current position
   theD.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
   // Goes on till it finds an empty space
   do
   {
    this.setX(this.getX() + 1);
    
    // Wraps around ( if it goes beyond GRID)
    if((this.getX()) > 9)
     this.setX(0);
    
   }while(theD.getTheDungeon()[this.getX()][this.getY()].getSize() > 0);
   
   // when it has found an empty space --> it then adds itself there
   theD.addMonsters(this, this.getX(), this.getY());
  }
  else
  {
   System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
  }
 }
 
 
 /* **************************************************************** */
 
 public String toString()
 {
  String str;
  //str = super.getName() + " " + super.getMonsterType().toString() + "\n";
  str = String.format("%s %s", super.getName(), super.getMonsterType().toString());
  return str;
 }
}

No comments: