samedi 3 février 2018

Generating Ships

I am trying to create a function that will randomly choose places for computer ships but it can't place them on the same spot or on another player ships.

for board I have a int[5][5] board where 0 means a place is empty 1 is for player ship and 2 is for computer ship. I wrote this but it keeps freezing and crashing android studio.

    boolean wrong = true;
    for(int i = 0; i < 5; i++){

        while (wrong)
        {
            Random rand = new Random();
            int x = rand.nextInt(4);
            int y = rand.nextInt(4);

            if(board[x][y] == 1 || board[x][y] == 2){
                wrong = true;
            }

            else {
                board[x][y] = 2;
                wrong = false;
            }
        }




Aucun commentaire:

Enregistrer un commentaire