[Re-Edited] Clone Of Bejeweled for Android

Hi Guys any one could point me out how to make Bejeweled type game…all i want to know is how to check for the three or more objects are in line either vertical or Horizontal axis?? all the part is in 2d Only…
Any kind of help will be much appreciated…Can’t afford plugins…For learning purpose only…If providing Scripts Then Go for C# only…Thanks a Lot in Advance…

On way to do it is to first create a list, and then create a loop-function that checks the neighbours of a object.

  • If object has a neighbour (horizontally and vertically) that is the same type, then save that object to your list.
  • When you have checked all the neighbours for the first object, take your list and you do the same to all the objects in the list, until all objects have checked if they have a neighbour of the same type.

Remember to have some kind of check that you either ignore a neighbour if its already in the list or that you ignore the last object you checked so you won’t get duplicates in the list

  • Now you have a list of objects which are connected both vertically and horizontally and are connected as neighbours.
  • If the list has enough objects (more than 3 objects for instance), loop through your finished list and destroy the objects and when done clear the list, and you are done.

A very simple pseudo example code:

private List<GameObject>m_listOfObjectsToRemove = new List<GameObject>();
private int m_minimumNoOfObjects = 3;

private void StartCheck(GameObject startObject)
{
    m_listOfObjectsToRemove.Add(startObject);
    CheckNeighbours(startObject, null, true, true);
    
    GameObject previousStartObject = startObject;
    
    for(int i = 1; i < m_listOfObjectsToRemove.Count; ++i)
    {
        CheckNeighbours(m_listOfObjectsToRemove*, previousStartObject, true, true);*

previousStartObject = m_listOfObjectsToRemove*;
_
}*_

if(m_listOfObjectsToRemove.Count > m_minimumNoOfObjects)
{
for(int i = 1; i < m_listOfObjectsToRemove.Count; ++i)
{
Destroy(m_listOfObjectsToRemove*);
_}
m_listOfObjectsToRemove.Clear();
}
}
private void CheckNeighbours(GameObject startObject, GameObject previousStartObject, bool check horizontally, bool check vertically)
{
// Check all four neighbours (vertically and horizontally)
// and make sure the previous start object is ignored if found…*_

if(horizontally == true)
{
if(leftNeighbourObject != null)
{
if(leftNeighbourObject.typeOfObject == startObject.typeOfObject && leftNeighbourObject != previousStartObject)
{
m_listOfObjectsToRemove.Add(leftNeighbourObject);
}
}
if(rightNeighbourObject != null)
{
if(rightNeighbourObject.typeOfObject == startObject.typeOfObject && rightNeighbourObject != previousStartObject)
{
m_listOfObjectsToRemove.Add(rightNeighbourObject);
}
}
}

if(vertically == true)
{
if(aboveNeighbourObject != null)
{
if(aboveNeighbourObject.typeOfObject == startObject.typeOfObject && aboveNeighbourObject != previousStartObject)
{
m_listOfObjectsToRemove.Add(aboveNeighbourObject);
}
}
if(belowNeighbourObject != null)
{
if(belowNeighbourObject.typeOfObject == startObject.typeOfObject && belowNeighbourObject != previousStartObject)
{
m_listOfObjectsToRemove.Add(belowNeighbourObject);
}
}
}
}
It’s not a very nice looking code, but it should give you an idea of one way you can do it.
Good luck!

int gameSize = 6;
public GameObject MyGems = new GameObject [6];
public Material MyMaterial = new Material [6];
void Start ()
{
for(int x = 0;x < 6 ; x++)
{
for(int y = 0;y < 6 ; y++)
{
int RandomNumber = Random.Range (0,6);

				if(RandomNumber == 0)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x,y,0),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}
				
				if(RandomNumber == 1)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x+0.35f,y-0.0f,0f),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}
				if(RandomNumber == 2)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x+0.05f,y+0f,0.0f),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}
				if(RandomNumber == 3)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x+0.0f,y-0.2f,0),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}
				if(RandomNumber == 4)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x+0.0f,y+0.94f,0.0f),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}
				if(RandomNumber == 5)
				{
				
				Instantiate (MyGems[RandomNumber],new Vector3(x,y,0),MyGems[RandomNumber].transform.rotation);
				MyGems[RandomNumber].renderer.material = MyMaterial[RandomNumber];
				}

Was unable to Edit the question and Post this So posted as a answer so sorry about this
Till now i am having this Simple Script to Create the Grid…

  1. How to swap objects (only next either Horizontally or Vertically)

2)After Swapping Check if there are 3 Or more same are there or not(either Horizontally or Vertically Only) if there then destroy the matching objects…

Currently Grid of Object Look like this------> 6370-grid.jpg

I am trying to develop this kind of game for Android, and i am stuck with swapping grid cells (cubes in my case). I successfully initialized grid (9x9) and filled it with cubes with different textures. I am using Mouse Button Down, Up for catching movement of cubes. On Down i just get selected cube using RayCast and then while moving I detect direction, and move one cube till it goes for example 0,1 on x axis and then try to swap with next cube (in this case right of current cube, so i am moving just by x). For now swapping is my main problem, i know it is probably simple but since I am amateur in Unity world, this is really annoying for me now.

