⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ship.java

📁 proxy server for caching and packet filtering final project
💻 JAVA
字号:
/**
 * Details of a ship and its status on the board
 */
public class Ship{                          
  /**
   * Name of the ship
   */
  private String name;
  /**
   * Number of holes this ship occupies
   */
  private int length;                
  /**
   * Location of the body parts : an array 0..length-1
   */
  private Location body[];              
  /**
   * Indices of the body parts destroyed
   */
  private int hit[];                   
  /**
   * Number of body parts destroyed
   */
  private int hitNumber;                
  /**
   * Creates a ship, and places it in a specified place.
   * @param n Ship's name
   * @param l Ship's length
   * @param end Location of Ship's top, left end point
   * @param dir Direction to place the Ship (1=vertical, 0=horizontal)
   */
  Ship(String n, int l, Location end, int dir){  //places ship starting at location end, in the direction of dir
  name=n;
  length=l;
  hitNumber=0;
  body = new Location[l];
  hit=new int[l];
  if(dir==1){                         //Vertical alignment
    for(int i=0;i<l;i++)
      body[i] = new Location(end.row() + i,end.col());
  }
  else{                               //Horizontal alignment
    for(int i=0;i<l;i++)
      body[i] = new Location(end.row(),end.col() + i);
  } 
}                 
/**
 * @return Length of this ship
 */
public int length(){               
  return length;
}
/**
 * Get Location of specified body part
 * @param i index of body part required
 * @return Location of body part i
 */
public Location locPart(int i){       
  if(i<length)
    return body[i];
  else return null;
}
/**
 * @return name and length of Ship, as a string
 */
public String detail(){
  return (name+" : "+length+" holes");
}
/**
 *@return True if all body parts are hit
 */
public boolean isSunk(){            
return (hitNumber==length);
}
/**
 * @return number of body parts hit
 */
public int numHit(){
  return hitNumber;
}
/**
 * Marks body part i, as hit
 * @param i number of body part to be destroyed
 */
private void hit(int i){              
  boolean old=false;
  for(int c=0;c<hitNumber;c++)
    old=old||(i==hit[c]);
  if(!old){
  hit[hitNumber]=i;
  hitNumber++;
 }
}
/**
 * Carries out opponent's guess.
 * @param l Location guessed
 * @return true if this ship occupies location l
 */
public boolean guess(Location l){        
for(int i=0;i<length;i++)
  if(l.equal(body[i])){                  //if some body part coincides with location guessed
  hit(i);
  return true;
}
return false;
}
/**
 * Checks if two ships are in the same hole
 * @param s Ship to compare with
 * @return true if this ship and s have a common hole
 */
public boolean isClash(Ship s){          
  boolean clash=false;                   
  for(int i=0;i<length;i++)
    for(int j=0;j<s.length();j++)
    clash=clash||body[i].equal(s.locPart(j));
  return clash;
}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -