基本上我正在尝试用java制作战舰游戏。最终目标是实现一个 GUI 版本 (MVC),但目前我正试图让游戏模型使用控制台输出工作。我还创建了一个简单的游戏引擎来管理游戏。我已经让游戏的大部分工作在某种意义上说,游戏会自动为两个玩家放置船只(随机),并且用户可以输入坐标以击中每个玩家(轮流)。
我遇到的问题是,当玩家指定他们已经击中的坐标(无论是未命中还是击中战舰)时,它会向控制台指定正确的消息(您已经在这里击中,再试一次)但它会将回合传递给其他玩家,而不允许玩家指定另一组要击球的坐标。
我不确定如何解决这个问题。在 GUI 版本中,我想这并不是非常重要,因为当射击时,网格上的 JButton 将只是禁用所以它是不可点击的???
我已经包含了我的 PlayingBoard 类的代码片段:
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";
}
}
任何帮助将不胜感激,谢谢