public static List <GameObject> CheckVertical3(int row, int column, CandyArray candies) { /* EXAMPLE *\ * * & * * * // Row 4 * * * * * // Row 3 * & * * * // Row 2 * @ * * * // Row 1 * * * * * // Row 0 * \* */ if (row <= GameVariables.Rows - 4) { if ( candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 3, column].GetComponent <Candy>()) ) { return(new List <GameObject> { candies[row, column], candies[row + 1, column], candies[row + 3, column] }); } } /* EXAMPLE *\ * * * * * * // Row 4 * & * * * // Row 3 * @ * * * // Row 2 * * * * * // Row 1 * & * * * // Row 0 * \* */ if (row >= 2 && row <= GameVariables.Rows - 2) { if ( candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>()) && candies[row - 2, column].GetComponent <Candy>().IsSameType(candies[row - 2, column].GetComponent <Candy>()) ) { return(new List <GameObject> { candies[row, column], candies[row - 1, column], candies[row + 2, column] }); } } return(null); } // CheckVertical3
/// <summary> /// Initialize candies in the scene /// </summary> public void InitCandies() { if (candyArray != null) { DestroyAllCandy(); } candyArray = new CandyArray(); spawnPosition = new Vector2[GameVariables.Columns]; for (int row = 0; row < GameVariables.Rows; row++) { for (int col = 0; col < GameVariables.Columns; col++) { GameObject newCandy = GetRandomCandy(); //Checking, previous two horizontal candies are of same colour. while (col >= 2 && candyArray[row, col - 1].GetComponent <Candy>().IsOfSameColour(newCandy.GetComponent <Candy>()) && candyArray[row, col - 2].GetComponent <Candy>().IsOfSameColour(newCandy.GetComponent <Candy>())) { newCandy = GetRandomCandy(); } //Checking, previous two vertical candies are of same colour. while (row >= 2 && candyArray[row - 1, col].GetComponent <Candy>().IsOfSameColour(newCandy.GetComponent <Candy>()) && candyArray[row - 2, col].GetComponent <Candy>().IsOfSameColour(newCandy.GetComponent <Candy>())) { newCandy = GetRandomCandy(); } CreateNewCandy(row, col, newCandy); } } SetSpawnPosition(); }
public static List <GameObject> CheckHorizontal1(int row, int column, CandyArray candies) { if (column <= GameVariables.Columns - 2) { if (candies[row, column].GetComponent <Candy> .IsSameType(candies[row, column + 1].GetComponent <Candy>())) { if (row >= 1 && column >= 1) { if (candies [row, column].GetComponent <Candy>().IsSameType(candies [row - 1, column - 1].GetComponent <Candy> ())) { return(new List <GameObject> () { candies [row, column], candies [row, column + 1], candies [row - 1, column - 1] }); if (row <= GameVariables.Rows - 2 && column >= 1) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column - 1].GetComponent <Candy>())) { return(new List <GameObject> () { candies [row, column], candies [row, column + 1], candies [row + 1, column - 1] }); } } } } } } return(null); }
} // CheckVertical1 // TODO public static List <GameObject> CheckVertical2(int row, int column, CandyArray candies) { /* EXAMPLE *\ * * * * * * // Row 4 * & * * * * // Row 3 * & * * * // Row 2 * @ * * * // Row 1 * * * * * // Row 0 * \* */ if (row <= GameVariables.Rows - 3) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>())) { if (column >= 1) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 2, column - 1].GetComponent <Candy>())) { return new List <GameObject> { candies[row, column], candies[row + 1, column], candies[row + 2, column - 1] } } ; } } else if (column <= GameVariables.Columns - 2) { /* EXAMPLE *\ * * * * * * // Row 4 * * & * * // Row 3 * & * * * // Row 2 * @ * * * // Row 1 * * * * * // Row 0 * \* */ if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 2, column + 1].GetComponent <Candy>())) { return(new List <GameObject> { candies[row, column], candies[row + 1, column], candies[row + 2, column + 1] }); } } } return(null); }
/// <summary> /// Generate candies for level /// </summary> public void InitializeCandyAndSpawnPositions() { InitializeVariables(); // If any candies are left, destroy them if (candies != null) { DestroyAllCandy(); } // create new candy array candies = new CandyArray(); // Define the number of columns in this array of vector 2 positions spawnPositions = new Vector2[GameVariables.Columns]; // loop through row and column count and generate the candies for (int row = 0; row < GameVariables.Rows; row++) { for (int column = 0; column < GameVariables.Columns; column++) { GameObject newCandy = GetRandomCandy(); // If we find two adjacent candies horizontally, loop through until we force it to a non-match candy while ( column >= 2 && candies[row, column - 1].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>()) && candies[row, column - 2].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>()) ) { newCandy = GetRandomCandy(); } // If two adjacent candies in a vertical line, loop through until we get a non-match candy while ( row >= 2 && candies[row - 1, column].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>()) && candies[row - 2, column].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>()) ) { // generate the candy newCandy = GetRandomCandy(); } // Place the new candy InstantiateAndPlaceNewCandy(row, column, newCandy); } } SetupSpawnPositions(); }
public static List <GameObject> CheckVertical2(int row, int column, CandyArray Candys) { if (row <= Constant.xSize - 3) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 1, column].GetComponent <Candy>())) { if (column >= 1) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 2, column - 1].GetComponent <Candy>())) { return new List <GameObject>() { Candys[row, column], Candys[row + 1, column], Candys[row + 2, column - 1] } } } ; /* example *\ * * * * * * & * * * * * & * * * * & * * * * * * * * \* example */ if (column <= Constant.ySize - 2) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 2, column + 1].GetComponent <Candy>())) { return new List <GameObject>() { Candys[row, column], Candys[row + 1, column], Candys[row + 2, column + 1] } } } ; /* example *\ * * * * * * * & * * * & * * * * & * * * * * * * * \* example */ } } return(null); }
/// Will check for potential matches vertically and horizontally public static IEnumerable <GameObject> GetPotentialMatches(CandyArray candies) { //list contain all matches found List <List <GameObject> > matches = new List <List <GameObject> >(); for (int i = 0; i < Constant.xSize; i++) { for (int j = 0; j < Constant.ySize; j++) { var matches1 = CheckHorizontal1(i, j, candies); var matches2 = CheckHorizontal2(i, j, candies); var matches3 = CheckHorizontal3(i, j, candies); var matches4 = CheckVertical1(i, j, candies); var matches5 = CheckVertical2(i, j, candies); var matches6 = CheckVertical3(i, j, candies); if (matches1 != null) { matches.Add(matches1); } if (matches2 != null) { matches.Add(matches2); } if (matches3 != null) { matches.Add(matches3); } if (matches4 != null) { matches.Add(matches4); } if (matches5 != null) { matches.Add(matches5); } if (matches6 != null) { matches.Add(matches6); } if (matches.Count >= 3) { return(matches[UnityEngine.Random.Range(0, matches.Count)]); } if (j >= Constant.xSize / 2 && matches.Count > 0 && matches.Count >= 2) { return(matches[UnityEngine.Random.Range(0, matches.Count - 1)]); } } } return(null); }
public static IEnumerable <GameObject> GetPotentialMatches(CandyArray candies) { List <List <GameObject> > matches = new List <List <GameObject> > (); for (int row = 0; row < GameVariables.Rows; row++) { for (int column = 0; column < GameVariables.Columns; column++) { var matches1 = CheckHorizontal1(row, column, candies); var matches2 = CheckHorizontal2(row, column, candies); var matches3 = CheckHorizontal3(row, column, candies); var matches4 = CheckVertical1(row, column, candies); var matches5 = CheckVertical2(row, column, candies); var matches6 = CheckVertical3(row, column, candies); if (matches1 != null) { matches.Add(matches1); } if (matches2 != null) { matches.Add(matches2); } if (matches3 != null) { matches.Add(matches3); } if (matches4 != null) { matches.Add(matches4); } if (matches5 != null) { matches.Add(matches5); } if (matches6 != null) { matches.Add(matches6); } if (matches.Count >= 3) { return(matches[Random.Range(0, matches.Count - 1)]); } if (row >= GameVariables.Rows / 2 && matches.Count > 0 && matches.Count <= 2) { return(matches[Random.Range(0, matches.Count - 1)]); } } } return(null); }
public static List <GameObject> CheckHorizontal2(int row, int column, CandyArray candies) { if (column <= GameVariables.Columns - 3) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>())) { if (row >= 1 && column <= GameVariables.Columns - 3) { } } } return(null); }
public static List <GameObject> CheckVertical3(int row, int column, CandyArray Candys) { if (row <= Constant.xSize - 4) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 1, column].GetComponent <Candy>()) && Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 3, column].GetComponent <Candy>())) { return(new List <GameObject>() { Candys[row, column], Candys[row + 1, column], Candys[row + 3, column] }); } } /* example *\ * & * * * * * * * * * & * * * * & * * * * * * * * \* example */ if (row >= 2 && row <= Constant.xSize - 2) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row + 1, column].GetComponent <Candy>()) && Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row - 2, column].GetComponent <Candy>())) { return(new List <GameObject>() { Candys[row, column], Candys[row + 1, column], Candys[row - 2, column] }); } } /* example *\ * * * * * * & * * * * & * * * * * * * * * & * * * \* example */ return(null); }
public static List <GameObject> CheckHorizontal3(int row, int column, CandyArray Candys) { if (column <= Constant.ySize - 4) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row, column + 1].GetComponent <Candy>()) && Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row, column + 3].GetComponent <Candy>())) { return(new List <GameObject>() { Candys[row, column], Candys[row, column + 1], Candys[row, column + 3] }); } /* example *\ * * * * * * * * * * * * * * * * & & * & * * * * * \* example */ } if (column >= 2 && column <= Constant.ySize - 2) { if (Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row, column + 1].GetComponent <Candy>()) && Candys[row, column].GetComponent <Candy>(). IsSameType(Candys[row, column - 2].GetComponent <Candy>())) { return(new List <GameObject>() { Candys[row, column], Candys[row, column + 1], Candys[row, column - 2] }); } /* example *\ * * * * * * * * * * * * * * * * & * & & * * * * * \* example */ } return(null); }
/// <summary> /// Get the list of matched candy(GameObject) by passing the candy array /// </summary> /// <param name="candyArray"></param> /// <returns></returns> public static IEnumerable <GameObject> GetPotentialMatches(CandyArray candyArray) { List <List <GameObject> > matches = new List <List <GameObject> >(); for (int row = 0; row < GameVariables.Rows; row++) { for (int col = 0; col < GameVariables.Columns; col++) { //HORIZONTAL List <GameObject> match1 = CheckHorizontallyOne(row, col, candyArray); List <GameObject> match2 = CheckHorizontallyTwo(row, col, candyArray); List <GameObject> match3 = CheckHorizontallyThree(row, col, candyArray); //VERTICAL List <GameObject> match4 = CheckVerticallyOne(row, col, candyArray); List <GameObject> match5 = CheckVerticallyTwo(row, col, candyArray); List <GameObject> match6 = CheckVerticallyThree(row, col, candyArray); if (match1 != null) { matches.Add(match1); } if (match2 != null) { matches.Add(match2); } if (match3 != null) { matches.Add(match3); } if (match4 != null) { matches.Add(match4); } if (match5 != null) { matches.Add(match5); } if (match6 != null) { matches.Add(match6); } if ((matches.Count >= 3) || (row >= GameVariables.Rows / 2 && matches.Count > 0 && matches.Count <= 2)) { return(matches[Random.Range(0, matches.Count - 1)]); } } } return(null); }
public static List <GameObject> CheckVertical2(int row, int column, CandyArray candies) { if (row <= GameVariables.Rows - 3) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>())) { if (column >= 1) { if (candies [row, column].GetComponent <Candy> ().IsSameType(candies [row + 2, column - 1].GetComponent <Candy> ())) { return(new List <GameObject> { candies [row, column], candies [row + 1, column], candies [row + 2, column - 1] }); /* example *\ * * * * * * & * * * * * & * * * * @ * * * <-- starter * * * * * \* example */ } if (column <= GameVariables.Columns - 2) { if (candies [row, column].GetComponent <Candy> ().IsSameType(candies [row + 2, column + 1].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row + 1, column], candies [row + 2, column + 1] }); /* example *\ * * * * * * * & * * * & * * * * @ * * * <-- starter * * * * * \* example */ } } } } } return(null); }
/// <summary> /// Check horizontal matches /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <param name="candyArray"></param> /// <returns></returns> public static List <GameObject> CheckHorizontallyOne(int row, int col, CandyArray candyArray) { /*Example * 0 0 0 0 0 * 0 @ @ 0 0 * @ 0 0 0 0 * 0 0 0 0 0 */ if (col <= GameVariables.Columns - 2) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row, col + 1].GetComponent <Candy>())) { if (row >= 1 && col >= 1) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row - 1, col - 1].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row, col + 1], candyArray[row - 1, col - 1] }); if (row <= GameVariables.Rows - 2 && col >= 1) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 1, col - 1].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row, col + 1], candyArray[row + 1, col - 1] }); } } /*Example * 0 0 0 0 0 * 0 @ @ 0 0 * @ 0 0 0 0 * 0 0 0 0 0 */ } } } } return(null); }
public static List <GameObject> CheckVerticallyTwo(int row, int col, CandyArray candyArray) { /*Example * 0 0 0 0 0 * @ 0 0 0 0 * 0 @ 0 0 0 * 0 @ 0 0 0 */ if (row <= GameVariables.Rows - 3) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 1, col].GetComponent <Candy>())) { if (col >= 1) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 2, col - 1].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row + 1, col], candyArray[row + 2, col - 1] }); /*Example * 0 0 0 0 0 * 0 0 @ 0 0 * 0 @ 0 0 0 * 0 @ 0 0 0 */ if (col <= GameVariables.Columns - 2) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 2, col + 1].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row + 1, col], candyArray[row + 2, col + 1] }); } } } } } } return(null); }
public static List <GameObject> CheckHorizontal1(int row, int column, CandyArray candies) { if (column <= GameVariables.Columns - 2) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>())) { if (row >= 1 && column >= 1) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row - 1, column - 1].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row, column + 1], candies [row - 1, column - 1] }); /* example *\ * * * * * * * * * * * * * * * * @ & * * <-- The first one is the starter * & * * * * \* example */ } if (row <= GameVariables.Rows - 2 && column >= 1) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column - 1].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row, column + 1], candies [row + 1, column - 1] }); } } /* example *\ * * * * * * * * * * * & * * * * * @ & * * <-- The first one is the starter * * * * * \* example */ } } } return(null); }
public static List <GameObject> CheckHorizontal3(int row, int column, CandyArray candies) { if (column <= GameVariables.Columns - 4) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 3].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row, column + 1], candies [row, column + 3] }); /* example *\ * * * * * * * * * * * * * * * * @ & * & <-- The first one is the starter * * * * * \* example */ } } if (column >= 2 && column <= GameVariables.Columns - 2) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column - 2].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row, column + 1], candies [row, column - 2] }); /* example *\ * * * * * * * * * * * * * * * * & * @ & <-- The middle one is the starter * * * * * \* example */ } } return(null); }
public static List <GameObject> CheckVertical3(int row, int column, CandyArray candies) { if (row <= GameVariables.Rows - 4) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy> ().IsSameType(candies[row + 3, column].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row + 1, column], candies [row + 3, column] }); /* example *\ * & * * * * * * * * * & * * * * @ * * * <-- starter * * * * * \* example */ } } if (row >= 2 && row <= GameVariables.Rows - 2) { if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row - 2, column].GetComponent <Candy>())) { return(new List <GameObject> { candies [row, column], candies [row + 1, column], candies [row - 2, column] }); /* example *\ * * * * * * & * * * * @ * * * <-- starter * * * * * * & * * * \* example */ } } return(null); }
public static List <GameObject> CheckVerticallyThree(int row, int col, CandyArray candyArray) { /*Example * 0 @ 0 0 0 * 0 0 0 0 0 * 0 @ 0 0 0 * 0 @ 0 0 0 */ if (row <= GameVariables.Rows - 4) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 1, col].GetComponent <Candy>()) && candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 3, col].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row + 1, col], candyArray[row + 3, col] }); } } /*Example * 0 @ 0 0 0 * 0 @ 0 0 0 * 0 0 0 0 0 * 0 @ 0 0 0 */ if (row >= 2 && row <= GameVariables.Rows - 2) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row + 1, col].GetComponent <Candy>()) && candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row - 2, col].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row + 1, col], candyArray[row - 2, col] }); } } return(null); }
public static List <GameObject> CheckHorizontallyThree(int row, int col, CandyArray candyArray) { /*Example * 0 0 0 0 0 * 0 @ @ 0 @ * 0 0 0 0 0 * 0 0 0 0 0 */ if (col <= GameVariables.Columns - 4) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row, col + 1].GetComponent <Candy>()) && candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row, col + 3].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row, col + 1], candyArray[row, col + 3] }); } } /*Example * 0 0 0 0 0 * 0 @ 0 @ @ * 0 0 0 0 0 * 0 0 0 0 0 */ if (col >= 2 && col <= GameVariables.Columns - 2) { if (candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row, col + 1].GetComponent <Candy>()) && candyArray[row, col].GetComponent <Candy>().IsOfSameColour(candyArray[row, col - 2].GetComponent <Candy>())) { return(new List <GameObject> { candyArray[row, col], candyArray[row, col + 1], candyArray[row, col - 2] }); } } return(null); }
public void InitializeCandyAndSpawnPositions() { InitializeVariables(); if (candies != null) { DestroyAllCandy(); } candies = new CandyArray(); SpawnPositions = new Vector2[GameVariables.Columns]; for (int row = 0; row < GameVariables.Rows; row++) { for (int column = 0; column < GameVariables.Columns; column++) { GameObject newCandy = GetRandomCandy(); //- Checking if previous 2 horizontal candies are the same while (column >= 2 && candies[row, column - 1].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>()) && candies[row, column - 2].GetComponent <Candy>().IsSameType(newCandy.GetComponent <Candy>())) { newCandy = GetRandomCandy(); } //- Checking if previous 2 vertical candies are the same while (row >= 2 && candies [row - 1, column].GetComponent <Candy> ().IsSameType(newCandy.GetComponent <Candy> ()) && candies [row - 2, column].GetComponent <Candy> ().IsSameType(newCandy.GetComponent <Candy> ())) { newCandy = GetRandomCandy(); } InstantiateAndPlaceNewCandy(row, column, newCandy); } } SetupSpawnPositions(); }
public void FillGrid() { InitializeVariables(); if (candies != null) { DestroyAllCandy(); } candies = new CandyArray(); SpawnPositions = new Vector2[Constant.ySize]; // grid = new Candy[Constant.xSize, Constant.ySize] ; for (int i = 0; i < Constant.xSize; i++) { for (int j = 0; j < Constant.ySize; j++) { GameObject random = GetRandomCandy(); //check if two previous horizontal are of the same type while (j >= 2 && candies[i, j - 1].GetComponent <Candy>().IsSameType(random.GetComponent <Candy>()) && candies[i, j - 2].GetComponent <Candy>().IsSameType(random.GetComponent <Candy>())) { random = GetRandomCandy(); } //check if two previous vertical are of the same type while (i >= 2 && candies[i - 1, j].GetComponent <Candy>() .IsSameType(random.GetComponent <Candy>()) && candies[i - 2, j].GetComponent <Candy>().IsSameType(random.GetComponent <Candy>())) { random = GetRandomCandy(); } InstantiateAndPlaceNewCandy(i, j, random); } } SetupSpawnPositions(); }
} // CheckHorizontal3 // First function to help us find based on column public static List <GameObject> CheckVertical1(int row, int column, CandyArray candies) { /* EXAMPLE *\ * * * * * * // Row 4 * * * * * // Row 3 * & * * * // Row 2 * @ * * * // Row 1 * & * * * * // Row 0 * \* */ // if there is at least one row above if (row <= GameVariables.Rows - 2) { // look at candy immediately above it if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column].GetComponent <Candy>())) { // Now confirm we're not on the first row or first column, so we can check "below" and "left" if (column >= 1 && row >= 1) { // compare the two if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row - 1, column - 1].GetComponent <Candy>())) { return(new List <GameObject> { candies[row, column], candies[row + 1, column], candies[row - 1, column - 1] }); } } else if (column <= GameVariables.Columns - 2 && row >= 1) { // Confirm we're not on the first row or last column, so we can check "below" and "right" /* EXAMPLE *\ * * * * * * // Row 4 * * * * * // Row 3 * & * * * // Row 2 * @ * * * // Row 1 * * & * * // Row 0 * \* */ // compare the two if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row - 1, column + 1].GetComponent <Candy>())) { return(new List <GameObject> { candies[row, column], candies[row + 1, column], candies[row - 1, column + 1] }); } } } } return(null); } // CheckVertical1
public static IEnumerable <GameObject> GetPotentialMatches(CandyArray candies) { List <List <GameObject> > matches = new List <List <GameObject> >(); return(null); }
} // CheckHorizontal1 // checking to the right of the specific candy public static List <GameObject> CheckHorizontal2(int row, int column, CandyArray candies) { /* EXAMPLE *\ * * * * * * * * * * * * * * * * * @ & * * * * * & * * \* */ // Prep to look at the column two places to the right if (column <= GameVariables.Columns - 3) { // check if the candy to the right is the same Type if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>())) { // If we are at least in the 2nd row (starts numbering at zero), and if we are at least 3 columns from end on the right if (row >= 1 && column <= GameVariables.Columns - 3) { // check two right, one down if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row - 1, column + 2].GetComponent <Candy>())) { return(new List <GameObject> { candies[row, column], candies[row, column + 1], candies[row - 1, column + 2] }); } else if (row <= GameVariables.Rows - 2 && column <= GameVariables.Columns - 3) { /* EXAMPLE *\ * * * * * * * * * * * * * * & * * @ & * * * * * * * * \* */ // check two right, one up if (candies[row, column].GetComponent <Candy>().IsSameType(candies[row + 1, column + 2].GetComponent <Candy>())) { return(new List <GameObject> { candies[row, column], candies[row, column + 1], candies[row + 1, column + 2] }); } } } } } // catch all return(null); } // CheckHorizontal2
} // CheckHorizontal2 // checking to the right of the specific candy, skipping one in the row public static List <GameObject> CheckHorizontal3(int row, int column, CandyArray candies) { /* EXAMPLE *\ * * * * * * * * * * * * * * * * * @ & * & * * * * * * \* */ // Prep to look at the column two places to the right if (column <= GameVariables.Columns - 4) { // check if the candy to the right is the same Type and check if the candy +3 to the right if ( candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 3].GetComponent <Candy>()) ) { return(new List <GameObject> { candies[row, column], candies[row, column + 1], candies[row, column + 3] }); } } /* EXAMPLE *\ * * * * * * * * * * * * * * * * * & * @ & * * * * * * \* */ // Prep to look at the column two places to the left if (column >= 2 && column <= GameVariables.Columns - 2) { // check if the candy to the right is the same Type and check if the candy -2 to the left if ( candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column + 1].GetComponent <Candy>()) && candies[row, column].GetComponent <Candy>().IsSameType(candies[row, column - 2].GetComponent <Candy>()) ) { return(new List <GameObject> { candies[row, column], candies[row, column + 1], candies[row, column - 2] }); } } // catch all return(null); } // CheckHorizontal3
public static IEnumerable <GameObject> GetPotentialMatches(CandyArray candies) { // create a list of Lists List <List <GameObject> > matches = new List <List <GameObject> >(); // loop through each candy and check for matches for (int row = 0; row < GameVariables.Rows; row++) { for (int column = 0; column < GameVariables.Columns; column++) { // check horizontal matches var matches1 = CheckHorizontal1(row, column, candies); var matches2 = CheckHorizontal2(row, column, candies); var matches3 = CheckHorizontal3(row, column, candies); // Check vertical matches var matches4 = CheckVertical1(row, column, candies); var matches5 = CheckVertical2(row, column, candies); var matches6 = CheckVertical3(row, column, candies); // Now verify if they had anything, and if so, add it to the total matches list if (matches1 != null) { matches.Add(matches1); } if (matches2 != null) { matches.Add(matches2); } if (matches3 != null) { matches.Add(matches3); } if (matches4 != null) { matches.Add(matches4); } if (matches5 != null) { matches.Add(matches5); } if (matches6 != null) { matches.Add(matches6); } // if we have more than three matches, return a random one if (matches.Count >= 3) { return(matches[Random.Range(0, matches.Count - 1)]); } // if we're in middle of the loop and we have one or two, return it. if (row > GameVariables.Rows / 2 && matches.Count > 0 && matches.Count <= 2) { return(matches[Random.Range(0, matches.Count - 1)]); } } } return(null); }