示例#1
0
    private void destroyNeighbours(GameObject curr, Color color)
    {
        string name = curr.name;

        // get row and col of curr cell from curr name
        string string_row = name.Substring(name.IndexOf('[') + 1, name.Length - name.Substring(name.IndexOf(',')).Length - name.Substring(0, name.IndexOf('[') + 1).Length);

        int.TryParse(string_row, out int row);

        string string_col = name.Substring(name.IndexOf(',') + 1, name.Length - 1 - name.Substring(0, name.IndexOf(',') + 1).Length);

        int.TryParse(string_col, out int col);

        grid[row, col] = 1;

        // get neighbours
        int left_col  = col - 1;
        int right_col = col + 1;

        int up_row   = row - 1;
        int down_row = row + 1;

        string left  = "Square[" + string_row + "," + left_col.ToString() + "]";
        string right = "Square[" + string_row + "," + right_col.ToString() + "]";
        string up    = "Square[" + up_row.ToString() + "," + string_col + "]";
        string down  = "Square[" + down_row.ToString() + "," + string_col + "]";

        List <GameObject> neighbours = new List <GameObject>();

        neighbours = neighbourCell(left, row, left_col, neighbours);
        neighbours = neighbourCell(right, row, right_col, neighbours);
        neighbours = neighbourCell(up, up_row, col, neighbours);
        neighbours = neighbourCell(down, down_row, col, neighbours);

        int patchSize = 0;

        // if no neighbours
        if (neighbours.ToArray().Length == 0)
        {
            if (curr != start)
            {
                Destroy(curr);
                ScoringSystem.AddToScore(patchSize);
            }
            //           else if (curr == start)
            //               ScoringSystem.DeleteFromScore();
            else
            {
                if (curr == start)
                {
                    ScoringSystem.DeleteFromScore();
                }
                notSameColor.Clear();
            }

            return;
        }

        foreach (GameObject g in neighbours)
        {
            string_row = g.name.Substring(name.IndexOf('[') + 1, 1);
            int.TryParse(string_row, out row);

            string_col = g.name.Substring(name.IndexOf(',') + 2, 1);
            int.TryParse(string_col, out col);

            destroyNeighbours(g, color);

            if (grid[row, col] == 1)
            {
                Destroy(g);
                patchSize++;
            }
        }

        Destroy(curr);
        patchSize++;

        ScoringSystem.AddToScore(patchSize);
    }