Search Unity

Question update an 8x8 grid with 0s and 1s

Discussion in 'Scripting' started by julien83franceschi, Feb 21, 2024.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    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 :

    [code = CSharp] 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);
    }[/code]

    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+
     
    Last edited: Feb 21, 2024
  2. BrianLH369

    BrianLH369

    Joined:
    Feb 26, 2022
    Posts:
    6
    Hi,
    I dont know exacly what gridRouge.grid[i, j] is and how its updated but if you print Grid it gives ones.
    Code (CSharp):
    1. row += Grid[i, j] + " ";
     
    Kurt-Dekker likes this.
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    Hello,

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

    Your help is most welcome,

    A+
     
  4. BrianLH369

    BrianLH369

    Joined:
    Feb 26, 2022
    Posts:
    6
    This should print Grid array values.
    This should print Grid array values.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class grid8x8test : MonoBehaviour
    4. {
    5.     int[,] Grid = new int[8, 8];
    6.     int[,] rectangle3D = new int[5, 5]
    7.     {
    8.         {1, 1, 1, 1, 1},
    9.         {0, 0, 0, 0, 0},
    10.         {0, 0, 0, 0, 0},
    11.         {0, 0, 0, 0, 0},
    12.         {0, 0, 0, 0, 0}
    13.     };
    14.  
    15.     void OnMouseUp()
    16.     {
    17.         UpdateGridWithPentaminoX(3, 2);
    18.         PrintGrid();
    19.     }
    20.  
    21.     void UpdateGridWithPentaminoX(int row, int col)
    22.     {
    23.         for (int i = 0; i < 5; i++)
    24.             for (int j = 0; j < 5; j++)
    25.                 if (rectangle3D[i, j] == 1)
    26.                 {
    27.                     //gridRouge.grid[row + i, col + j]=1;
    28.                     Grid[row + i, col + j] = 1;
    29.                 }
    30.     }
    31.  
    32.     void PrintGrid()
    33.     {
    34.         for (int i = 0; i < 8; i++)
    35.         {
    36.             string row = "";
    37.             for (int j = 0; j < 8; j++)
    38.             {
    39.                 //row += gridRouge.grid[i, j] + " ";
    40.                 row += Grid[i, j] + " ";
    41.             }
    42.             Debug.Log(row);
    43.         }
    44.     }
    45. }
     
  5. BrianLH369

    BrianLH369

    Joined:
    Feb 26, 2022
    Posts:
    6
    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.
     
  6. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    I tried the above code, but it doesn't work.

    It doesn't update the grid with 1s.

    If you have any ideas,

    A+
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    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.

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

    This looks like the same problem as you posted before here.
     
    cdr9042 and Kurt-Dekker like this.
  8. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    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).

    Code (CSharp):
    1. using UnityEngine ;
    2. public class grid8x8test : MonoBehaviour
    3. {
    4.     int[,] Grid = new int[8, 8] ;
    5.     int[,] rectangle3D = new int[5, 5]
    6.     {
    7.         {0, 1, 0, 0, 0},
    8.         {1, 1, 1, 0, 0},
    9.         {0, 1, 0, 0, 0},
    10.         {0, 0, 0, 0, 0},
    11.         {0, 0, 0, 0, 0}
    12.     } ;
    13.     void OnMouseUp()
    14.     {
    15.         UpdateGridWithPentaminoX(3, 2) ;
    16.         PrintGrid() ;
    17.     }
    18.     void UpdateGridWithPentaminoX(int row, int col)
    19.     {
    20.         for (int i = 0 ; i < 5 ; i++)
    21.             for (int j = 0 ; j < 5 ; j++)
    22.                 if (rectangle3D[i, j] == 1)
    23.                 {
    24.                     //gridRouge.grid[row + i, col + j]=1 ;
    25.                     Grid[row + i, col + j] = 1 ;
    26.                 }
    27.     }
    28.     void PrintGrid()
    29.     {
    30.         for (int i = 0 ; i < 8 ; i++)
    31.         {
    32.             string row = "" ;
    33.             for (int j = 0; j < 8; j++)
    34.             {
    35.                 //row += gridRouge.grid[i, j] + " " ;
    36.                 row += Grid[i, j] + " " ;
    37.             }
    38.             Debug.Log(row) ;
    39.         }
    40.     }
    41. }
    If you can improve my code,

    A+
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    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.
     
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    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.
     
  11. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    I used the debugger in the following code:

    Code (CSharp):
    1.  void PrintGrid()
    2.     {
    3.         for (int i = 0 ; i < 8 ; i++)
    4.         {
    5.             string row = "" ;
    6.             for (int j = 0; j < 8; j++)
    7.             {
    8.                 row += Grid[i, j] + " " ;
    9.             }
    10.             Debug.Log(row) ;
    11.         }
    12.     }
    But this one only displays 0s (see image below).



    Thanks for your help.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    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.
     
  13. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    The debugger finds no errors (under Visual Studio).

    Thank you very much,

    A+
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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
     
  15. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    233
    As I've already said, I'm using Debug.Log here:

    Code (CSharp):
    1. void PrintGrid()
    2. {
    3. for (int i = 0; i < 8; i++)
    4. {
    5. string row = "";
    6. for (int j = 0; j < 8; j++)
    7. {
    8. row += Grid[i, j] + " ";
    9. }
    10. Debug.Log(row);
    11. }
    12. }
    The problem is that it returns only 0s.

    Can you help me?

    A+
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    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”.
     
    Bunny83 likes this.
  17. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    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?
     
    MelvMay likes this.