-2

基本的に私は Java で戦艦ゲームを作ろうとしています。最終的な目標は、GUI バージョン (MVC) を実装することですが、現在、コンソール出力を使用してゲームのモデルを動作させようとしています。また、ゲームを管理するための簡単なゲーム エンジンも作成しました。私は、ゲームが両方のプレイヤーのために船を自動的に (ランダムに) 配置し、ユーザーが座標を入力して各プレイヤーを攻撃できる (ターンごとに) という意味で、ゲームの大部分を動作させました。

私が抱えている問題は、プレイヤーが既にヒットした座標を指定すると (ミスまたは戦艦でのヒット)、コンソールに正しいメッセージが指定されることです (あなたはすでにここでヒットしています, TRY AGAIN)ただし、プレーヤーがヒットする別の座標セットを指定することを許可せずに、ターンを他のプレーヤーに渡します。

これを回避する方法がよくわかりません。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";
    }
}

どんな助けでも大歓迎です、ありがとう

4

2 に答える 2