战舰麻烦(Java)

时间:2014-04-18 15:30:40

标签: java

我正在尝试用Java制作一个战舰游戏,我在那里放入船只的坐标,然后对手(计算机)放入他们的坐标。然后,两个玩家都进入行/列以选择地图上的一个点。但随后,它崩溃了。

这是代码。

import java.util.Random;
import java.util.Scanner;

public class battleShip {

public static void main(String[] args) {
    int[][] board = new int[5][5];
    int[][] ships = new int[5][5];
    int[] shoot = new int[5];
int[] shoot2 = new int[4];
    int attempts=0,
        shotHit=0;

    initBoard(board);
initBoard2(board);
    initShips(ships);

    System.out.println();

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

        if(hit(shoot,ships)){
            hint(shoot,ships,attempts);
            shotHit++;
        }      
    if(hit2(shoot2,ships)){
            hint2(shoot2,ships,attempts);
            shotHit++;
        }                
        else
            hint(shoot,ships,attempts);
    hint2(shoot2,ships,attempts);

        changeboard(shoot,ships,board);

    changeboard2(shoot2,ships,board);
    }while(shotHit!=4);

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

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

public static void showBoard(int[][] board){
    System.out.println("\t1 \t2 \t3 \t4 \t5");
    System.out.println();

    for(int row=0 ; row < 5 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 5 ; column++ ){
            if(board[row][column]==-1||board[row][column]==-1||board[row][column]==-3){
                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 showBoard2(int[][] board){
    System.out.println("\t1 \t2 \t3 \t4 \t5");
    System.out.println();

    for(int row2=0 ; row2 < 5 ; row2++ ){
        System.out.print((row2+1)+"");
        for(int column2=0 ; column2 < 5 ; column2++ ){
            if(board[row2][column2]==-1||board[row2][column2]==-1||board[row2][column2]==-3){
                System.out.print("\t"+"~");
            }else if(board[row2][column2]==0){
                System.out.print("\t"+"*");
            }else if(board[row2][column2]==1){
                System.out.print("\t"+"X");
            }



        }
        System.out.println();
    }

}

public static void initShips(int[][] ships){
    Random random = new Random();
    Scanner input = new Scanner(System.in);
    for(int ship=0 ; ship < 4 ; ship++){
        ships[ship][0]=input.nextInt();
        ships[ship][1]=input.nextInt();
    ships[ship][2]=random.nextInt(4);
        ships[ship][3]=random.nextInt(4);

        //let's check if that shot was already tried 
        //if it was, just finish the do...while when a new pair was randomly selected
        for(int last=0 ; last < ship ; last++){
            if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1])&&(ships[ship][2] == ships[last][2])&&(ships[ship][3] == ships[last][3]) )
                do{

        ships[ship][2]=random.nextInt(4);
                ships[ship][3]=random.nextInt(4);
                }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1])&& (ships[ship][2] == ships[last][2])&&(ships[ship][3] == ships[last][3]) );
        }

    }
}

public static void shoot(int[] shoot){
Random random = new Random();
    Scanner input = new Scanner(System.in);

    System.out.print("Row: ");
    shoot[0] = input.nextInt();
    shoot[0]--;

    System.out.print("Column: ");
    shoot[1] = input.nextInt();
    shoot[1]--;
}
public static void shoot2(int[] shoot2){
Random random = new Random();
    Scanner input = new Scanner(System.in);

System.out.print("Computer turn: ");
    shoot2[2] = random.nextInt(5);
    shoot2[2]--;


    shoot2[3] = random.nextInt(5);
    shoot2[3]--;

}

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 boolean hit2(int[] shoot2, int[][] ships){

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

public static void hint(int[] shoot, int[][] ships, int attempt){
    int row=0,
        column=0;

    for(int line=0 ; line < ships.length ; line++){
        if(ships[line][0]==shoot[0])
            row++;
        if(ships[line][1]==shoot[1])
            column++;

    }

    System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +
                             "Column %d -> %d ships\n",attempt,shoot[0]+1,row,shoot[1]+1,column);
 }
public static void hint2(int[] shoot2, int[][] ships, int attempt){
    int row2=0,
        column2=0;

    for(int line=0 ; line < ships.length ; line++){
    if(ships[line][2]==shoot2[2])
            row2++;
    if(ships[line][3]==shoot2[3])
            column2++;

    }
System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +
                             "Column %d -> %d ships\n",attempt,shoot2[2]+1,row2,shoot2[3]+1,column2);
}

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;
}    
public static void changeboard2(int[] shoot2, int[][] ships, int[][] board){   
if(hit2(shoot2,ships))
    board[shoot2[2]][shoot2[3]]=1;
else
    board[shoot2[2]][shoot2[3]]=0;
}
}

当我在CMD中运行它时会发生这种情况。

C:\Users\Me\Desktop\Battleship>javac battleShip.java

C:\Users\Me\Desktop\Battleship>java battleShip
1
2
3
4
5
1
2
3

         1       2       3       4       5

1       ~       ~       ~       ~       ~
2       ~       ~       ~       ~       ~
3       ~       ~       ~       ~       ~
4       ~       ~       ~       ~       ~
5       ~       ~       ~       ~       ~
        1       2       3       4       5

1       ~       ~       ~       ~       ~
2       ~       ~       ~       ~       ~
3       ~       ~       ~       ~       ~
4       ~       ~       ~       ~       ~
5       ~       ~       ~       ~       ~
Row: 2
Column: 3
Computer turn: You hit a ship located in (2,3)

Hint 1:
Row 2 -> 1 ships
Column 3 -> 1 ships

Hint 1:
Row 2 -> 1 ships
Column 3 -> 1 ships

Hint 1:
Row 4 -> 1 ships
Column 0 -> 0 ships
You hit a ship located in (2,3)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at battleShip.changeboard2(battleShip.java:216)
        at battleShip.main(battleShip.java:41)
    Issue occurs when person selects column or row 0

我真的很感激帮助:(

1 个答案:

答案 0 :(得分:0)

public int nextInt(int n)

返回伪随机,均匀分布的int值介于0(包括)和指定值(不包括)

之间
public static void shoot2(int[] shoot2){
    Random random = new Random(System.currentTimeMillis());
    System.out.print("Computer turn: ");
    shoot2[2] = random.nextInt(5);
    shoot2[3] = random.nextInt(5);
}