Initialize() public method

Initializes the tile with new data.
public Initialize ( int diamonds, int xCoord, int yCoord, LevelScript, inLevel ) : void
diamonds int Number of diamonds in the tile.
xCoord int x-position in the grid for the tile.
yCoord int y-position in the grid for the tile.
inLevel LevelScript, Reference to the level the tile is in.
return void
Ejemplo n.º 1
0
    private void CreateBoard(float xTileSize, float yTileSize)
    {
        tiles = new GameObject[xSize, ySize];

        float startX = transform.position.x;
        float startY = transform.position.y;



        //generate our tiles
        for (int x = 0; x < xSize; x++)
        {
            for (int y = 0; y < ySize; y++)
            {
                GameObject newTile = Instantiate(tile, new Vector3(startX + (xTileSize * x), startY + (yTileSize * y), 0), tile.transform.rotation);
                newTile.transform.localScale = new Vector3(TileScale.x, TileScale.y, 1);
                tiles[x, y] = newTile;
                newTile.transform.parent = transform;
            }
        }

        //ensure there is at least one solution
        GenerateSolution();

        //fill out remaining tiles
        for (int x = 0; x < xSize; x++)
        {
            for (int y = 0; y < ySize; y++)
            {
                TileScript Tile = tiles[x, y].GetComponentInChildren <TileScript>();
                if (!Tile.HasContent())
                {
                    //populate empty tiles with a random option
                    Tile.Initialize(Options[Random.Range(0, Options.Count)], x, ySize - 1 - y, this);
                }
            }
        }

        SetHorizontalBoxPosition(0);
        SetVerticalBoxPosition(-1);
    }
Ejemplo n.º 2
0
    private void GenerateSolution()
    {
        int  prevRow     = -1;
        int  prevCol     = -1;
        bool validChoice = false;

        int  startPoint = 0;
        bool offset     = false;
        int  startCol   = 0;

        //set the start point for possible mid way
        //have more room in buffer than answers
        if (NumAnswerComponents < BufferSize)
        {
            int difference = BufferSize - NumAnswerComponents;
            //make the difference inclusive so add 1
            startPoint = Random.Range(0, difference + 1);
            if (startPoint > 0)
            {
                //prevent from starting in our start
                prevRow = Random.Range(1, ySize);
                prevCol = Random.Range(0, xSize);
                if (startPoint % 2 == 1)
                {
                    offset   = true;
                    startCol = prevCol;
                }
            }
        }


        for (int i = startPoint; i < NumAnswerComponents + startPoint; i++)
        {
            validChoice = false;
            if (i % 2 == 0) //even number so a row
            {
                while (!validChoice)
                {
                    int        selection = Random.Range(0, xSize);
                    TileScript Tile      = tiles[selection, (prevRow < 0 ? ySize - 1 : prevRow)].GetComponentInChildren <TileScript>();
                    if (!(offset && prevRow == ySize - 1 && selection == startCol)) //if we did an offset want to reserve the starting square to not be used
                    {
                        if (!Tile.HasContent())
                        {
                            Tile.Initialize(AnswerKey[i - startPoint], selection, (prevRow < 0 ? 0 : ySize - 1 - prevRow), this);
                            validChoice = true;
                            prevCol     = selection;
                        }
                    }
                    else
                    {
                        Debug.Log("Prevented impossible");
                    }
                }
            }
            else //  odd number so in a column
            {
                while (!validChoice)
                {
                    int        selection = Random.Range(0, ySize);
                    TileScript Tile      = tiles[(prevCol < 0 ? xSize - 1 : prevCol), selection].GetComponentInChildren <TileScript>();
                    if (!(offset && selection == ySize - 1 && prevCol == startCol))   //if we did an offset want to reserve the starting square to not be used
                    {
                        if (!Tile.HasContent())
                        {
                            Tile.Initialize(AnswerKey[i - startPoint], (prevCol < 0 ? xSize - 1 : prevCol), ySize - 1 - selection, this);
                            validChoice = true;
                            prevRow     = selection;
                        }
                    }
                    else
                    {
                        Debug.Log("Prevented impossible");
                    }
                }
            }
        }
    }