package com.provinggrounds.match3things.game;

import java.util.Random;


/*
 * Represents a game grid. Contains a rectangular grid of numbers.
 * All numbers must be positive integers greater than 0, between 1 and numObjectTypes(both inclusive)
 */
public class Grid {
    private static final int NUM_MATCHING_SETS = 4;
    int width;
    int height;
    /*
     * Represents number of unique object types
     */
    int numObjectTypes;
    Integer[] gameGrid;
    static final Random randomNumberGenerator = new Random();

    /*
     * Width and height must always be provided to Grid - should not be constructed without initializing/passing in those values
     */
    private Grid() {
    }

    public Grid(int width, int height, int numObjectTypes) {
	this.width = width;
	this.height = height;
	this.numObjectTypes = numObjectTypes;

	initGameGridArray();
	fillGameGrid();
    }

    /*
     * Creates grid, allocates storage
     */
    private void initGameGridArray() {
	gameGrid = new Integer[height*width];
    }

    /*
     * Initialize/fill game grid with random objects
     */
    private void fillGameGrid() {
		//create matching sets first, then fill in remaining elements with random numbers

		//pick 4 random positions
		Coord[] matchingSetStartPoints = new Coord[NUM_MATCHING_SETS];
		for(Coord currentPoint : matchingSetStartPoints) {
			currentPoint.x = randomNumberGenerator.nextInt(width);
			currentPoint.y = randomNumberGenerator.nextInt(height);
		}

		for(int counter = 0; counter < gameGrid.length; counter++) {
			if(gameGrid[counter] == null)
				gameGrid[counter] = randomNumberGenerator.nextInt(numObjectTypes) + 1;
		}
    }

    public Integer[] getGameGrid() {
        return gameGrid;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
}

@@ -1237,17 +1237,20 @@
 sitions%0A
-%09
+    
 %09Coord%5B%5D
@@ -1279,18 +1279,31 @@
 s = 
-new Coord%5B
+getRandomGridPositions(
 NUM_
@@ -1319,81 +1319,129 @@
 SETS
-%5D;
+);%0A
 %0A%09%09for(
-Coord currentPoint : matchingSetStartPoints) %7B%0A%09%09%09currentPoint.x
+int counter = 0; counter %3C gameGrid.length; counter++) %7B%0A%09%09%09if(gameGrid%5Bcounter%5D == null)%0A%09%09%09%09gameGrid%5Bcounter%5D
  = r
@@ -1473,254 +1473,343 @@
 Int(
-width)
+numObjectTypes) + 1
 ;%0A%09%09
-%09
-currentPoint.y = randomNumberGenerator.nextInt(height);%0A%09%09%7D%0A%0A%09%09for(int counter = 0; counter %3C gameGrid.length; counter++) %7B%0A%09%09%09if(gameGrid%5Bcounter%5D == null)%0A%09%09%09%09gameGrid%5Bcounter%5D = randomNumberGenerator.nextInt(numObjectTypes) + 1;%0A%09%09%7D
+%7D%0A    %7D%0A%0A    private Coord%5B%5D getRandomGridPositions(int numElements) %7B%0A    %09Coord%5B%5D gridPositions = new Coord%5BnumElements%5D;%0A%09%09for(Coord currentPoint : gridPositions) %7B%0A%09%09%09currentPoint.x = randomNumberGenerator.nextInt(width);%0A%09%09%09currentPoint.y = randomNumberGenerator.nextInt(height);%0A%09%09%7D%0A%09%09return gridPositions;
 %0A   

Refactor code to pick random grid positions into it's own method