I’m making the memory game concentration.
In this I have a specified obligation to generate random pairs of numbers.
These are to be assigned to a cell at its creation on a game board.
My problem lies not in how to generate a random number as I am able to do this with —
type = generator.nextInt(NUMBER_OF_ROWS*NUMBER_OF_COLUMNS);
However I must make sure that when I am populating my game board with Cell objects
board[row][column] = new Cell(this,type, row, column);
I have 2 cells of the same type, in order for the game to function.
Basically
I need 12 pairs of numbers that have been randomly generated.
Thanks
Varun
Invert the problem. Rather than think of generating 24 random numbers, what you really want is to distribute the numbers 1-12 randomly into a 24-cell array, with each number going into TWO randomly selected cells in the array. The simplest way to do this would be be to create a
List<Cell>, populate it with 24 cells (two containing value 1, two containing value 2, etc), and then useCollections.shuffle(List<?>...)