Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Alp-Giray-Savrum · Mar 20, 2017 at 03:39 PM · arraygridmatrixalgorithmfill

Approach for filling 6x6 matrix with shapes

Hello, guys. I'm trying to develop a Tetris-like blocks game. I've got 6x6 unit "module" that filled with shapes like this;

alt text

I thought that i should create an 6x6 matrix and fill it with my blocks. Thus far I've developed an approach like this,

 matrix = 
 [0,0,0,0,0,0]
 [0,0,0,0,0,0]
 [0,0,0,0,0,0]
 [0,0,0,0,0,0]
 [0,0,0,0,0,0]
 [0,0,0,0,0,0]
 
 shapes //For example L
 L =
 [1,0]
 [1,0]
 [1,1]

Also I'll need a filling algorithm that places shapes and fills empty spaces. I don't know if there's an example for this. Everything will be appreciated.

4th Grade Senior Math, please be gentle :)

unity-2017-03-20-18-26-03.png (30.3 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Mar 20, 2017 at 09:08 PM 0
Share

how do you want to fill it? without missing a spot? with predefined shapes?

avatar image Alp-Giray-Savrum hexagonius · Mar 21, 2017 at 09:04 AM 0
Share

Yes exactly like this. I've got 24 shapes to fill with and i want to fill randomly.

avatar image hexagonius Alp-Giray-Savrum · Mar 21, 2017 at 09:51 PM 0
Share

ok, then I'm out. I have no idea how to code that all of those blocks fit leaving no space at all. I used to have a real puzzle toy challenging you with exactly this kind of task and I never found the underlying system.

but I found this:

http://gamedev.stackexchange.com/questions/87971/how-can-i-ensure-a-grid-can-be-filled-with-tetris-like-pieces

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by toddisarockstar · Mar 22, 2017 at 06:28 AM

     public int[][] grid;
     public Vector2[][] pieces;
     int i;
     int i2;
     
     public Texture2D dis;
 
     public Color[] col;
 
     int numberplaced;
     
     int squaresplaced;
 
     int attempts;
     int maxattempts;
 
     int gridsize;
     
     void Start () {
 
         maxattempts = 10000;//this is max number of times the script will try to fill every square!!! 
 
 
         //we need colors for our pieces
         col = new Color[5];
         col [0] = Color.red;
         col [1] = Color.green;
         col [2] = Color.blue;
         col [3] = Color.yellow;
         col [4] = Color.cyan;
         int bx;
         int by;
         int i;
 
 
 
 
 
                 //an make an array of arrays to represent our pieces 
                 pieces = new Vector2[5][];
 
                 //inside the array of pieces i make an array to stick coordinates for the shape of the peices
                 //each piece is going to need local coordinates of its own blocks to represent its shape;
 
                  
                 //make a red dot
                 pieces [0] = new Vector2[1];
                 pieces [0] [0] = new Vector2 (0, 0);
                  
 
                 //make a green square
                 pieces [1] = new Vector2[4];
                 pieces [1] [0] = new Vector2 (0, 0);
                 pieces [1] [1] = new Vector2 (0, 1);
                 pieces [1] [2] = new Vector2 (1, 0);
                 pieces [1] [3] = new Vector2 (1, 1);
 
                 //make a blue line
                 pieces [2] = new Vector2[4];
                 pieces [2] [0] = new Vector2 (0, 0);
                 pieces [2] [1] = new Vector2 (0, 1);
                 pieces [2] [2] = new Vector2 (0, 2);
                 pieces [2] [3] = new Vector2 (0, 3);
 
         
         
         //make a yellow L
                 pieces [3] = new Vector2[5];
                 pieces [3] [0] = new Vector2 (0,0);
                 pieces [3] [1] = new Vector2 (0,1);
                 pieces [3] [2] = new Vector2 (0,2);
                 pieces [3] [3] = new Vector2 (1,0);
                 pieces [3] [4] = new Vector2 (2,0);
 
         //make a small light blue line
         pieces [4] = new Vector2[2];
         pieces [4] [0] = new Vector2 (0, 0);
         pieces [4] [1] = new Vector2 (1, 0);
         //pieces [4] [2] = new Vector2 (2, 0);
 
         
         
 
 
         gridsize = 6;
 
         //we will keep looping random attempts to fill all squares !!!!
 
         while(squaresplaced<gridsize*gridsize){
             squaresplaced=0;
             attempts++;
             if(attempts>maxattempts){squaresplaced=gridsize*gridsize;}
 
             //now to create the grid i 
             //create an array of arrays; (array of 6 each containg 6 arrays;
             //so that we have values that can be checked with 2 coordinates;
         grid = new int[gridsize][];
         i = grid.Length;
         
         while (i>0) {i--;
             grid [i] = new int[gridsize];
         }
         // now here is where things get a bit crazy
                 // first of all i need to loop through all our pieces right?
                 numberplaced = pieces.Length;
                 while(numberplaced==pieces.Length){
                 numberplaced=0;
                 int p = pieces.Length;
                 while (p>0) {p--;
 
 
                     int xpad=0;
                     int ypad=0;
                     //find the size of our piece to make sure we dont try to 
                     //place it where it hangs off the grid
                     i=pieces[p].Length;
                     while(i>0){i--;
                         if(xpad<pieces[p][i].x){xpad=Mathf.FloorToInt(pieces[p][i].x);}
                         if(ypad<pieces[p][i].y){ypad=Mathf.FloorToInt(pieces[p][i].y);}
                     }
                         //lets choose a random coordinate to start with
                         
                         int gridy=Random.Range(0,grid.Length-ypad);
                         int gridx=Random.Range(0,grid[0].Length-xpad);
                        
                         //another number that stops the loop after checking all the squares
                         int count = grid.Length*grid[0].Length;
                         //print ("count="+count);
                         
                         //now i need a loop inside a loop
                         //to page through every sqaure in my grid
                         //starting with our random start position
 
                           while(count>0){
                                  while(gridx>-1){
                                  count--;
                                  
 //here we check through all our piece coordinates against the grid to see if they fit
                                  if(gridx+xpad<grid[0].Length){
                                  if(gridy+ypad<grid.Length){
                                  bool placed=true;
                                  int b=pieces[p].Length;
                                  while(b>0){b--;
                                  bx = Mathf.FloorToInt(pieces[p][b].x);
                                  by = Mathf.FloorToInt(pieces[p][b].y);
 
                                  //if(gridx+bx>=grid[0].Length){placed=false;}
                                  //if(gridy+by>=grid.Length){placed=false;}
                                  
                                  if(placed==true){
                                  if(grid[gridx+bx][gridy+by]!=0){placed=false;}}
                                  }
 
                                  //if successfull we will mark it on the grid
                                  if(placed==true){
                                  
                                  numberplaced++;
                                  b=pieces[p].Length;
                                  while(b>0){b--;
                                  bx = Mathf.RoundToInt(pieces[p][b].x);
                                  by = Mathf.RoundToInt(pieces[p][b].y);
                                  squaresplaced++;
                                  grid[gridx+bx][gridy+by]=p+1;
                                  
                                  }
                         count=0;
                         gridx=-1;
                     }
                             }}
                     

                                  
                         gridx--;     
                         }
                        
                         gridx=grid[0].Length-1;
                           
 
                 if(gridy<1){gridy=grid.Length;}
                 gridy--; 
                 }
     
         }
     }
         //print ("squares placed:" + squaresplaced);
     }
         i = gridsize * gridsize;
         if (squaresplaced < i + 1) {
                         print ("i filled " + squaresplaced + "out of " + i + "squares with your peices without overlap");}
         print ("i used " + attempts + "random attempts to get this!!!!"); 
         if (squaresplaced != i) {print ("if you increase my Max attempts i will fill them all!!!");}
 
 
 
 
         //I added this at the bottome just to display what is going on!!!!
         dis = new Texture2D (grid.Length, grid [0].Length);
         i =dis.width;
 
         while (i>0) {i--;
             i2 = dis.height;
             while(i2>0){i2--;
                 if(grid[i][i2]>0){dis.SetPixel(i,i2,col[grid[i][i2]-1]);}
                 else{dis.SetPixel(i,i2,Color.clear);}
             }}
         dis.Apply ();
         dis.filterMode = FilterMode.Point;
 
     }
     void OnGUI(){
         GUI.DrawTexture (new Rect(0, 0, 300, 300), dis);
     }


first all you need a grid which is a two dimentional array, or an array inside an array. so we have 36 variables that can be accessed with coordinates.

then we need the coordinates to represet the shape of the pieces for checking purposes since each piece has multiple coordinates and we have multiple pieces we use another array of arrays for the pieces!

the script i wrote starts with a peice and trys to randomly place it. if it doesen't fit it continues to try every other place on the grid. If successful, it trys the same process with the next piece untill it cant find a proper placement.

if all the squares in the grid are full it stops. if all the squares are not full. it erases and trys again till it gives you what you want.

the script allways uses the same amounts of peices so depending on size / shape of your peices there is a chance that is either impossible to fit them or there could be very few possible outcomes.

the little grid is simple to figure out for the PC. if you where using a bigger grid you would pry need to implement a little AI such as checking ahead of itself before placing the blocks or starting with big peices and using small ones last.

Anyways, this code produces your random outcome just fine on a little 6*6 grid !

you can take the time to change the number and shape of the peices as you need i just gave it a few basics for example

if i was in your situation i would just run this code for a good amount of time and store all the possiblitys. then use the outputs in your game assets instead of the user running this!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Alp-Giray-Savrum · Mar 22, 2017 at 05:58 PM

Thanks a lot, I'll try to implement this code to my game !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slow flood fill of sprite c# 2 Answers

3d grid/matrix inside a mesh 1 Answer

C# Multidimensional arrays 0 Answers

Connecting Pods/Nodes together on a grid using pathfinding? 0 Answers

Calculate the new coordinates of points after the plane of the points is transformed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges