这是我尝试使用Java重建战舰。我决定只用一艘船来测试更简单的游戏版本,并在游戏板上为游艇提供一个具体的位置。我发现我的代码有问题。无论我进入什么样的坐标,我最终“击中”了这艘船。
以下是我到目前为止编写的所有代码:
import java.util.Scanner;
class GameBoard {
Scanner input = new Scanner(System.in); // scanner object
String[][] board = { // game board
{"_", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10"},
{"A", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"B", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"C", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"D", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"E", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"F", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"G", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"H", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"I", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
{"J", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"}
};
boolean frigateIsAlive = true; // the ship is still alive
int numOfHitsOnFrigate = 0; // number of hits the player made on the frigate
String [] frigate = {board[1][1], board[1][2]}; // ship
public void createBoard(){ // draws the battleship game board
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col] + "\t");
} // inner loop
System.out.println();
System.out.println();
System.out.println();
} // outer loop
}
public String getUserGuess() { // takes the users guess
System.out.println("Choose a coordinate on the board to fire at");
int x = input.nextInt();
int y = input.nextInt();
String userGuess = board[x][y];
return userGuess;
}
public void checkResult(String userGuess) { // checks the user's guess
if(userGuess.equalsIgnoreCase(frigate[0])){
System.out.println("hit!");
numOfHitsOnFrigate++;
board[1][1] = " *";
createBoard();
}
else if(userGuess.equalsIgnoreCase(frigate[1])) {
System.out.println("hit!");
numOfHitsOnFrigate++;
board[1][2] = " *";
createBoard();
}
else {
System.out.println("miss!");
}
if (numOfHitsOnFrigate == 2) {
System.out.println("Enemy frigate has been sunk!");
frigateIsAlive = false;
}
}
} // end class
public class Game {
public static void run() {
GameBoard newGame = new GameBoard();
newGame.createBoard();
while(newGame.frigateIsAlive) {
newGame.checkResult(newGame.getUserGuess());
}
}
}
public class App {
public static void main(String[] args) {
Game.run();
}
}
答案 0 :(得分:1)
由于frigate
的声明是:
frigate = {board[1][1], board[1][2]}
,最终将字符串'[ ]'
分配给护卫舰的两个值。然后,当您正在寻找护卫舰并比较值时,将其与更多空字符串进行比较。
这可以通过在[1,2,3,4, n]
和[A,B,C...,Letter_n]
中创建位置x的板来修复。也就是说,护卫舰的坐标为Frigate.x = 1
和Frigate.y = A
。
我希望这有帮助!
我看到了你如何实现这一点的进一步问题。我会让Frigate成为一个有坐标列表的类:
this.x
作为一个字母或数字
this.y
作为一个点不是像你的例子中那样的类型
元组(this.x, this.y)
在您的列表Frigate中运行良好
对Frigate列表中的任何其他点执行相同的操作。
在Frigate列表完成之后,还有两件事需要改变。
首先要改变的是如何检查用户是否正在调用您想要的范围内的内容。
必须改变的第二件事是如何确保不会一次又一次地调用同一点以“炸毁”一艘船。也就是说,当Frigate中的一个点被调用时,它应该从Frigate中移除。护卫舰中剩余的元组将成为护卫舰留下的“生命值”。要想起Frigate的原始大小,添加Frigate.initialSize()
会非常方便,但这可能会在以后发生。