mercredi 28 février 2018

Battleship Game Java

I need to place ships randomly in to dynamic array with size 10 by 10. The data type of the ships is char, like 'x'and there are 4 types of ships 1,2,3 and 4 x ships. Program should place this ships in to array randomly. Can anyone explain how i can place this ships in to array randomly? import java.util.Random;

public class Source {

    public static void main(String[] args) {

        char arr[][] = new char [10][10];
        Random r = new Random();

        int x = r.nextInt(10);
        int y = r.nextInt(10);

        arr [x][y] = 'x';






        for(int i=0; i<arr.length; i++) {
            for(int j = 0 ; j<arr.length; j++) {
            System.out.println(arr[x][y]);

            }
        }       
    }
}




Aucun commentaire:

Enregistrer un commentaire