Tuesday, October 26, 2010

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


public class Ghost extends Monster 
{
 private int x;
 private int y;
 
 public Ghost(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());
  
 }
 
 /* ************************************************************** */
 
 // Getters and Setters
 
 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 a TILE without another GHOST
   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()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   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 south till it finds a TILE without another ghost
   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()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this TILE
   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 west till it finds a TILE without another ghost
   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()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   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 EAST till it finds a TILE without another ghost
   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()].contains(Monster.MonsterType.GHOST));
   
   // theD.getTheDungeon() --> returns Tile[][] array
   // theD.getTheDungeon()[x][y] --> returns a Tile Object --> containing Monsters[]
   
   // Now it adds itself to this CHANNEL
   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() + "\n";
  str = String.format("%s %s", super.getName(), super.getMonsterType().toString());
  return str;
 }
 

}

No comments: