Java 爪哇战舰的转弯管理

Java 爪哇战舰的转弯管理,java,Java,基本上,我正在尝试用java制作战舰游戏。最终的目标是实现一个GUI版本的MVC,但目前我正在尝试使用控制台输出使游戏模型工作。我还创建了一个简单的游戏引擎来管理游戏。我已经得到了游戏的大部分工作的意义上,游戏将自动放置船只随机为两个玩家,用户可以输入坐标,以打击每个玩家轮流 我遇到的问题是,当一个玩家指定一个他们已经命中的坐标时,不管是未命中还是战舰命中,它会向你已经命中的控制台指定正确的消息,再试一次,但它会将回合传递给另一个玩家,而不允许玩家指定另一组要命中的坐标 我真的不知道该怎么解决这

基本上,我正在尝试用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];
    }
任何帮助都将不胜感激,
谢谢

如果shootAtShip没有返回成功值,理想情况下您不会推进回合。因此,当从射击飞船返回10或11时,设置一个标志,表示不推进回合。

您应该在消息为玩家回合后到调用下一回合前进行嵌套循环。当玩家状态为10或11时,该循环应重复执行类似于okToProceed的操作,该操作默认为false,仅当shootAtShip返回10或11以外的值时才设置为true。okToProceed=射击结果!=10&&shootResult!=11; 或者诸如此类的事情。似乎无法让它发挥作用;似乎循环成一个镜头两次显示在控制台你错过了…,那么,你已经在这里拍摄!再试一次;即使说坐标0,0是空的…并且保持在这个位置上,实际上,更好的方法是返回一个新的状态标志,表示丢失,如15,并且不将其保存在黑板上。对不起,我删除了以前的评论。我还没有修复显示2条错误消息的问题…在一个真正的未命中快照上,现在它将显示您未命中的2条消息…,您已经在这里拍摄了。。。正确地过弯。在已经完成的快照上,它会显示您已在此处拍摄的正确消息。。。而且没有过弯。我认为返回语句中有一个我没有看到的问题。感谢您的输入,到目前为止,我不认为return语句是在用户错过时退出该方法;不知道为什么?你可以使用一个回合计数器,基本上如果回合%2==0,那么轮到玩家2了,其他的都是玩家1了。你不必编辑你的问题,事实上,这是个坏主意。如果一个答案对你有帮助,接受它,我想你已经接受了;如果你解决了,写下你自己的答案并接受它。
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";
    }
}