📄 battleship.java
字号:
/**
* Details of the current game
*/
public class Battleship{
/**
* Computer's ship board
*/
private ShipBoard comp;
/**
* User's guess board
*/
private GuessBoard user;
/**
* Status of the game : true when user has won
*/
private boolean win;
/**
* Initialize the game. Places computer's ships randomly on the board.
*/
Battleship(){
win=false;
user = new GuessBoard();
comp = new ShipBoard(); //for placing computer's ships randomly
}
/**
* Get the results of user's guess.
* @param l Location to be queried
* @return 1 if location l has not been guessed; 2 if location l was a hit; 3 if location l was a miss
*/
public int guessStatus(Location l){
if (user.isNew(l)) return 1; //not guessed so far
if (user.wasHit(l)) return 2; //guessed and was a hit
return 3; //guessed and was a miss
}
/**
* Carry out the user's guess, by modifying the shipBoard and guessBoard
* @param l Location that the user guesses
* @return index of the ship (in computer's shipBoard) that is hit. Returns 5 if no ship is hit.
*/
public int userGuess(Location l){ //carries out the user's guess, returns ship number hit (if any)
int res=5; //no ship hit
boolean strike=false;
for(int i=0;i<5;i++)
if(comp.hitShip(l,i)){
res=i;
strike=true;
}
user.markGuess(l,strike); //mark result of this guess in user's guess board
win=comp.isDestroyed(); //check if computer's ships are all destroyed
return res;
}
/**
* @return An array containing the status (number of holes hit) of computer's ships
*/
public int[] compStat(){ //get status of computer's ships - for display
return comp.getFleetStat();
}
/**
* @return The number of guesses made by user
*/
public int numUserGuess(){
return user.numGuesses();
}
/**
* @return True if user has finished the game
*/
public boolean win(){
return win;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -