📄 shipboard.java
字号:
/**
* Records all the ships of a player
*/
public class ShipBoard {
/**
* The number of ships on this ship board
*/
private int size;
/**
* All the ships on this ship board
*/
private Ship fleet[];
/**
* Builds a Shipboard containing specified ships
* @param f the ships to be added to this Shipboard
* @param s the number of ships to be added
*/
ShipBoard(Ship f[],int s){ //Constructor - specifies the ships to be added
fleet=f;
size=s;
}
/**
* Builds a standard ship board containing five ships in the order : PatrolBoat,Destroyer,Submarine,BattleShip,Carrier.
* Places all the ships randomly on the board.
*/
ShipBoard(){
Location shipL;
int r,c;
int shipD;
boolean done; //to find non overlapping locations for ships
fleet=new Ship[5];
size=5;
//initialize Patrol Boat
shipD=(int)(2*Math.random()); //get a random direction to align (row/column)
if (shipD==1){ //column alignment
c=(int)(10*Math.random());
r=(int)(9*Math.random()); //get random hole to place end of ship - so that it lies within the board
}
else{ //row alignment
c=(int)(9*Math.random());
r=(int)(10*Math.random());
}
shipL=new Location(r,c);
fleet[0]=new Ship("Patrol Boat",2,shipL,shipD);
//initialize Destroyer
done=false;
while(!done){
shipD=(int)(2*Math.random());
if (shipD==1){ //column alignment
c=(int)(10*Math.random());
r=(int)(8*Math.random());
}
else{ //row alignment
c=(int)(8*Math.random());
r=(int)(10*Math.random());
}
shipL=new Location(r,c);
fleet[1]=new Ship("Destroyer",3,shipL,shipD);
done=true;
for(int i=0;i<1;i++) //Check if this ship clashes with any previously placed ship
done=done&&(!fleet[1].isClash(fleet[i]));
}
//initialize Submarine
done=false;
while(!done){
shipD=(int)(2*Math.random());
if (shipD==1){ //column alignment
c=(int)(10*Math.random());
r=(int)(8*Math.random());
}
else{ //row alignment
c=(int)(8*Math.random());
r=(int)(10*Math.random());
}
shipL=new Location(r,c);
fleet[2]=new Ship("Submarine",3,shipL,shipD);
done=true;
for(int i=0;i<2;i++) //Check if this ship clashes with any previously placed ship
done=done&&(!fleet[2].isClash(fleet[i]));
}
//initialize Battleship
done=false;
while(!done){
shipD=(int)(2*Math.random());
if (shipD==1){ //column alignment
c=(int)(10*Math.random());
r=(int)(7*Math.random());
}
else{ //row alignment
c=(int)(7*Math.random());
r=(int)(10*Math.random());
}
shipL=new Location(r,c);
fleet[3]=new Ship("Battleship",4,shipL,shipD);
done=true;
for(int i=0;i<3;i++) //Check if this ship clashes with any previously placed ship
done=done&&(!fleet[3].isClash(fleet[i]));
}
//initialize Carrier
done=false;
while(!done){
shipD=(int)(2*Math.random());
if (shipD==1){ //column alignment
c=(int)(10*Math.random());
r=(int)(6*Math.random());
}
else{ //row alignment
c=(int)(6*Math.random());
r=(int)(10*Math.random());
}
shipL=new Location(r,c);
fleet[4]=new Ship("Carrier",5,shipL,shipD);
done=true;
for(int i=0;i<4;i++) //Check if this ship clashes with any previously placed ship
done=done&&(!fleet[4].isClash(fleet[i]));
}
}
/**
* Check if the specified ship is hit in a location
* @param l Location to check
* @param n index of the ship to check
* @return true if ship fleet[n] occupies location l
*/
public boolean hitShip(Location l,int n){ //check if ship number n is hit
if(n<size)
return fleet[n].guess(l);
else return false;
}
/**
* Test if all ships on this Shipboard are sunk
* @return true if all ships are sunk
*/
public boolean isDestroyed(){ //Check if all the ships are sunk
boolean des=true;
for(int i=0;i<size;i++)
des=des&&fleet[i].isSunk();
return des;
}
/**
* Get the status of all ships on this Shipboard
* @return An array of length size. The entry in the i-th position is the number of hits on ship fleet[i]
*/
public int[] getFleetStat(){
int stat[]=new int[size];
for(int i=0;i<size;i++)
stat[i]=fleet[i].numHit(); //number of hits on ship i
return stat;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -