0
\$\begingroup\$

I am making a puzzle game, i made 5X5 cube, lets take example of orange. very left column is orange, upper with orange dot is static and lower with orange dot is also static. inbetween there are 3 orange tile, if i click on one orange tile color gets change on every click, if i click 5 times, five different color will appear as i told earlier. i want to connect the orange dot with orange tile as shown in figure(mark as tick),

I assined all 5 color texture in array having 1 to 5 indexPointer using inspector panel... All tile has same TagName "CubeTag" but object Name is different for all tile.

enter image description here

my question is how to tell the engine that all 3 orange tile is connected. and print "Orange is Connected"

For Example take orange texture:

I want to make a path of orange ..if I click on 3 orange tile (tick mark)then If indexpointer of OrangePath1 == 3 and indexpointer of OrangePath2 == 3 and indexpointer of OrangePath3 == 3 then print ("Orange connected")

Here is my code for assigning all 5 2dTexture array:

 using UnityEngine;
 using System.Collections;

 public class Raycast : MonoBehaviour 
 {
     public Ray ray;
     public RaycastHit hit;
     public Texture2D[] diffuseTextures;
     public int indexPointer = 0;

     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {

             if(Input.GetMouseButtonUp(0)){
                 if(hit.transform.tag == "CubeTag"){
                     hit.collider.gameObject.renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
                     indexPointer++;
                     print (indexPointer);
                     if (indexPointer >= diffuseTextures.Length) {
                         indexPointer = 0;
                     }
                 }
             }
         }
     }
 }
\$\endgroup\$
  • \$\begingroup\$ You can have the board stored in a 2D array and check the adjacent tiles by a neat algorithm or just hard coded as board[x, y + 1] which checks the tile above the current tile... Understand? \$\endgroup\$ – Savlon Nov 24 '14 at 11:49
  • \$\begingroup\$ @Savlon I am new to this, can you show me how to do that? It will be thankfull to you. \$\endgroup\$ – Jamshaid Alam Nov 24 '14 at 15:03
1
\$\begingroup\$

Wouldn't it be simpler to just use some integer state logic to drive the match comparisons?

For example you could just keep track of each Tile's state and colour and do numerical comparisons instead of interrogating the actual texture of each gameObject? This has the added value of using a single Texture Atlas to store your tile textures and then just map to that Atlas as per the norm if a tile needs to change via a user mouse click.

A quick comparison example for you to Ctrl-F5 (if you run it a few times you'll get a match) :

using System;
using System.Collections.Generic;
using System.Text;

namespace CubePuzzle {

    [Flags]
    public enum Colours {
        Red = 0,
        Blue = 2,
        Green = 4,
        Orange = 8,
        Purple = 16,
        Black = 32,
        Grey = 64,
    }

    public class Tile {
        public int xPos; public int yPos;
        public int colourValue;

        public Tile(int x, int y, int colourValue) {
            this.xPos = x; this.yPos = y;
            this.colourValue = colourValue;
        }
    }

    class Program {

        public static void TestForColourMatches(Tile[,] tiles, Array colours, int matchCount) {
            Console.WriteLine();
            int xSize = tiles.GetLength(0);
            int ySize = tiles.GetLength(1);
            bool sequence = false;

            int rowCount = 0;
            // row test
            for (int y = 0; y < ySize; y++) {
                foreach (int c in colours) {
                    rowCount = 0; // reset this value for each colour in each row
                    sequence = false;
                    for (int x = 0; x < xSize; x++) {
                        // test each colour for a row match
                        if (tiles[x, y].colourValue == c) {
                            rowCount++;
                            sequence = true;
                        } else {
                            rowCount = 0;
                            sequence = false;
                        }

                        if (rowCount == matchCount && sequence) {
                            Console.WriteLine("A match was found for colour '" + Enum.GetName(typeof(Colours), c) + " in row [" + y + "].");
                            Console.Write("    --> ");
                            for (int vx = 0; vx < xSize; vx++) {
                                Console.Write(" " + tiles[vx, y].colourValue);
                            }
                            Console.WriteLine("");
                            break;
                        }
                    }

                }
            }

            Console.WriteLine("");

            int colCount = 0;
            // column test
            for (int x = 0; x < xSize; x++) {
                foreach (int c in colours) {
                    colCount = 0; // reset this value for each colour in each column
                    sequence = false;
                    for (int y = 0; y < ySize; y++) {
                        // test each colour for a row match
                        if (tiles[x, y].colourValue == c) {
                            colCount++;
                            sequence = true;
                        } else {
                            colCount = 0;
                            sequence = false;
                        }

                        if (colCount == matchCount && sequence) {
                            Console.WriteLine("A match was found for colour '" + Enum.GetName(typeof(Colours), c) + " in column [" + x + "].");
                            //Console.Write("    --> ");
                            for (int vy = 0; vy < ySize; vy++) {
                                Console.WriteLine("    --> " + tiles[x, vy].colourValue);
                            }
                            Console.WriteLine("");
                            break;
                        }
                    }

                }
            }

            Console.WriteLine("");

        }

        static void Main(string[] args) {

            const int MATCHCOUNT = 3;
            int xSize = 5; int ySize = 5;
            Tile[,] tiles = new Tile[xSize, ySize];
            Random rand = new Random();
            Array colours = Enum.GetValues(typeof(Colours));

            // populate tile set
            for (int y = 0; y < tiles.GetLength(1); y++) {
                Console.Write("[" + y + "] ");
                for (int x = 0; x < tiles.GetLength(0); x++) {
                    tiles[x,y] =  new Tile(x, y, (int)colours.GetValue(rand.Next(colours.Length))); // random colour between (0,4]
                    Console.Write(tiles[x, y].colourValue + " ");
                }
                Console.WriteLine("");
            }

            TestForColourMatches(tiles, colours, MATCHCOUNT);


        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

This will check whether every tile in a column has the same texture. You should be able to modify it to suit your needs.

You also need to set up the board in the inspector. Set the board size to 5, then each column size to 5, then drag over all 25 gameObjects.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Raycast : MonoBehaviour 
{
    public Ray ray;
    public RaycastHit hit;
    public Texture2D[] diffuseTextures;
    public int indexPointer = 0;
    public List<ColumnOfRenderers> board;

    void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit))
        {

            if(Input.GetMouseButtonUp(0)){
                if(hit.transform.tag == "CubeTag"){
                    hit.collider.gameObject.renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
                    indexPointer++;
                    print (indexPointer);
                    if (indexPointer >= diffuseTextures.Length) {
                        indexPointer = 0;
                    }
                    if(AllTilesInColumnMatch())
                        Debug.Log("Tiles are connected");
                }
            }
        }
    }

    bool AllTilesInColumnMatch()
    {
        bool sameTextures = true;
        for(int row = 0; row < 5; row++)
        {
            for(int col = 0; col < 5; col++)
            {
                sameTextures &= TilesHaveSameTexture(row, col, row, col+1);
            }
        }
        return sameTextures;
    }

    bool TilesHaveSameTexture(int x1, int y1, int x2, int y2)
    {
        //check out-of-bounds
        if(y2 < 0 || y2 >= 5)
            return false;

        if(board[x1].column[y1].material.mainTexture == board[x2].column[y2].material.mainTexture)
            return true;
        else
            return false;
    }
}

[System.Serializable]
public class ColumnOfRenderers
{
    public List<Renderer> column = new List<Renderer>();
}
\$\endgroup\$
0
\$\begingroup\$

From your example I can tell you're making a "connect the pipes" type game. You have two possible approaches here:

  1. You can hard code each level, testing the map array for a specific total state, making sure that A2, A3, and A4 are all orange.
  2. You can program a pathing algorithm to test if there is a path between the two colors.

The second option will be better for you in the long run, as it means less work for you to design new levels.

Do a little research for finding paths in an array between two points. It's an old problem with many solutions. The most optimal solution, if my memory serves me, is the A* (A-star) algorithm.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.