Chaos of my code:
//////////////////////////////////

if(Input.GetMouseButtonDown (0)) {

        RaycastHit hitInfo;

        target = GetClickedObject(out hitInfo);

        if (target != null) {

            _mouseState = true;
			
            screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);

            offset = target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
			
			startScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
			
        	//convert the screen mouse position to world point and adjust with offset -> temp position of touched cell!

        	startScreenPosition = Camera.main.ScreenToWorldPoint(startScreenSpace) + offset;
			
			
			Debug.Log("Kliknuto X= " + startScreenPosition.x + " Kliknuto Y= " + startScreenPosition.y);
			
			//referece to selected obj from array of Gams
			currentSelectedObj = arrayOfGames[GRID_SIZE * (int)startScreenPosition.y + (int)startScreenPosition.x];
			
			Debug.Log("currentSelectedObj cor: " + currentSelectedObj.positionInGrid);
			
			//currentSelectedObj.gObject.transform.position = currentSelectedObj.positionInGrid;
			
			
			Debug.Log("World cor:" + currentSelectedObj.gObject.transform.position);
			
			setCurrentDirection = false;
			
        }

    }

    if (Input.GetMouseButtonUp (0)) {

        _mouseState = false;
		
		
    }

    if (_mouseState) {

        //keep track of the mouse position
		
		
        curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

        //convert the screen mouse position to world point and adjust with offset

        curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
		//Debug.Log("Curr pos: " + curPosition);
		
		//najpre odredjujemo pravac kretanja
		
		if(!setCurrentDirection)
		{
			
			
			if(curPosition.x != startScreenPosition.x)
			{//promena po x -> horizontalno kretanje
				
				setCurrentDirection = true;
				
				//Debug.Log("Move Drirection " + "promena po x -> horizontalno kretanje");
				
				if(curPosition.x > startScreenPosition.x)
					currDirection = MovingDirection.Right;
				else
					currDirection = MovingDirection.Left;
			}	
			else if(curPosition.y != startScreenPosition.y)
			{ //promena po y -> vertikalno kretanje
				
				setCurrentDirection = true;
				
				
				if(curPosition.y > startScreenPosition.y)
					currDirection = MovingDirection.Top;
				else
					currDirection = MovingDirection.Bottom;
			}
		}
		
		
		
		if(setCurrentDirection && isNeighbrouExist(currDirection))
		{
		

			if(currDirection == MovingDirection.Right && (currentSelectedObj.gObject.transform.position.x >= startScreenPosition.x + 0.1))
			{
				//Debug.Log("Pozicija targeta: " + target.transform.position);
				
				_mouseState = false;
				
				SwapCubes(currentSelectedObj);
				
			}
			else
			{
				switch(currDirection)
				{
					case MovingDirection.Bottom:
					
						target.transform.position = new Vector3(target.transform.position.x, curPosition.y, target.transform.position.z);
						break;
					
					case MovingDirection.Top:
						target.transform.position = new Vector3(target.transform.position.x, curPosition.y, target.transform.position.z);
						break;
								
					case MovingDirection.Left:
						target.transform.position = new Vector3(curPosition.x, target.transform.position.y, target.transform.position.z);
						break;
					
					case MovingDirection.Right:
						target.transform.position = new Vector3(curPosition.x, target.transform.position.y, target.transform.position.z);
						break;
					
				}
			}
			
			
		}
		
    }

}

	void SwapCubes(Gam sourceCube)
{
	
	Vector3 tempVect = arrayOfGames[sourceCube.indexInGrid].getPositionInGrid();
	arrayOfGames[sourceCube.indexInGrid].gObject.transform.position = new Vector3(arrayOfGames[sourceCube.indexInGrid + 1].gObject.transform.position.x, 
														arrayOfGames[sourceCube.indexInGrid + 1].gObject.transform.position.y, 
														arrayOfGames[sourceCube.indexInGrid + 1].gObject.transform.position.z);
	
	arrayOfGames[sourceCube.indexInGrid + 1].gObject.transform.position = tempVect;
	
	
	//change position in grid
	int temp = sourceCube.indexInGrid;
	sourceCube.indexInGrid = arrayOfGames[sourceCube.indexInGrid + 1].indexInGrid;
	arrayOfGames[sourceCube.indexInGrid + 1].indexInGrid = temp;
	
	
	Vector3 tempPosition = sourceCube.positionInGrid;
	sourceCube.positionInGrid = arrayOfGames[sourceCube.indexInGrid - 1].positionInGrid;
	arrayOfGames[sourceCube.indexInGrid - 1].positionInGrid = tempPosition;
	
}

///////////////////////////////////////////

So, this is BIG chunk of my code, a apologize because I am so detailed, but I am stuck, and don’t know if my logic is good for now. Any advice or help is more then welcome.

@Maulik2208 & rest of you → if someone has some kind of tutorial or detailed explanation for this game in Unity, please feel free to share with a group.

Thank you in advance!