mardi 26 janvier 2016

Generate locations which are touching in a 2d array

Problem: I want to generate random locations on a grid which are touching. The total number of locations is 5. Is there a more efficient/different way of doing the following code (I call this 5 times with the use of recursion and use other code which stores it):

 /*
 * 8        1       2
 * 7    [original]  3
 * 6        5       4
 */
    int rand = 1 + (int)(Math.random() * ((8 - 1) + 1));
    if(rand >= 2 && rand<= 4)
    {
        newx++;
    }
    else if(rand >=6 && rand<=8)
    {
        newx--;
    }

    //change y according to number
    if(rand == 8 || rand == 1 || rand==2)
    {
        newy++;
    }
    else if(rand >= 4 && rand<= 6 )
    {
        newy--;
    }




Aucun commentaire:

Enregistrer un commentaire