0

Java ゲームの作成を学んでいますが、まだ Java は初めてです。今、私は戦艦ゲームを作りたいと思っています。しかし、今ここで立ち往生しています。今、シペをコンピューターボードとしてランダムに配置すると、前の船と重なってゲームのバランスが悪くなることがあります。次に、プレーヤーから入力を取得した後、入力値をボードに入れるにはどうすればよいですか。これが私が持っているコードです:

import javax.swing.JOptionPane;
import java.util.Random;
import java.util.Scanner;

public class Battleship
{
    public static void main (String args[]){
        String map [][][]=new String [10][10][2];
        int row =0,col=0;

        //initPlayerMap(map);
        //printMap(map,true);// true to printout the map
        placeShips(map); // place the shipe at computer map
        initMap(map,"~", true);
        initMap(map,"#",false);
        placeShips(map); // place the shipe at computer map
        printMap(map,true);
        System.out.println("Now enter your coordinate of the boom");
        row = getInput("Please enter row: ");
        col = getInput("Please enter col: ");
        printMap(map,false); // computer map
        hitShip(row,col);

    }

    private static void hitShip (int row, int col){
        if (map[startFrom++][colOrRow][1]== map[row][col][1]){
            System.out.println("abc");
        }
        else
        {
            System.out.println("darn!");
        }
    }
    private static void initMap(String map[][][] , String initChar, boolean player){
        //the 0 in 3rd dimension is representing player map and 1 for computer
        int mapNo= (player?0:1);
        for (int i = 0 ; i < 10; i ++)
            for (int j=0; j<10; j++)
                map [i][j][mapNo]= initChar;
    }

    private static void printMap(String map[][][], boolean player){
        int whichMap=0;
        if (!player)
            whichMap=1;

        System.out.println(" 0\t1\t2\t3\t4\t5\t6\t7\t8\t9 ");
        for (int i = 0 ; i < 10; i ++){
            System.out.print(i+" ");
            for (int j=0; j<10; j++){
                System.out.print(map [i][j][whichMap]+ "\t");
            }
            System.out.print("\n");
        }
    }// end of printMap method

    public static int getInput(String message){
        Scanner sc = new Scanner (System.in);
        System.out.print(message);
        return sc.nextInt();
    }

    private static void placeShips(String[][][] grid)
    {
        char[] shipType = { 'B' , 'C' , 'F' , 'M' };
        int[] shipSize = { 5 , 4 , 3 , 2 };
        int[] shipNums = { 1 , 2 , 4 , 4 };

        for (int x = 0 ; x < shipType.length ; x++)
            for (int y = 1 ; y <= shipNums[x] ; y++)
            {

                String shipName = shipType[x]+""+y;
                placeShip(grid,shipName,shipSize[x]);
            }
    }

    private static void placeShip(String[][][] map, String shipName, int size){
        int direction = (int)(Math.random()*2);// 0 or 1
        int colOrRow = (int)(Math.random()*map.length); // pick
        int startFrom =(int)(Math.random()*(map.length-size)); // which cell?


        // placing the ship
        for(int i=0; i < size; i++){
            // weather is vertical or horizontal
            // vertical
            if (direction == 0 ){
            map[startFrom++][colOrRow][1] = shipName;

            }
            else {
                map[colOrRow][startFrom++][1] = shipName;
            }
        }
    }
}
4

3 に答える 3

4

まず、これを正しくモデル化していません(IMO)

私は java.awt.Rectangle をもっと活用したいと思います。ボードを Rectangle にすることから始め、次に各船も Rectangle にします。Rectangle (実際には Shape に由来する) にはメソッド contains(...) があるため、四角形が重なっているかどうかをすばやくテストできるはずです。

ボード上のショットをマークする限り、おそらくあなたの船は単なる長方形以上のものとして定義する必要があります - ヒットしたスポットの機能を船に与えます. ヒット/ミスショットにはjava.awt.Pointを使用できます

于 2012-05-18T15:27:14.577 に答える
0

ここで 2 つの質問をされていると思いますが、両方にお答えします。

  1. 船をフィールドに配置し、それらが重ならないようにするには、新しい船を配置しようとしているマスに船があるかどうかを確認します。ブール値の 2D 配列を作成します。この配列で、どの正方形が船であるかを保存します。
  2. ユーザー入力については、何を試しましたか? また、どのような問題が発生しましたか? しがみつくものがなければ、私はあなたに働くものを何も与えることができません。ユーザーに船の始点と終点の 2 つの座標を指定させることをお勧めします。そのデータを処理します。
于 2012-05-18T15:24:19.793 に答える
0

マップ内の「既に埋められている」位置を追跡するためにデータ構造を使用していないようです。そうすれば、マップで埋められていない位置を比較または「検証」できます。

于 2012-05-18T15:26:40.443 に答える