📄 guessboard.java
字号:
/**
* Records all the guesses and their results of a player
*/
public class GuessBoard{
/**
* An array holding all locations guessed so far
*/
private Location guess[]; //all prior guesses
/**
* Results of all guesses so far. result[i] is true if the guess at location guess[i] hit a ship.
*/
private boolean result[];
/**
* Number of guesses so far
*/
private int numGuess;
/**
* Initializes the arrays to the maximum size (100)
*/
GuessBoard(){
numGuess=0;
guess=new Location[100];
result=new boolean[100];
}
/**
* @param l Location to be checked
* @return True if location l has already been guessed.
*/
public boolean isNew(Location l){
boolean old=false;
for(int i=0;i<numGuess;i++)
old=(old||l.equal(guess[i]));
return (!old);
}
/**
* Marks the result of a guess
* @param l Location corresponding to the guess
* @param res Result of the guess at location l (true=hit, false=miss)
*/
public void markGuess(Location l,boolean res){
if(isNew(l)){
guess[numGuess]=l;
result[numGuess]=res;
numGuess++;
}
}
/**
* Tests whether a location was guessed and hit
* @param l Location to test
* @return True if location l was guessed earlier and was a hit
*/
public boolean wasHit(Location l){
for(int i=0;i<numGuess;i++) //location guessed before
if(l.equal(guess[i])) return result[i];
return false; //not guessed so far
}
/**
* @return The number of guesses so far
*/
public int numGuesses(){
return numGuess;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -