public void SpawnRandomObject()
 {
     if (prefabCounter <= 25)
     {
         GridController    gridController        = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridController>();
         GameObject        randomPos             = gridController.returnRandomObjectPos();
         GridPosController randGridPosController = randomPos.GetComponent <GridPosController>();
         int posX = randGridPosController.positionX;
         int posY = randGridPosController.positionY;
         spawnSquareAtLocation(arrayOfPrefabs[prefabCounter], posX, posY);
         //Debug.Log("PrefabCounter" + prefabCounter);
         prefabCounter++;
     }
 }
    public GameObject returnRandomObjectPos()
    {
        int randomArrayLength = 0; //changed from 0 for a test

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                GameObject        objectpos         = gridArray[x][y];
                GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                if (gridPosController.currentSquare == null)
                {
                    randomArrayLength++;
                    Debug.Log(x + "," + y + " is empty");
                }
            }
        }
        if (randomArrayLength > 0)
        {
            GameObject[] randomArray = new GameObject[randomArrayLength];
            int          i           = 0;
            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    GameObject        objectpos         = gridArray[x][y];
                    GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                    //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                    if (gridPosController.currentSquare == null)
                    {
                        randomArray[i] = objectpos;
                        i++;
                    }
                }
            }
            int               randomArrayPos          = Random.Range(0, randomArrayLength - 1);
            GameObject        randomObject            = randomArray[randomArrayPos];
            GridPosController randomGridPosController = randomObject.GetComponent <GridPosController>();
            //Debug.Log("Random Object Pos" + randomGridPosController.positionX + "," + randomGridPosController.positionY);
            return(randomObject);
        }
        else
        {
            return(null);
        }
    }
    public GameObject[] returnIncorrectPositions()
    {
        int numIncorrect = 0;

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                GameObject        objectpos         = gridArray[x][y];
                GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                if (gridPosController.currentSquare != null)
                {
                    SquareController squareController = gridPosController.currentSquare.GetComponent <SquareController>();
                    if (!squareController.IsPositionCorrect())
                    {
                        numIncorrect++;
                    }
                }
            }
        }
        GameObject[] incorrectArray = new GameObject[numIncorrect];
        int          i = 0;

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                GameObject        objectpos         = gridArray[x][y];
                GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                if (gridPosController.currentSquare != null)
                {
                    SquareController squareController = gridPosController.currentSquare.GetComponent <SquareController>();
                    if (!squareController.IsPositionCorrect())
                    {
                        incorrectArray[i] = objectpos;
                        i++;
                    }
                }
            }
        }
        Debug.Log("Number incorrect:" + numIncorrect);
        return(incorrectArray);
    }
    public void ShuffleObjects()
    {
        int numberOfSquares = 0;

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                GameObject        objectpos         = gridArray[x][y];
                GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                if (gridPosController.currentSquare != null)
                {
                    numberOfSquares++;
                }
            }
        }
        GameObject[] ArrayOfSquares = new GameObject[numberOfSquares];
        int          i = 0;

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                GameObject        objectpos         = gridArray[x][y];
                GridPosController gridPosController = objectpos.GetComponent <GridPosController>();
                //Debug.Log("Position:" + gridPosController.positionX + "," + gridPosController.positionY);
                if (gridPosController.currentSquare != null)
                {
                    ArrayOfSquares[i] = gridPosController.currentSquare;
                    i++;
                }
            }
        }

        for (int n = 0; n < i; n++)
        {
            GameObject        objectPos         = returnRandomObjectPos();
            GridPosController gridPosController = objectPos.GetComponent <GridPosController>();
            SquareController  squareController  = ArrayOfSquares[n].GetComponent <SquareController>();
            squareController.MoveTo(gridPosController.positionX, gridPosController.positionY);
        }
    }
    public void MoveTo(int posX, int posY)
    {
        //Debug.Log("currentPosX at time of move:" + currentPosX + " CurrentPosY at time of move:" + currentPosY);
        if (currentPosX != -1 || currentPosY != -1)
        {
            //Debug.Log("section added to deal with old position called");
            GridController    OldGridController    = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridController>();
            GameObject        OldGridPos           = OldGridController.getPosObject(currentPosX, currentPosY);
            GridPosController OldGridPosController = OldGridPos.GetComponent <GridPosController>();
            OldGridPosController.setCurrentSquare(null);
        }


        currentPosX = posX;
        currentPosY = posY;
        GridController gridController = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridController>();
        GameObject     gridPos        = gridController.getPosObject(posX, posY);

        gameObject.transform.position = gridPos.transform.position; // move the object to the specified position on the grid
        gameObject.transform.position = new Vector3(transform.position.x, transform.position.y, -1);
        GridPosController gridPosController = gridPos.GetComponent <GridPosController>();

        gridPosController.setCurrentSquare(gameObject);
    }