Tuesday, October 26, 2010

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

public class Tile 
{
 
private Monster[] monsters;
 
private int size;
 
 
public Tile()
 
{
  monsters
= new Monster[100];
  size
= 0;
 
}
 
 
/* ******************************************************************* */
 
 
public void setSize(int s)
 
{
 
this.size = s;
 
}
 
 
/* ******************************************************************* */
 
 
public int getSize()
 
{
 
return this.size;
 
}
 
 
/* ******************************************************************* */
 
 
public void addTheMonster(Monster m)
 
{
  monsters
[size] = m;
  size
++;
 
}
 
 
/* ******************************************************************* */
 
 
public void deleteTheMonster(Monster m)
 
{
 
int index;
 
 
// If match found then delete
 
if((index = this.contains(m)) >= 0)
 
{
   
for(int i = index; i < this.getSize() - 1; i++)
   
{  
   
// The shift after deletion
    monsters
[i] = monsters[i+1];
   
}
   size
--;
 
}
 
}
 
 
/* ******************************************************************* */
 
 
public int contains(Monster m)
 
{
 
int match_index = -1;
 
 
for(int i=0; i < this.getSize(); i++)
 
{
   
if(monsters[i].getName().equalsIgnoreCase(m.getName()) && monsters[i].getMonsterType() == m.getMonsterType())
   
{
    match_index
= i;
   
break;
   
}
 
}
 
 
return match_index;
 
}
 
 
/* ******************************************************************* */
 
 
// returns the array of monsters at the particular tile
 
public Monster[] getMonsters()
 
{
 
return monsters;
 
}
 
 
/* ******************************************************************* */
 
 
// set the array of monsters for a particular tile
 
public void setMonsters(Monster[] m)
 
{
 
this.monsters = m;
 
}
 
 
/* ******************************************************************* */
 
 
// Checks to see if this CHANNEL contains anyone of Monster.MonsterType monsType
 
public boolean contains(Monster.MonsterType monsType)
 
{
 
boolean returnValue = false;
 
 
for(int i = 0; i < this.getSize(); i++)
 
{
   
if(monsters[i].getMonsterType() == monsType)
   
{
    returnValue
= true;
   
break;
   
}
 
}
 
return returnValue;
 
}
 
 
/* ******************************************************************* */
 
 
//
 
public String toString()
 
{
 
String str = "";
 
 
if(this.getSize() > 0)
 
{
   
for(int i = 0; i < this.getSize(); i++)
   
{
    str
+= String.format("Cell[%d]: %s %n",i, monsters[i]);
   
}
 
}
 
return str;
 
}
 
 
/* ******************************************************************* */

}

No comments: