Example #1
0
    public bool ClearAllValidMatches()
    {
        bool needsRefill = false;

        for (int y = 0; y < yDim; y++)
        {
            for (int x = 0; x < xDim; x++)
            {
                if (pieces[x, y].IsClearable())
                {
                    List <GamePiece> match = GetMatch(pieces[x, y], x, y);

                    if (match != null)
                    {
                        PieceType            specialPieceType = PieceType.NORMAL;
                        GamePiece            randomPiece      = match[Random.Range(0, match.Count)];
                        int                  specialPieceX    = randomPiece.X;
                        int                  specialPieceY    = randomPiece.Y;
                        ColorPiece.ColorType colortype        = ColorPiece.ColorType.COUNT;

                        for (int i = 0; i < match.Count; i++)
                        {
                            ColorPiece.ColorType temp = match[i].ColorComponent.Color;

                            if (ClearPiece(match[i].X, match[i].Y))
                            {
                                needsRefill = true;
                                colortype   = temp;

                                if (match[i] == pressedPiece || match[i] == enteredPiece)
                                {
                                    specialPieceX = match[i].X;
                                    specialPieceY = match[i].Y;
                                    colortype     = temp;
                                }
                            }
                        }

                        // 最高等级,直接拿出,并展示动画
                        if (colortype == ColorPiece.ColorType.COUNT - 2)
                        {
                            level.OnPieceClicked(examplepiece);
                        }
                        // 不是最高等级,进行升级
                        else
                        {
                            Destroy(pieces[specialPieceX, specialPieceY]);
                            GamePiece newPiece = SpawnNewPiece(specialPieceX, specialPieceY, specialPieceType);
                            newPiece.ColorComponent.SetColor(colortype + 1);
                            newPiece.GenerativeComponent.Generate(fillTime, fillTime);
                        };
                    }
                }
            }
        }

        return(needsRefill);
    }
Example #2
0
    private List <GamePiece> GetPiecesOfColor(ColorPiece.ColorType pieceColor)
    {
        var sameColor = from GamePiece p in pieces
                        where p.Type != PieceType.EMPTY &&
                        p.ColorComponent.Color == pieceColor &&
                        p != pressedPiece
                        select p;

        return(sameColor.ToList());
    }
Example #3
0
 public void ClearColor(ColorPiece.ColorType color)
 {
     for (int x = 0; x < totalRows; x++)
     {
         for (int y = 0; y < collumnHeight; y++)
         {
             if (pieces[x, y].IsColored() && (pieces[x, y].ColorComponent.Color == color || color == ColorPiece.ColorType.ANY))
             {
                 ClearPiece(x, y);
             }
         }
     }
 }
Example #4
0
 public void ClearColor(ColorPiece.ColorType color)
 {
     for (int x = 0; x < xDim; x++)
     {
         for (int y = 0; y < yDim; y++)
         {
             if ((pieces[x, y].IsColored() && pieces[x, y].ColorComponent.Color == color) || color == ColorPiece.ColorType.ANY)
             {
                 ClearPiece(x, y);
             }
         }
     }
 }
Example #5
0
 internal void ClearColor(ColorPiece.ColorType color)
 {
     for (int x = 0; x < maxWidth; x++)
     {
         for (int y = 0; y < height; y++)
         {
             if (pieces[x, y].IsColored() && (pieces[x, y].ColorComponent.Color == color))
             {
                 ClearPiece(x, y);
             }
         }
     }
 }
Example #6
0
 public void ClearColor(ColorPiece.ColorType color) //查找所有的同色物体将之清除,当两个物体均为any时,全部清除
 {
     for (int x = 0; x < xDim; x++)
     {
         for (int y = 0; y < yDim; y++)
         {
             if (pieces[x, y].IsColored() && (pieces[x, y].ColorComponent.Color == color || color == ColorPiece.ColorType.ANY))
             {
                 ClearPiece(x, y);
             }
         }
     }
 }
Example #7
0
 public void StartingDragging(GamePiece piece)
 {
     if (gameOver)
     {
         return;
     }
     if (!selecting)
     {
         selectedPieces = new Stack <GamePiece>();
         selecting      = true;
         selectedColor  = piece.ColorComponent.Color;
         SelectPiece(piece);
     }
 }
Example #8
0
 //清除所有同色
 public void ClearColor(ColorPiece.ColorType color)
 {
     for (int x = 0; x < xdim; x++)
     {
         for (int y = 0; y < ydim; y++)
         {
             if (gamePieces[x, y] != null && gamePieces[x, y].isColor &&
                 gamePieces[x, y].isClear)
             {
                 if (gamePieces[x, y].ColorComponent.ColorTypeRef == color ||
                     color == ColorPiece.ColorType.ANY)   //相同颜色,或者交换两个彩虹清除所有颜色
                 {
                     ClearPiece(x, y);
                 }
             }
         }
     }
 }
Example #9
0
    public void Spawnitem(List <GamePiece> MatchingItems)
    {
        GamePiece needed = MatchingItems[1];

        ColorPiece.ColorType color = needed.ColorComponent.Color;
        int coolor = (int)color;

        switch (coolor)
        {
        case 0:
            GameObject newItem = Instantiate(items[2], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        case 1:
            GameObject bone = Instantiate(items[0], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        case 2:
            GameObject bottle = Instantiate(items[1], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        case 3:
            GameObject spring = Instantiate(items[4], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        case 4:
            GameObject scroll = Instantiate(items[3], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        case 5:
            GameObject grymuar = Instantiate(items[5], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;

        default:
            GameObject cos = Instantiate(items[5], new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
            break;
        }

        //GameObject newItem = Instantiate(items, new Vector3(Random.Range(4, 10), 6, 0), Quaternion.identity);
        BackPack.instance.ItemCount++;
    }
Example #10
0
    private List <GamePiece> GetMatchOthersInLine(GamePiece piece, int newX, int newY, int dir)
    {
        ColorPiece.ColorType color     = piece.ColorComponent.Color;
        List <GamePiece>     colPieces = new List <GamePiece>();

        int[] dx = new int[] { 1, -1, 0, 0 };
        int[] dy = new int[] { 0, 0, 1, -1 };

        for (int i = dir * 2; i < 4 - (1 - dir) * 2; i++)
        {
            int tx = newX;
            int ty = newY;
            while (true)
            {
                tx += dx[i];
                ty += dy[i];
                if (tx < 0 || tx >= xDim)
                {
                    break;
                }
                if (ty < 0 || ty >= yDim)
                {
                    break;
                }
                if (tx == piece.X && ty == piece.Y)
                {
                    break;
                }
                if (!pieces[tx, ty].IsColored() || pieces[tx, ty].ColorComponent.Color != color)
                {
                    break;
                }
                colPieces.Add(pieces[tx, ty]);
            }
        }

        return(colPieces);
    }
Example #11
0
    /// <summary>
    /// FOR TEST/STUDY ONLY. PLEASE IGNORE IT!!
    /// </summary>
    //void GetData()
    //{
    //    int index = 0;
    //    foreach (var p in pieces)
    //    {
    //        piecesData[index] = new PieceData
    //        {
    //            x = p.X,
    //            y = p.Y,
    //            normal = p.Type == PieceType.NORMAL ? true : false
    //        };
    //        allPiecesTransform[index] = p.transform;
    //        index++;
    //    }
    //    transformAccessArray = new TransformAccessArray(allPiecesTransform);
    //}

#if UNITY_EDITOR
    //Only for tests and debug
    private void Update()
    {
        // Q# Restart scene
        if (Input.GetKeyDown(KeyCode.Q))
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene(1);
        }

        // P# Creates new column on Right side and push all current columns to left (until limit)
        if (Input.GetKeyDown(KeyCode.P))
        {
            PushColumns();
        }

        // R# Removes entire random row
        if (Input.GetKeyDown(KeyCode.R))
        {
            ClearRow(0);
            StartCoroutine(SnapPieces());
        }

        // C# Removes entire column on left side
        if (Input.GetKeyDown(KeyCode.C))
        {
            ClearColumn(Random.Range(0, curWidth - 1));
            StartCoroutine(SnapPieces());
        }

        // K# Removes all pieces of a random color type
        if (Input.GetKeyDown(KeyCode.K))
        {
            int cInt = Random.Range(0, 6);
            ColorPiece.ColorType cType = (ColorPiece.ColorType)cInt;
            ClearColor(cType);
            Debug.Log($"Removing elements with color: {cType}");
            StartCoroutine(SnapPieces());
        }
    }
Example #12
0
    public List <Piece> GetMatch(Piece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <Piece>         horizontalPieces = new List <Piece>();
            List <Piece>         verticalPieces   = new List <Piece>();
            List <Piece>         matchingPieces   = new List <Piece>();

            //First check horizontall
            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 [x, newY].IsColored() && pieces [x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces [x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces [i]);
                }
            }

            // 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 [horizontalPieces [i].X, y].IsColored() && pieces [horizontalPieces [i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces [horizontalPieces [i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        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 [newX, y].IsColored() && pieces [newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces [newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces [i]);
                }
            }

            // 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 [x, verticalPieces[i].Y].IsColored() && pieces [x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces [x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces [j]);
                        }

                        break;
                    }
                }
            }

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

        return(null);
    }
Example #13
0
    //.......................................................直線配對功能,l型配對功能
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece> ();
            List <GamePiece>     verticalPieces   = new List <GamePiece> ();
            List <GamePiece>     matchingPieces   = new List <GamePiece> ();

            //水平
            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 [x, newY].IsColored() && pieces [x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces [x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces [i]);
                }
            }
            //...........................................水平l型
            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 [horizontalPieces [i].X, y].IsColored() && pieces [horizontalPieces [i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces [horizontalPieces [i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces [j]);
                        }
                        break;
                    }
                }
            }
            //****************************
            if (matchingPieces.Count >= 3)
            {
                return(matchingPieces);
            }



            //垂直
            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 [newX, y].IsColored() && pieces [newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces [newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces [i]);
                }
            }

            //...........................................垂直l型
            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 [x, verticalPieces [i].Y].IsColored() && pieces [x, verticalPieces [i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces [x, verticalPieces [i].Y]);                                         ////這個是自己改的,如果之後執行有誤改他式式,原本是 verticalPieces.Add (pieces [x,verticalPieces [i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces [j]);
                        }
                        break;
                    }
                }
            }
            //************************

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

        return(null);
    }
Example #14
0
    //清除全局
    bool ClearAllMatch()
    {
        bool needRefill = false;

        //遍历每一个Piece
        for (int i = 0; i < xdim; i++)
        {
            for (int j = 0; j < ydim; j++)
            {
                if (gamePieces[i, j] != null && gamePieces[i, j].Type == PieceType.NORMOL &&
                    gamePieces[i, j].isClear && !gamePieces[i, j].ClearComponent.isBeingClear)
                {
                    //以各个piece为基点查找匹配
                    List <GamePiece> matchPieces = GetMatch(gamePieces[i, j], gamePieces[i, j].X, gamePieces[i, j].Y);
                    if (matchPieces != null)    //有配对
                    {
                        //特殊元素默认为系统出现的坐标
                        int middle        = matchPieces.Count / 2;
                        int specialPieceX = matchPieces[middle].X;
                        int specialPieceY = matchPieces[middle].Y;
                        ColorPiece.ColorType specialPieceColor = matchPieces[middle].ColorComponent.ColorTypeRef;
                        PieceType            specialPieceType  = (PieceType)Random.Range((int)PieceType.ROW_CLEAR, (int)PieceType.COLUMN_CLEAR);

                        bool isNeedSpecial = false;//是否需要生成特殊元素
                        if (matchPieces.Count == 4)
                        {
                            // Debug.Log("match 4");
                            isNeedSpecial = true;
                            //用户交换出现的T|L
                            if (pieceA != null && pieceB != null)
                            {
                                //Debug.Log("user cause");
                                //横纵确认
                                if (pieceA.X == pieceB.X)
                                {
                                    specialPieceType = PieceType.ROW_CLEAR;
                                }
                                else
                                {
                                    specialPieceType = PieceType.COLUMN_CLEAR;
                                }
                                //坐标确认
                                if (pieceA.Type == matchPieces[0].Type)
                                {
                                    specialPieceX = pieceA.X;
                                    specialPieceY = pieceA.Y;
                                }
                                else
                                {
                                    specialPieceX = pieceB.X;
                                    specialPieceY = pieceB.Y;
                                }
                            }
                        }
                        else if (matchPieces.Count >= 5)
                        {
                            //Debug.Log("match 5");
                            isNeedSpecial = true;
                            //用户交换出现的大于5
                            if (pieceA != null && pieceB != null)
                            {
                                //Debug.Log("user cause");
                                specialPieceType  = PieceType.COLOR_CLEAR;
                                specialPieceColor = ColorPiece.ColorType.ANY;   //彩色
                                //坐标确认
                                if (pieceA.Type == matchPieces[0].Type)
                                {
                                    specialPieceX = pieceA.X;
                                    specialPieceY = pieceA.Y;
                                }
                                else
                                {
                                    specialPieceX = pieceB.X;
                                    specialPieceY = pieceB.Y;
                                }
                            }
                        }
                        //清除
                        ClearPieces(matchPieces);

                        //生成特殊元素
                        if (isNeedSpecial)
                        {
                            gamePieces[specialPieceX, specialPieceY] = SpawnNewPiece(specialPieceX, specialPieceY, specialPieceType);
                            gamePieces[specialPieceX, specialPieceY].ColorComponent.setColor(specialPieceColor);
                        }

                        needRefill = true;  //需要重新填充
                    }
                }
            }
        }
        return(needRefill);
    }
Example #15
0
    //takes in a gamePiece and the new position it is moving to, return a list of gamePieces that are part of a match
    //A match is 3 or more colours that match in a row or in a L or T shape
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece>();
            List <GamePiece>     verticalPieces   = new List <GamePiece>();
            List <GamePiece>     matchingPieces   = new List <GamePiece>();

            //Traverse horrizontally and then vertically, each adjacent piece we find that is the same colour will get added to the array of the potential pieces
            //we will stop when we reach a different colour, if we have enough pieces to form a match they will get added to the matchingPieces list
            // First check horizontal
            horizontalPieces.Add(piece);

            //if dir = 0 we go one way, if dir = 1 we go the other way
            for (int dir = 0; dir <= 1; dir++)
            {
                //xOffset is how far away our adjacent piece is from our central piece
                for (int xOffset = 1; xOffset < xDim; xOffset++)
                {
                    int x;

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

                    //if it goes out of bounds
                    if (x < 0 || x >= xDim)
                    {
                        break;
                    }

                    //we check if the piece is coloured and if the piece is the same colour as our current piece
                    //y coordinate is the same and the x coordinate is the one we just calculated
                    if (pieces[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                }
            }

            // 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[horizontalPieces[i].X, y].IsColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        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);

            //if dir = 0 we go one way, if dir = 1 we go the other way
            for (int dir = 0; dir <= 1; dir++)
            {
                for (int yOffset = 1; yOffset < yDim; yOffset++)
                {
                    int y;

                    //yOffset is how far away our adjacent piece is from our central piece
                    if (dir == 0)
                    { //Going Up
                        y = newY - yOffset;
                    }
                    else
                    { //Going Down
                        y = newY + yOffset;
                    }

                    //If it goes out of bounds of the board
                    if (y < 0 || y >= yDim)
                    {
                        break;
                    }

                    //we check if the piece is coloured and if the piece is the same colour as our current piece
                    //x coordinate is the same and the y coordinate is the one we just calculated
                    if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces[i]);
                }
            }

            // 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[x, verticalPieces[i].Y].IsColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }

                        break;
                    }
                }
            }

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

        return(null);
    }
Example #16
0
    public List <GamePiece> findMatches(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color = piece.ColorComponent.Color;

            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 searchDirection = 0; searchDirection <= 1; searchDirection++)
            {
                for (int xOffset = 1; xOffset < xDim; xOffset++)
                {
                    int x;

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

                    if (x < 0 || x >= xDim)
                    {
                        break;
                    }

                    if (pieces [x, newY].IsColored() && pieces [x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces [x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces [i]);
                }
            }


            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 searchDirection = 0; searchDirection <= 1; searchDirection++)
            {
                for (int yOffset = 1; yOffset < yDim; yOffset++)
                {
                    int y;

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

                    if (y < 0 || y >= yDim)
                    {
                        break;
                    }

                    if (pieces [newX, y].IsColored() && pieces [newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces [newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces [i]);
                }
            }

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

        return(null);
    }
Example #17
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.isColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece>();
            List <GamePiece>     verticalPieces   = new List <GamePiece>();
            List <GamePiece>     matchingPieces   = new List <GamePiece>();
            // 先检测水平的
            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[x, newY].isColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                }
            }
            // 对于L或者T形的我们需要再找对应的连接的
            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[horizontalPieces[i].X, y].isColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces[j]);
                        }
                        break;
                    }
                }
            }

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

            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[newX, y].isColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces[i]);
                }
            }
            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[x, verticalPieces[i].Y].isColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }
                        break;
                    }
                }
            }
            if (matchingPieces.Count >= 3)
            {
                return(matchingPieces);
            }
        }
        return(null);
    }
Example #18
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece>();
            List <GamePiece>     verticalPieces   = new List <GamePiece>();
            List <GamePiece>     matchingPieces   = new List <GamePiece>();


            //Checking for horizontal matches
            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[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                }
            }

            // Traverse vertically if we found a match (for L and T shape)
            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.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[horizontalPieces[i].X, y].IsColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                if (verticalPieces.Count < 2)
                {
                    verticalPieces.Clear();
                }
                else
                {
                    for (int j = 0; j < verticalPieces.Count; j++)
                    {
                        matchingPieces.Add(verticalPieces[j]);
                    }
                    //break;
                }
            }

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


            //Checking for vertial matches
            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[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces[i]);
                }
            }

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

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

                            if (x < 0 || x >= xDim)
                            {
                                break;
                            }

                            if (pieces[x, verticalPieces[i].Y].IsColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                if (horizontalPieces.Count < 2)
                {
                    horizontalPieces.Clear();
                }
                else
                {
                    for (int j = 0; j < horizontalPieces.Count; j++)
                    {
                        matchingPieces.Add(horizontalPieces[j]);
                    }
                    //break;
                }
            }

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

        return(null);
    }
Example #19
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            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 [x, newY].IsColored() && pieces [x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces [x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces [i]);
                }
            }


            // 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 [horizontalPieces [i].X, y].IsColored () && pieces [horizontalPieces [i].X, y].ColorComponent.Color == color) {
            //					verticalPieces.Add (pieces [horizontalPieces [i].X, y]);
            //				} else {
            //					break;
            //				}
            //			}
            //		}

            //		if (horizontalPieces.Count < 2) {
            //			horizontalPieces.Clear ();
            //		} else {
            //			for (int j = 0; j < horizontalPieces.Count; j++) {
            //				matchingPieces.Add (horizontalPieces[j]);
            //			}

            //			break;
            //		}
            //	}
            //}

            //if (matchingPieces.Count >= 5)
            //{
            //    Spawnitem(matchingPieces);
            //    Spawnitem(matchingPieces);
            //    Spawnitem(matchingPieces);
            //    AudioSource audiosouce = this.gameObject.GetComponent<AudioSource>();
            //    audiosouce.Play();
            //    return matchingPieces;
            //}
            //else if (matchingPieces.Count == 4)
            //{
            //    Spawnitem(matchingPieces);
            //    Spawnitem(matchingPieces);
            //    AudioSource audiosouce = this.gameObject.GetComponent<AudioSource>();
            //    audiosouce.Play();
            //    return matchingPieces;
            //}
            //else if (matchingPieces.Count == 3)
            //{
            //    Spawnitem(matchingPieces);
            //    AudioSource audiosouce = this.gameObject.GetComponent<AudioSource>();
            //    audiosouce.Play();
            //    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 [newX, y].IsColored() && pieces [newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces [newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces [i]);
                }
            }

            //// 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 [x, verticalPieces[i].Y].IsColored () && pieces [x, verticalPieces[i].Y].ColorComponent.Color == color) {
            //					horizontalPieces.Add (pieces [x, verticalPieces[i].Y]);
            //				} else {
            //					break;
            //				}
            //			}
            //		}

            //		if (horizontalPieces.Count < 2) {
            //			horizontalPieces.Clear ();
            //		} else {
            //			for (int j = 0; j < horizontalPieces.Count; j++) {
            //				matchingPieces.Add (horizontalPieces [j]);
            //			}

            //			break;
            //		}
            //	}
            //}

            if (matchingPieces.Count >= 5)
            {
                Spawnitem(matchingPieces);
                Spawnitem(matchingPieces);
                Spawnitem(matchingPieces);
                AudioSource audiosouce = this.gameObject.GetComponent <AudioSource>();
                audiosouce.Play();
                return(matchingPieces);
            }
            else if (matchingPieces.Count == 4)
            {
                Spawnitem(matchingPieces);
                Spawnitem(matchingPieces);
                AudioSource audiosouce = this.gameObject.GetComponent <AudioSource>();
                audiosouce.Play();
                return(matchingPieces);
            }
            else if (matchingPieces.Count == 3)
            {
                Spawnitem(matchingPieces);
                AudioSource audiosouce = this.gameObject.GetComponent <AudioSource>();
                audiosouce.Play();
                return(matchingPieces);
            }
        }

        return(null);
    }
Example #20
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            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 [x, newY].IsColored() && pieces [x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces [x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces [i]);
                }
            }

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

            // Second check vertical
            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 [newX, y].IsColored() && pieces [newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces [newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces [i]);
                }
            }

            if (matchingPieces.Count >= 3)
            {
                return(matchingPieces);
            }
        }
        return(null);
    }
Example #21
0
    //检测交换后是否有3个连子确定是否能交换
    List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        ColorPiece.ColorType color           = piece.ColorComponent.ColorTypeRef; //颜色
        List <GamePiece>     horizontalPiece = new List <GamePiece>();            //保存临时竖直方向的匹配
        List <GamePiece>     verticalPiece   = new List <GamePiece>();            //保存横向临时的匹配
        List <GamePiece>     matchPieces     = new List <GamePiece>();            //保存最终匹配结果

        //横向检测
        horizontalPiece.Add(piece);
        //先检测左边,再检测右边
        for (int dir = -1; dir <= 1; dir += 2)
        {
            //邻近是否与自身连子
            for (int index = 1; index <= xdim; index++)
            {
                int x = newX + index * dir;
                if (x < 0 || x >= xdim)  //防止越界
                {
                    break;
                }
                //Debug.Log("横向:" + x + " " + newY);
                if (gamePieces[x, newY] != null && gamePieces[x, newY].isColor)
                {
                    if (gamePieces[x, newY].ColorComponent.ColorTypeRef == color)
                    {
                        horizontalPiece.Add(gamePieces[x, newY]);
                        continue;//发现匹配可以继续
                    }
                }

                break;//无匹配结束
            }
        }
        //有连子,检测T,L形
        if (horizontalPiece.Count >= 3)
        {
            //以每一个横向为基地查纵向是否有两个以上构成T||L
            for (int i = 0; i < horizontalPiece.Count; i++)
            {
                matchPieces.Add(horizontalPiece[i]);   //先加入最终匹配结果
                for (int dir = -1; dir <= 1; dir += 2) //-1向下,1向上
                {
                    for (int index = 1; index <= ydim; index++)
                    {
                        int y = horizontalPiece[i].Y + index * dir;
                        if (y < 0 || y >= ydim) //出界
                        {
                            break;
                        }
                        if (gamePieces[horizontalPiece[i].X, y] != null && gamePieces[horizontalPiece[i].X, y].isColor)
                        {
                            if (gamePieces[horizontalPiece[i].X, y].ColorComponent.ColorTypeRef == color)
                            {
                                verticalPiece.Add(gamePieces[horizontalPiece[i].X, y]);
                                continue;//发现匹配可以继续
                            }
                        }

                        break;//无匹配结束
                    }
                }
                //如果大于2,说明有T,L出现
                if (verticalPiece.Count >= 2)
                {
                    for (int k = 0; k < verticalPiece.Count; k++)//, Debug.Log("横向有L"))
                    {
                        matchPieces.Add(verticalPiece[k]);
                    }
                }
                //出现可能性?
                //110
                //110
                //111
                verticalPiece.Clear();  //清空计算下一个横向piece
            }
            //ShowPiecesAxis(matchPieces);
            return(matchPieces);    //大于三返回真
        }


        //横向没有,进行纵向检测
        horizontalPiece.Clear();
        verticalPiece.Clear();
        matchPieces.Clear();//清空

        verticalPiece.Add(piece);
        //先检测下边,再检测上边
        for (int dir = -1; dir <= 1; dir += 2)
        {
            //邻近两个是否与自身连子
            for (int index = 1; index <= ydim; index++)
            {
                int y = newY + index * dir;
                if (y < 0 || y >= ydim)  //防止越界
                {
                    break;
                }
                //Debug.Log("纵向:" + newX + " " + y);
                if (gamePieces[newX, y] != null && gamePieces[newX, y].isColor)
                {
                    if (gamePieces[newX, y].ColorComponent.ColorTypeRef == color)
                    {
                        verticalPiece.Add(gamePieces[newX, y]);
                        continue;//发现匹配可以继续
                    }
                }

                break;//无匹配结束
            }
        }
        //纵向有三连子,检测L,T
        if (verticalPiece.Count >= 3)
        {
            //以每一个纵向为基查横向是否有两个以上构成T||L
            for (int i = 0; i < verticalPiece.Count; i++)
            {
                matchPieces.Add(verticalPiece[i]);     //先加入最终匹配结果
                for (int dir = -1; dir <= 1; dir += 2) //-1向下,1向上
                {
                    for (int index = 1; index <= xdim; index++)
                    {
                        int x = verticalPiece[i].X + index * dir;
                        if (x < 0 || x >= ydim) //出界
                        {
                            break;
                        }
                        if (gamePieces[x, verticalPiece[i].Y] != null && gamePieces[x, verticalPiece[i].Y].isColor)
                        {
                            if (gamePieces[x, verticalPiece[i].Y].ColorComponent.ColorTypeRef == color)
                            {
                                horizontalPiece.Add(gamePieces[x, verticalPiece[i].Y]);
                                continue;//发现匹配继续
                            }
                        }

                        break;//无匹配
                    }
                }
                //如果大于2,说明有T,L出现
                if (horizontalPiece.Count >= 2)
                {
                    for (int k = 0; k < horizontalPiece.Count; k++)//, Debug.Log("纵向有L"))
                    {
                        matchPieces.Add(horizontalPiece[k]);
                    }
                }
                horizontalPiece.Clear();  //清空计算下一个纵向piece
            }
            //ShowPiecesAxis(matchPieces);
            return(matchPieces);    //大于三返回真
        }
        else
        {
            return(null);   //横向纵向都没有
        }
    }
Example #22
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY) //获取可以配对的元素
    {
        if (piece.IsColored())                                            //如果存在颜色(图片)
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece>();
            List <GamePiece>     verticalPieces   = new List <GamePiece>();//检查当前物体的左右上下格子如果颜色相同保存在链表中
            List <GamePiece>     matchingPieces   = new List <GamePiece>();

            horizontalPieces.Add(piece);                         //把当前格子加入水平链表
            for (int dir = 0; dir <= 1; dir++)                   //改变元素走向,0和1代表不同方向
            {
                for (int xOffSet = 1; xOffSet < xDim; xOffSet++) //遍历两者相邻元素
                {
                    int x;
                    if (dir == 0)           //dir=0时代表左边,1时为右边
                    {
                        x = newX - xOffSet; //左边的x坐标
                    }
                    else
                    {
                        x = newX + xOffSet;
                    }
                    if (x < 0 || x >= xDim)//超出边界时break
                    {
                        break;
                    }
                    if (pieces[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        //先查看水平列,故y轴不变,判断该格子是否存在颜色,且和本格子颜色是否相同,匹配则保存入水平链表
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)//如果水平相同颜色存在3个,则放入match匹配链表
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                }
            }
            if (horizontalPieces.Count >= 3)                     //L型匹配
            {
                for (int i = 0; i < horizontalPieces.Count; i++) //当存在可配对时,遍历所有元素
                {
                    for (int dir = 0; dir <= 1; dir++)           //0上1下 通过水平检查竖直的情况
                    {
                        for (int yOffset = 1; yOffset < yDim; yOffset++)
                        {
                            int y;
                            if (dir == 0)
                            {
                                y = newY - yOffset;
                            }
                            else
                            {
                                y = newY + yOffset;
                            }
                            if (y < 0 || y >= yDim)//超越边界就停止
                            {
                                break;
                            }
                            if (pieces[horizontalPieces[i].X, y].IsColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color)
                            {
                                //检测自己的竖直方向是否存在相同物品,若存在则保存到vertical列表中
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;//不匹配则停止
                            }
                        }
                    }
                    //检测vectorial链表中的数量
                    if (verticalPieces.Count < 2)//除去公用的,只有两个相邻则达标,负责清空继续循环
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {  //如果大于2,则加入matching匹配链表
                        for (int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces[j]);
                        }
                        break;
                    }
                }
            }

            if (matchingPieces.Count >= 3)//存入元素大于3个,则返回
            {
                return(matchingPieces);
            }

            //垂直列表
            horizontalPieces.Clear();
            verticalPieces.Clear();                              //上面使用后清空链表

            verticalPieces.Add(piece);                           //把当前格子加入水平链表
            for (int dir = 0; dir <= 1; dir++)                   //改变元素走向,0和1代表不同方向
            {
                for (int yOffSet = 1; yOffSet < yDim; yOffSet++) //遍历两者相邻元素
                {
                    int y;
                    if (dir == 0)           //dir=0时代表上,1时为下
                    {
                        y = newY - yOffSet; //左边的x坐标
                    }
                    else
                    {
                        y = newY + yOffSet;
                    }
                    if (y < 0 || y >= yDim)//超出边界时break
                    {
                        break;
                    }
                    if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        //先查看水平列,故y轴不变,判断该格子是否存在颜色,且和本格子颜色是否相同,匹配则保存入水平链表
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)//如果水平相同颜色存在3个,则放入match链表
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    matchingPieces.Add(verticalPieces[i]);
                }
            }

            if (verticalPieces.Count >= 3)                     //L型匹配
            {
                for (int i = 0; i < verticalPieces.Count; i++) //当存在可配对时,遍历所有元素
                {
                    for (int dir = 0; dir <= 1; dir++)         //0左1右 通过竖直检查水平的情况
                    {
                        for (int xOffset = 1; xOffset < xDim; xOffset++)
                        {
                            int x;
                            if (dir == 0)
                            {
                                x = newX - xOffset;
                            }
                            else
                            {
                                x = newX + xOffset;
                            }
                            if (x < 0 || x >= xDim)//超越边界就停止
                            {
                                break;
                            }
                            if (pieces[x, verticalPieces[i].Y].IsColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                //检测自己的水平方向是否存在相同物品,若存在则保存到vertical列表中
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;//不匹配则停止
                            }
                        }
                    }
                    //检测horizontalPieces链表中的数量
                    if (horizontalPieces.Count < 2)//除去公用的,只有两个相邻则达标,负责清空继续循环
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {  //如果大于2,则加入matching匹配链表
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }
                        break;
                    }
                }
            }

            if (matchingPieces.Count >= 3)//符合标准则返回匹配(至少三个元素)
            {
                return(matchingPieces);
            }
        }
        return(null);
    }
Example #23
0
    public List <GamePiece> GetMatch(GamePiece piece, int newX, int newY)
    {
        if (piece.IsColored())
        {
            ColorPiece.ColorType color            = piece.ColorComponent.Color;
            List <GamePiece>     horizontalPieces = new List <GamePiece>();
            List <GamePiece>     verticalPieces   = new List <GamePiece>();
            List <GamePiece>     matchingPieces   = new List <GamePiece>();

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

            for (int dir = 0; dir <= 1; dir++)
            {
                for (int xOffset = 1; xOffset < totalRows; xOffset++)
                {
                    int x;
                    if (dir == 0)                       //Left
                    {
                        x = newX - xOffset;
                        if (x < 0)
                        {
                            x = x + totalRows;
                        }
                    }
                    else                         //Right
                    {
                        x = newX + xOffset;
                        if (x >= totalRows)
                        {
                            x = x - totalRows;
                        }
                    }

                    Debug.Log(xOffset);

                    if (x == newX)
                    {
                        break;
                    }

                    if (pieces[x, newY].IsColored() && pieces[x, newY].ColorComponent.Color == color)
                    {
                        horizontalPieces.Add(pieces[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

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

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

                            if (y < 0 || y >= collumnHeight)
                            {
                                break;
                            }

                            if (pieces[horizontalPieces[i].X, y].IsColored() && pieces[horizontalPieces[i].X, y].ColorComponent.Color == color)
                            {
                                verticalPieces.Add(pieces[horizontalPieces[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (verticalPieces.Count < 2)
                    {
                        verticalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < verticalPieces.Count; j++)
                        {
                            matchingPieces.Add(verticalPieces[j]);
                        }
                        break;
                    }
                }
            }

            if (horizontalPieces.Count >= 3)
            {
                for (int i = 0; i < horizontalPieces.Count; i++)
                {
                    matchingPieces.Add(horizontalPieces[i]);
                }
            }

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

            //Didnt find anything going horizontally first now
            //Check Vertical
            horizontalPieces.Clear();
            verticalPieces.Clear();
            verticalPieces.Add(piece);

            for (int dir = 0; dir <= 1; dir++)
            {
                for (int yOffset = 1; yOffset < collumnHeight; yOffset++)
                {
                    int y;
                    if (dir == 0)                       //Left
                    {
                        y = newY - yOffset;
                    }
                    else                         //Right
                    {
                        y = newY + yOffset;
                    }

                    Debug.Log(yOffset + " Y offset");

                    if (y < 0 || y >= collumnHeight)
                    {
                        break;
                    }

                    if (pieces[newX, y].IsColored() && pieces[newX, y].ColorComponent.Color == color)
                    {
                        verticalPieces.Add(pieces[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (verticalPieces.Count >= 3)
            {
                for (int i = 0; i < verticalPieces.Count; i++)
                {
                    Debug.Log(verticalPieces[i].Pos);
                    matchingPieces.Add(verticalPieces[i]);
                }
            }

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

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


                            if (pieces[x, verticalPieces[i].Y].IsColored() && pieces[x, verticalPieces[i].Y].ColorComponent.Color == color)
                            {
                                horizontalPieces.Add(pieces[x, verticalPieces[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (horizontalPieces.Count < 2)
                    {
                        horizontalPieces.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < horizontalPieces.Count; j++)
                        {
                            matchingPieces.Add(horizontalPieces[j]);
                        }
                        break;
                    }
                }
            }

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

        return(null);
    }