Tuesday, October 26, 2010

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

public class Zombie extends Monster 
{
 
private int x;
 
private int y;
 
 
public Zombie(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);
   
   
this.setY(this.getY() + 1);
   
   
// Wraps around ( if it goes beyond GRID)
   
if((this.getY()) > 9)
   
this.setY(0);
   
   
this.TheZombieAct(theD, 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);
   
   
this.setY(this.getY() - 1);
   
   
// Wraps around ( if it goes beyond GRID)
   
if((this.getY()) < 0)
   
this.setY(9);
   
   
   
this.TheZombieAct(theD, 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);
   
   
this.setX(this.getX() - 1);
   
   
// Wraps around ( if it goes beyond GRID)
   
if((this.getX()) < 0)
   
this.setX(9);
   
   
this.TheZombieAct(theD, this.getX(), this.getY());
 
}
 
else
 
{
   
System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
 
}
 
}
 
 
// Move east
 
public void moveEast(Dungeon theD)
 
{
 
if(this.getLiveStatus() == Monster.LiveStatus.LIVING)
 
{
 
   
// Removes itself from current position
   theD
.getTheDungeon()[this.getX()][this.getY()].deleteTheMonster(this);
   
   
this.setX(this.getX() + 1);
   
   
// Wraps around ( if it goes beyond GRID)
   
if((this.getX()) > 9)
   
this.setX(0);
   
   
this.TheZombieAct(theD, this.getX(), this.getY());
 
}
 
else
 
{
   
System.out.println(this.getName() + " " + this.getMonsterType() + " IS DEAD ");
 
}
 
}
 
 
 
/* **************************************************************** */
 
 
// The Zombie's Act
 
 
public void TheZombieAct(Dungeon theD, int x, int y)
 
{
 
// The Zombie now deletes all monsters at Dungeon[x][y + 1]
  theD
.deleteMonsters(x, y);
 
 
// And it adds itself at Dungeon[x][y + 1] after having eaten everyone at Dungeon[x][y + 1]
  theD
.addMonsters(this, x, y);
 
}
 
 
/* **************************************************************** */
 
 
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: