基本上我正试图在java中制作战舰游戏。最终目标是实现GUI版本(MVC),但目前我正在尝试使用控制台输出来使游戏模型工作。我还创建了一个简单的游戏引擎来管理游戏。我已经让游戏的大部分工作在游戏将自动为两个玩家放置船只(随机)的意义上,并且用户可以输入坐标来为每个玩家命中(转弯)。
我遇到的问题是,当玩家指定他们已经击中的坐标时(无论是战舰的未命中还是击中),它会向控制台指定正确的信息(你已经在这里击中了,再试一次)但是它转向另一个玩家而不允许玩家指定另一组坐标。
我不确定如何解决这个问题。在GUI版本中,我认为这并不是非常重要,因为当一个镜头被触发时,网格上的JButton将被禁用,因此它是不可点击的???
我已经为我的PlayBoard类添加了代码片段:
public int shootAtShip(int x, int y) {
//0 = empty, not hit
//10 = not empty, missed
//11 = hit on a battleship
//Check if co-ordinates are out of bounds
if(x >= playingStatus.length || x < 0 || y >= playingStatus[0].length || y < 0) {
System.out.println("Your specified move was invalid");
}
//If grid location was empty, player has missed
if(playingStatus[x][y] == 0) {
playingStatus[x][y] = 10;
System.out.println("You missed...");
return playingStatus[x][y];
}
//If grid location has already been hit
if(playingStatus[x][y] == 10 || playingStatus[x][y] == 11) {
System.out.println("You have already shot here! Try again...");
}
//Hit in a field with Aircraft Carrier
if(playingStatus[x][y] == 1){
playingStatus[x][y] = 11;
this.carrierHit++;
System.out.println("You hit an Aircraft Carrier");
if(this.carrierHit == 5) {
System.out.println("You destroyed the Aircraft Carrier");
fleet.remove(carrier);
}
}
//Hit in a field with Battleship
if(playingStatus[x][y] == 2) {
playingStatus[x][y] = 11;
this.battleshipHit++;
System.out.println("You hit a Battleship");
if(this.battleshipHit == 4) {
System.out.println("You destroyed the Battleship");
fleet.remove(battleship);
}
}
//Hit in a field with 1st Destroyer
if(playingStatus[x][y] == 3) {
playingStatus[x][y] = 11;
this.destroyerHit++;
System.out.println("You hit Destroyer #1");
if(this.destroyerHit == 3) {
System.out.println("You destroyed the #1 Destroyer");
fleet.remove(destroyer);
}
}
//Hit in a field with 2nd Destroyer
if(playingStatus[x][y] == 4) {
playingStatus[x][y] = 11;
this.destroyer2Hit++;
System.out.println("You hit Destroyer #2");
if(this.destroyer2Hit == 3) {
System.out.println("You destroyed the #2 Destroyer");
fleet.remove(destroyer2);
}
}
//Hit in a field with Patrol Boat
if(playingStatus[x][y] == 5) {
playingStatus[x][y] = 11;
this.patrolHit++;
System.out.println("You hit a patrol boat");
if(this.patrolHit == 2) {
System.out.println("You destroyed the Patrol Boat");
fleet.remove(patrol);
}
}
return playingStatus[x][y];
}
游戏引擎:
public void play() {
while(player1.board.isGameOver() == false || player2.board.isGameOver() == false) {
System.out.println("Player 1 ----------------------------");
player1.board.printBoard();
player1.board.printFleet();
System.out.println("Player2 -----------------------------");
player2.board.printBoard();
player2.board.printFleet();
System.out.println(currentTurn + " - It is this players turn");
System.out.println("Please enter target co-ordinates: ");
String move = userinput.nextLine();
int movex, movey;
movex = -2;
movey = -2;
StringTokenizer tokenizer = new StringTokenizer(move, ",");
if(tokenizer.countTokens() == 2) {
movex = Integer.parseInt(tokenizer.nextToken());
movey = Integer.parseInt(tokenizer.nextToken());
}
if(currentTurn == "player1") {
player2.board.shootAtShip(movex, movey);
}
if(currentTurn == "player2") {
player1.board.shootAtShip(movex, movey);
}
if(player1.board.isGameOver() == true) {
System.out.println("Player2 has won - Sorry");
break;
}
if(player2.board.isGameOver() == true) {
System.out.println("Player1 has won - Sorry");
break;
}
nextTurn();
}
}
public void nextTurn() {
if(this.currentTurn == "player2") {
currentTurn = "player1";
}
else {
currentTurn = "player2";
}
}
任何帮助将不胜感激, 感谢
答案 0 :(得分:0)
理想情况下,如果shootAtShip没有返回成功值,你就不会前进。因此,在从shootAtShip返回10或11时,设置一个标志意味着不要前进。
答案 1 :(得分:0)
你应该在“玩家转向”消息之后到下一回合的召唤之前进行嵌套循环。当玩家状态为10或11时,此循环应重复