Hello.
Iam having a problem with my code that makes the Random() part.
This is a code for a memory-game in android, but this question is more pure java than android sdk.
This is the code that i run right now.
ROW_COUNT = 3 and COL_COUNT = 4 cards = new int [COL_COUNT] [ROW_COUNT];
private void loadCards(){ try{ int size = ROW_COUNT*COL_COUNT; ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0;i<size;i++){ list.add(new Integer(i)); } Random r = new Random(); for(int i=size-1;i>=0;i--){ int t=0; if(i>0){ t = r.nextInt(i); } t=list.remove(t).intValue(); cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2); } } catch (Exception e) { Log.e("loadCards()", e+""); } }
The problem is that this will always make cards[0][0] = 5 becourse iam not requesting a random
number when i==0
if i try to change that
toif(i>0){ t = r.nextInt(i); }
if(i>=0){ t = r.nextInt(i); }
This is wrong of course: r.nextInt(0) will throw IllegalArgumentException.
So my question is to you guys if there is a way round this problem of mine?
as you could see, iam new to this so please be gentle