update an 8x8 grid with 0s and 1s

Salut tout le monde,

J’ai créé une grille 8x8 composée de petits avions rouges.
Et j’aimerais mettre à jour un tableau int Grid = new int [8, 8] avec des 1 lorsque nous déposons le rectangle 3D (composé de 5 cubes horizontaux) dans la grille.
J’utilise également un tableau int rectangle3D = new int [5, 5] comme indiqué ci-dessous :

int[,] rectangle3D = nouveau int[5, 5] {
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
} ;
Et voici le code qui met à jour la grille :

 void UpdateGridWithPentaminoX (ligne int, col int)
    {
         pour (int i = 0; i < 5; i++)
           pour (int j = 0; j < 5; j++)
            si (rectangle3D[i, j] == 1)
            {
              Grille[ligne + i, col + j] = 1;
            }
    }
    annuler OnMouseUp()
    {
       UpdateGridWithPentaminoX(3, 2);
       ImprimerGrid();
    }
annuler PrintGrid()
      {
      pour (int i = 0; i < 8; i++)
        {
           ligne de chaîne = "" ;
          pour (int j = 0; j < 8; j++)
         {
           row += Grille[i, j] + " ";
         }
           Debug.Log(ligne);
         }

Le problème est que Debug.Log ne renvoie que des 0 et aucun 1 (voir image).

Si quelqu’un a des idées pour mettre des 0 et des 1 dans ma grille,

ce serait d’une grande aide (j’utilise UpdateGridWithPentaminoX (3, 2) mais ça ne marche pas),

A+

Hi,
I dont know exacly what gridRouge.grid[i, j] is and how its updated but if you print Grid it gives ones.

row += Grid[i, j] + " ";
1 Like

Hello,

I’ve corrected your comment.
But I still can’t update the grid (with 0s and 1s).

Your help is most welcome,

A+

This should print Grid array values.
This should print Grid array values.

using UnityEngine;

public class grid8x8test : MonoBehaviour
{
    int[,] Grid = new int[8, 8];
    int[,] rectangle3D = new int[5, 5]
    {
        {1, 1, 1, 1, 1},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0}
    };

    void OnMouseUp()
    {
        UpdateGridWithPentaminoX(3, 2);
        PrintGrid();
    }

    void UpdateGridWithPentaminoX(int row, int col)
    {
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
                if (rectangle3D[i, j] == 1)
                {
                    //gridRouge.grid[row + i, col + j]=1;
                    Grid[row + i, col + j] = 1;
                }
    }

    void PrintGrid()
    {
        for (int i = 0; i < 8; i++)
        {
            string row = "";
            for (int j = 0; j < 8; j++)
            {
                //row += gridRouge.grid[i, j] + " ";
                row += Grid[i, j] + " ";
            }
            Debug.Log(row);
        }
    }
}

However if gridRouge.grid is a grid in another script you are using and you want that grid to be populated with values, then use the lines that are commented out.

I tried the above code, but it doesn’t work.

It doesn’t update the grid with 1s.

If you have any ideas,

A+

Have you tried debugging this by attaching a debugger, placing breakpoints and stepping through it? If not, you should do that rather than making incremental changes based on what others are guessing. You have the project right in front of you so it should be quick to determine what is going wrong.

This looks like the same problem as you posted before here .

2 Likes

Hi,

This code seems to work fine, but in reality it doesn’t.
When debugging, the console doesn’t display a 1 in the grid in row 3 and column 2 (I’ve changed the array of the 3D rectangle).

using UnityEngine ;
public class grid8x8test : MonoBehaviour
{
    int[,] Grid = new int[8, 8] ;
    int[,] rectangle3D = new int[5, 5]
    {
        {0, 1, 0, 0, 0},
        {1, 1, 1, 0, 0},
        {0, 1, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0}
    } ;
    void OnMouseUp()
    {
        UpdateGridWithPentaminoX(3, 2) ;
        PrintGrid() ;
    }
    void UpdateGridWithPentaminoX(int row, int col)
    {
        for (int i = 0 ; i < 5 ; i++)
            for (int j = 0 ; j < 5 ; j++)
                if (rectangle3D[i, j] == 1)
                {
                    //gridRouge.grid[row + i, col + j]=1 ;
                    Grid[row + i, col + j] = 1 ;
                }
    }
    void PrintGrid()
    {
        for (int i = 0 ; i < 8 ; i++)
        {
            string row = "" ;
            for (int j = 0; j < 8; j++)
            {
                //row += gridRouge.grid[i, j] + " " ;
                row += Grid[i, j] + " " ;
            }
            Debug.Log(row) ;
        }
    }
}

If you can improve my code,

A+

What does “seems to work fine” mean?

Then do as I suggested above, debug it by attaching the debugger. Just dumping out the result isn’t finding out why it’s not working.

You need to debug it propertly to find the problem rather than devs “improve” (fix) it.

I am not trying to get in your way here but the above problem can be solved by you debugging this correctly, stepping though it and looking if things are changing as you expect.

I just tried the code myself, online in the .net fiddle. It does work as expected. So you either adapted the code to your project wrongly, or you have some other aspect in your project we don’t know about.

It generally helps when you print more unique logs so they can actually be distinguished from one another. Some prefix text usually helps. Also make sure your logs actually originated from this script and not some other place.

ps: ahh update ^^

Well, the code you provided does work. However it should be clear that you will run into issues in a “real” tetris game. First of all, you don’t check or clamp the bounds of the arrays. Since your target array is 8x8 and your pieces array is 5x5, the only valid indices you could pass to UpdateGridWithPentaminoX is 0,1,2 and 3. That’s it. Any value greater than those would throw an exception. Your pieces array does not know the “bounds”. So it’s not clear which parts of your array are competely empty. However you need to store that information in order to apply proper bounding. In your new example your piece only requires a space of 3x3, so the loops should be adjusted accordingly. Each “Pentamino” should probably be a class instance where you store the relevant information bundled together. So the actual grid that defines the shape as well as the row and column count to indicate how much space is used by this piece.

Of course rotation would be another issue that has to be taken into account as a 90° rotation would flip the row / column logic.

I used the debugger in the following code:

 void PrintGrid()
    {
        for (int i = 0 ; i < 8 ; i++)
        {
            string row = "" ;
            for (int j = 0; j < 8; j++)
            {
                row += Grid[i, j] + " " ;
            }
            Debug.Log(row) ;
        }
    }

But this one only displays 0s (see image below).

Thanks for your help.

Using Debug.Log is not “the debugger”, as I’ve said several times above, attach a debugger (like Visual Studio, JetBrains Rider etc) and step through your code line by line. See the link I provided above on how to do that.

You’ll be able to stop, inspector all of your Grid array or anything else.

The debugger finds no errors (under Visual Studio).

Thank you very much,

A+

Debuggers don’t find errors.

Debuggers let YOU see each step of what the code does, and to inspect data at any point in the flow.

By seeing what happens you can reason about your problem better and hopefully fix it.

The first place you should debug is the piece of code that copies the 1 values over.

https://docs.unity3d.com/2021.1/Documentation/Manual/ManagedCodeDebugging.html

As I’ve already said, I’m using Debug.Log here:

void PrintGrid()
{
for (int i = 0; i < 8; i++)
{
string row = "";
for (int j = 0; j < 8; j++)
{
row += Grid[i, j] + " ";
}
Debug.Log(row);
}
}

The problem is that it returns only 0s.

Can you help me?

A+

And as we’ve said several times, this is NOT the debugger. This is writing to the console.

You’ve posted the same code block twice now stating you’re using Debug.Log. We can read and understand that. Unfortunately you’re not reading/understanding the replies here so it’s very difficult to help you.

Please read the link provided a few times now. Attach a debugger or go to youtube and search “unity debugger visual studio”.

1 Like

I quickly searched the internet for an example usage of the debugger in Unity( timestamp 1m48s). I found a video, though it’s in english, so I’m not sure if there will be a language barrier but you may want to give it a try or search for one in french?

1 Like