击中战舰错误连接方法

时间:2018-06-19 22:44:13

标签: java arrays methods

我遇到的问题是,当用户输入“ A2”时,程序将其喷出为A1。即使有人选择了“ B3”,它也会吐出A1。

我的Shoot方法连接到我的boolean hit方法时出现问题。我似乎无法正确完成阵列。

您中有更多经验丰富的用户可以告诉我我出了什么问题吗

public class Battleships {

    public static void main(String[] args) {
        int[][] board = new int[6][6];
        int[][] ships = new int[3][2];
        int[] shoot = new int[2];
        int attempts=0,
        shotHit=0;

        initBoard(board);
        initShips(ships);

        System.out.println();

        do{
            showBoard(board);
            shoot(shoot);
            attempts++;

            if(hit(shoot,ships)){
                shotHit++;
            }                
            else

                changeboard(shoot,ships,board);


        }while(shotHit!=3);

        System.out.println("\n\n\nBattleship Java game finished! You hit 3 ships in "+attempts+" attempts");
        showBoard(board);
    }

    public static void initBoard(int[][] board){
        for(int row=0 ; row < 6 ; row++ )
            for(int column=0 ; column < 6 ; column++ )
                board[row][column]=-1;
    }

    public static void showBoard(int[][] board){
        System.out.println("\tA \tB \tC \tD \tE \tF");
        System.out.println();

        for(int row=0 ; row < 6 ; row++ ){
            System.out.print((row+1)+"");
            for(int column=0 ; column < 6; column++ ){
                if(board[row][column]==-1){
                    System.out.print("\t"+"~");
                }else if(board[row][column]==0){
                    System.out.print("\t"+"*");
                }else if(board[row][column]==1){
                    System.out.print("\t"+"X");
                }

            }
            System.out.println();
        }

    }
    public static void initShips(int[][] ships){
        Random random = new Random();

        for(int ship=0 ; ship < 3 ; ship++){
            ships[ship][0]=random.nextInt(5);
            ships[ship][1]=random.nextInt(5);

            for(int last=0 ; last < ship ; last++){
                if ((ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]))
                    do{
                        ships[ship][0]=random.nextInt(5);
                        ships[ship][1]=random.nextInt(5);
                    } while ((ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]));
            }
        }
    }

    public static void shoot(int[] shoot){
        Scanner input = new Scanner(System.in);
        String[] attempt = new String[2];
        for(int i = 0; i < 10; i++) {
            System.out.println("Enter your guess: ");
            attempt[i] = input.next();
            for(int j = 0; j < 2; j++) {
                if (attempt[i].length() == 2 && Character.isLetter(attempt[i].charAt(0)) && Character.isDigit(attempt[i].charAt(1))) {
                    System.out.println("Good");
                    if (attempt[0].toUpperCase() == "A") {
                        shoot[0] = 1;
                        shoot[1] = Integer.valueOf(attempt[1]);
                    } else if (attempt[0].toUpperCase() == "B") {
                        shoot[0] = 2;
                        shoot[1] = Integer.valueOf(attempt[1]);
                    } else if (attempt[0].toUpperCase() == "C") {
                        shoot[0] = 3;
                        shoot[1] = Integer.valueOf(attempt[1]);
                    } else if (attempt[0].toUpperCase() == "D") {
                        shoot[0] = 4;
                        shoot[1] = Integer.valueOf(attempt[1]);
                    } else if (attempt[0].toUpperCase() == "E") {
                        shoot[0] = 5;
                        shoot[1] = Integer.valueOf(attempt[1]);
                    } else if (attempt[0].toUpperCase() == "F") {
                        shoot[0] = 6;
                        shoot[1] = Integer.valueOf(attempt[1]); 
                    }
                    return;
                } else {
                    System.out.println("invaid, please enter the proper format of letter then digit.");
                    j = 2;
                }
            }
        }
       input.close();
    }

    public static boolean hit(int[] shoot, int[][] ships){

        for(int ship=0 ; ship<ships.length ; ship++){
            if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
                System.out.printf("You hit a ship located in (%d,%d)\n",shoot[0]+1,shoot[1]+1);
                return true;
            }
        }
        return false;
    }


    public static void changeboard(int[] shoot, int[][] ships, int[][] board){
        if(hit(shoot,ships))
            board[shoot[0]][shoot[1]]=1;
        else
            board[shoot[0]][shoot[1]]=0;
    }
}

0 个答案:

没有答案