示例#1
0
文件: Grid.cs 项目: creatayeats/CTP
 //Clears all pieces in a colour
 //This is the core functionality for the Ceullular Automata
 public void ClearColour(ColourPiece.ColourType colour)
 {
     for (int x = 0; x < xDim; x++)
     {
         for (int y = 0; y < yDim; y++)
         {
             //ColourType.ANY Represents the CA
             //It clears colours it is swapped with
             if (pieces[x, y].IsColoured() && (pieces[x, y].ColourComponent.Colour == colour ||
                                               colour == ColourPiece.ColourType.ANY))
             {
                 ClearPiece(x, y);
             }
         }
     }
 }
示例#2
0
文件: Grid.cs 项目: creatayeats/CTP
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColoured())
        {
            ColourPiece.ColourType colour = piece.ColourComponent.Colour;
            //Lists for collating matches
            List <GamePiece> horizontalPieces = new List <GamePiece>();
            List <GamePiece> verticalPieces   = new List <GamePiece>();
            List <GamePiece> matchingPieces   = new List <GamePiece>();

            //First check horizontal
            horizontalPieces.Add(piece);

            for (int dir = 0; dir <= 1; dir++)
            {
                for (int xOffset = 1; xOffset < xDim; xOffset++)
                {
                    int x;

                    if (dir == 0)
                    { //Left
                        x = newX - xOffset;
                    }
                    else
                    { //Right
                        x = newX + xOffset;
                    }

                    if (x < 0 || x >= xDim)
                    {
                        break;
                    }
                    //If pieces match, add them to the list
                    if (pieces[x, newY].IsColoured() && pieces[x, newY].ColourComponent.Colour == colour)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                //Add the hroizontal matching pieces, to the list for all matching pieces
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                    //Spawn a light in the corner of matching pieces
                    GameObject matchLight = (GameObject)Instantiate(matchingLight, horizontalPieces[i].transform.position, horizontalPieces[i].transform.rotation);
                }
            }

            //Traverse vertically if we found a match (for L and T shapes)
            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    for (int dir = 0; dir <= 1; dir++)
                    {
                        for (int yOffset = 1; yOffset < yDim; yOffset++)
                        {
                            int y;

                            if (dir == 0)
                            { //Up
                                y = newY - yOffset;
                            }
                            else
                            { //Down
                                y = newY + yOffset;
                            }

                            if (y < 0 || y >= yDim)
                            {
                                break;
                            }
                            //If pieces match, add them to the list
                            if (pieces[horizontalPieces[i].X, y].IsColoured() && pieces[horizontalPieces[i].X, y].ColourComponent.Colour == colour)
                            {
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    //Clear the list if there is no match of 3 or over
                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        //Otherwise add pieces to matching list
                        for (int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces[j]);
                        }

                        break;
                    }
                }
            }

            if (matchingPieces.Count >= 3)
            {
                return(matchingPieces);
            }

            //Didn't find anything going horizontally first,
            //so now check vertically
            horizontalPieces.Clear();
            verticalPieces.Clear();
            verticalPieces.Add(piece);

            for (int dir = 0; dir <= 1; dir++)
            {
                for (int yOffset = 1; yOffset < yDim; yOffset++)
                {
                    int y;

                    if (dir == 0)
                    { //Up
                        y = newY - yOffset;
                    }
                    else
                    { //Down
                        y = newY + yOffset;
                    }

                    if (y < 0 || y >= yDim)
                    {
                        break;
                    }
                    //If pieces match, add them to the list
                    if (pieces[newX, y].IsColoured() && pieces[newX, y].ColourComponent.Colour == colour)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                //Add the vertical matching pieces, to the list for all matching pieces
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces[i]);
                    //Spawn a light in the corner of matching pieces
                    GameObject matchLight = (GameObject)Instantiate(matchingLight, verticalPieces[i].transform.position, verticalPieces[i].transform.rotation);
                }
            }

            //Traverse horizontally if we found a match (for L and T shapes)
            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    for (int dir = 0; dir <= 1; dir++)
                    {
                        for (int xOffset = 1; xOffset < xDim; xOffset++)
                        {
                            int x;

                            if (dir == 0)
                            { //Left
                                x = newX - xOffset;
                            }
                            else
                            { //Right
                                x = newX + xOffset;
                            }

                            if (x < 0 || x >= xDim)
                            {
                                break;
                            }
                            //If pieces match, add them to the list
                            if (pieces[x, verticalPieces[i].Y].IsColoured() && pieces[x, verticalPieces[i].Y].ColourComponent.Colour == colour)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    //Clear the list if there is no match of 3 or over
                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        //Otherwise add pieces to matching list
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }

                        break;
                    }
                }
            }

            if (matchingPieces.Count >= 3)
            {
                return(matchingPieces);
            }
        }

        return(null);
    }