package com.provinggrounds.match3things.game;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import com.provinggrounds.match3things.util.Direction;
import com.provinggrounds.match3things.util.Orientation;


/*
 * 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;
    private static final int NUMBER_ELEMENTS_MATCHING_SET = 3;
    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 = getRandomGridPositions(NUM_MATCHING_SETS);

    	//
    	for(Coord currentPoint : matchingSetStartPoints ) {
    		//decide match set orientation - vertical/horizontal, up/down/left/right
    		//Direction matchSetDirection = getRandomMatchSetDirection(Orientation.getRandom());
    		//decide block type
    		//generate block coords and fill in grid
    	}

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

    /*
     * Determine which directions are valid (match set must stay within
     * grid boundary), given a starting point and Grid dimensions
     * and randomly pick a valid direction
     */
	private Direction getRandomMatchSetDirection(Coord matchSetStartingPoint) {

    private Direction[] getValidMatchSetDirections(Coord matchSetStartingPoint) {
    	Set<Direction> validDirectionSet = new HashSet<Direction>();
    	//check UP
		if(matchSetStartingPoint.y - 2 >= 0) validDirectionSet.add(Direction.UP);
    	//check DOWN
		if(matchSetStartingPoint.y + 2 <= height) validDirectionSet.add(Direction.DOWN);
    	//check LEFT
		if(matchSetStartingPoint.x - 2 >= 0) validDirectionSet.add(Direction.LEFT);
    	//check RIGHT
		if(matchSetStartingPoint.x + 2 <= width) validDirectionSet.add(Direction.RIGHT);
		return validDirectionSet.toArray(new Direction[validDirectionSet.size()]);
	}

    private Coord[] getRandomGridPositions(int numElements) {
    	Coord[] gridPositions = new Coord[numElements];
		for(Coord currentPoint : gridPositions) {
			currentPoint.x = randomNumberGenerator.nextInt(width);
			currentPoint.y = randomNumberGenerator.nextInt(height);
		}
		return gridPositions;
    }

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

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }
}

@@ -2313,16 +2313,217 @@
 Point) %7B
+%0A%09%09Direction%5B%5D validDirections = getValidMatchSetDirections(matchSetStartingPoint);%0A%09%09//return random valid direction%0A%09%09return validDirections%5BrandomNumberGenerator.nextInt(validDirections.length)%5D;%0A%09%7D
 %0A%0A    pr

Implement the getValidMatchSetDirections method