Jump to content

Hi guys, I'm looking for someone to aid me while making a Sliding Puzzle game. I already made the initial code and I can't figure out how to these methods:

- randomizing initial blocks (done)

Spoiler

for (int q = 0; q < 15;) 
{
     int a = Random.Range (0, 15);
     if (!randomList.Contains (a)) 
     {
     	randomList.Add (a);
     	q++;
     }
}

 

- moving the blocks

- checker as well if the blocks should move accordingly

 

I'm using Texture2D for the blocks

 

here's the code snip:

 

Spoiler

using UnityEngine;
using System.Collections;

public class ActivityScript : MonoBehaviour {

	public Texture2D[] image1;
	public Texture2D image2;
	Rect[] imageRectangle;
	Rect backgroundImage;
	bool isCalled, isPressed;
	GUIStyle menuStyle;

	int randomer, x, y;
	void Start () {
		x = 0;

		menuStyle = new GUIStyle ();
		menuStyle.alignment = TextAnchor.MiddleCenter;
		menuStyle.normal.textColor = Color.white;

		backgroundImage = new Rect (0, 0, Screen.width, Screen.height);
		isCalled = false;
		isPressed = true;

		imageRectangle = new Rect[15];

		imageRectangle [0] = new Rect (Screen.width / 2 - 150, Screen.height / 4 - 75, 75, 75);
		imageRectangle [1] = new Rect (Screen.width / 2 - 75, Screen.height / 4 - 75, 75, 75);
		imageRectangle [2] = new Rect (Screen.width / 2, Screen.height / 4 - 75, 75, 75);
		imageRectangle [3] = new Rect (Screen.width / 2 + 75, Screen.height / 4 - 75, 75, 75);

		imageRectangle [4] = new Rect (Screen.width / 2 - 150, Screen.height / 4, 75, 75);
		imageRectangle [5] = new Rect (Screen.width / 2 - 75, Screen.height / 4, 75, 75);
		imageRectangle [6] = new Rect (Screen.width / 2, Screen.height / 4, 75, 75);
		imageRectangle [7] = new Rect (Screen.width / 2 + 75, Screen.height / 4, 75, 75);

		imageRectangle [8] = new Rect (Screen.width / 2 - 150, Screen.height / 4 + 75, 75, 75);
		imageRectangle [9] = new Rect (Screen.width / 2 - 75, Screen.height / 4 + 75, 75, 75);
		imageRectangle [10] = new Rect (Screen.width / 2, Screen.height / 4 + 75, 75, 75);
		imageRectangle [11] = new Rect (Screen.width / 2 + 75, Screen.height / 4 + 75, 75, 75);

		imageRectangle [12] = new Rect (Screen.width / 2 - 150, Screen.height / 4 + 150, 75, 75);
		imageRectangle [13] = new Rect (Screen.width / 2 - 75, Screen.height / 4 + 150, 75, 75);
		imageRectangle [14] = new Rect (Screen.width / 2, Screen.height / 4 + 150, 75, 75);
	}
	

	void Update () {
		if (Input.GetKeyDown (KeyCode.Escape)) {
			isCalled = false;
			isPressed = true;
		}

		/*if (Input.GetMouseButtonDown ("Fire1")) {
			
		}*/

	}

	void OnGUI()
	{		
		if (isCalled) {
			
			GUI.DrawTexture (imageRectangle [0], image1 [0]);
			GUI.DrawTexture (imageRectangle [1], image1 [1]);
			GUI.DrawTexture (imageRectangle [2], image1 [2]);
			GUI.DrawTexture (imageRectangle [3], image1 [3]);

			GUI.DrawTexture (imageRectangle [4], image1 [4]);
			GUI.DrawTexture (imageRectangle [5], image1 [5]);
			GUI.DrawTexture (imageRectangle [6], image1 [6]);
			GUI.DrawTexture (imageRectangle [7], image1 [7]);

			GUI.DrawTexture (imageRectangle [8], image1 [8]);
			GUI.DrawTexture (imageRectangle [9], image1 [9]);
			GUI.DrawTexture (imageRectangle [10], image1 [10]);
			GUI.DrawTexture (imageRectangle [11], image1 [11]);

			GUI.DrawTexture (imageRectangle [12], image1 [12]);
			GUI.DrawTexture (imageRectangle [13], image1 [13]);
			GUI.DrawTexture (imageRectangle [14], image1 [14]);
		}

		if (isPressed) {
			GUI.DrawTexture (backgroundImage, image2);
			if (GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 + 100, 105, 105), "Start Game", menuStyle)) {
				isCalled = true;
				isPressed = false;
			}
		}
	}
}

 

 

 

Spoiler

 

Link to comment
https://linustechtips.com/topic/589361-sliding-puzzle-tips-and-help-as-well/
Share on other sites

Link to post
Share on other sites

I'm assuming you're using Unity, which I'm not that experienced in, but I can give you some pseudocode to go off of for the randomization.

Some sort of loop {
	Number = Generate random number
	Switch(number) {
		case 0:
			Add block 1
		case 2:
			Add block 2
		etc.
	}
}

 

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to post
Share on other sites

9 hours ago, dannytech357 said:

I'm assuming you're using Unity, which I'm not that experienced in, but I can give you some pseudocode to go off of for the randomization.


Some sort of loop {
	Number = Generate random number
	Switch(number) {
		case 0:
			Add block 1
		case 2:
			Add block 2
		etc.
	}
}

 

forgot to bump it. i'm already finished the code for the spawn rng. i used a list<>. thanks for the pseudocode though, i appreciated it

 

Spoiler

 

Link to post
Share on other sites

Some updates:

- Only need how do I improve move/swap/slide the blocks.

 

Here's my initial pseudocode. I added block[15] as a null texture and counts as a block

for (int q = 0; q < 15; q++) 
{
		if (imageRectangle [x].position.y == imageRectangle[15].position.y) 
        { // FOR ADJACENT BLOCK-EMPTYBLOCK SLIDES
			imageRectangle [x].y += 75;
			imageRectangle [15].y -= 75;
		}
}

 

 

Spoiler

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×