4

I have this app game which, at the beginning, creates two 3x3 grids of squares. One of my functions setGridDimensions() goes in and assigns the min and max X/Y values for each square in the gird(based on the screen size). This is then used to draw the grid. The strange thing is that sometimes the x or y value of a square will get randomly switched with another square, resulting in holes in my grid. I can't seem to figure out why this is happening. Any help is appreciated. Note I do know the draw function works which is why it is not displayed. Something happens to the x/y values before it is called.

Instances

gameGrid = new Grid(Settings.gridWidth, Settings.gridHeight);
    target = new Grid(Settings.gridWidth, Settings.gridHeight);

This is my code for the function

private void setGridDimensions()
 {
     for (int x=0; x<gameGrid.grid.length; x++)
        {
          for (int y= 0; y< gameGrid.grid.length; y++)
          {
             gameGrid.grid[x][y].minX =  (int)((Settings.WIDTH * .2) + Settings.padding + (Settings.squareWidth * x));
             gameGrid.grid[x][y].maxX = gameGrid.grid[x][y].minX + Settings.squareWidth;
             gameGrid.grid[x][y].minY = Settings.padding + (Settings.squareHeight * y);
             gameGrid.grid[x][y].maxY = gameGrid.grid[x][y].minY + Settings.squareHeight;

             target.grid[x][y].minX = (int)((Settings.WIDTH * .8) + Settings.padding + (Settings.squareWidth/4 * x));
             target.grid[x][y].maxX =  target.grid[x][y].minX + Settings.squareWidth/4;
             target.grid[x][y].minY = Settings.padding + (Settings.squareHeight/4 * y);
             target.grid[x][y].maxY = target.grid[x][y].minY + Settings.squareHeight/4;

             System.out.println("minX " + target.grid[x][y].minX);

          }
        }
     System.out.println("0-0  " + target.grid[0][0].minX);

 }

The output below shows the prints when at least one of the missing squares is in position 0,0 of my array. The 0-0 output should be the same as the first minX output I believe. As you can see the minX value has changed from 600 to 626 (I don't have the rep to post a picture)

OUTPUT

minX 600

minX 600

minX 600

minX 626

minX 626

minX 626

minX 652

minX 652

minX 652

0-0 626

Square Class

    import android.graphics.Color;
class Square
{
  public boolean on = false; //If the square is on or off. For black and white board true = black, white = false
  public int colorOn = Color.BLACK;
  public int colorOff = Color.WHITE;
  public int sideWidth=10;
  public int sideHeight =10;
  public int minX =0;
  public int maxX=10;
  public int minY=0;
  public int maxY=10;

  public Square(boolean isOn, int cOn, int cOff, int sW, int sH)
  {
    on =isOn;
    colorOn = cOn;
    colorOff = cOff;
    sideWidth = sW;
    sideHeight = sH;
  }

  public void changeState()
  {
   on = !on;
  }

}

Grid Class

    import java.util.ArrayList;
    import java.util.Random;
    import android.graphics.Color;
    class Grid
    {
      Square[][] grid; //Contains a list of  gridY 's so grid can be thought of as a sort of gridX
      int gridWidth;
      int gridHeight;
      public int minimumX =0;
      public int maximumX=20;
      public int minimumY=0;
      public int maximumY=20;
      private static Random rand = new Random();
      //private ArrayList <Square> gridY;

      public Grid(int width, int height)
      {
        gridWidth= width;
        gridHeight= height;
        grid = new Square[gridWidth][gridHeight];
        fillWithSquares();
      }
      private void fillWithSquares()
      {

        for(int x =0; x< gridWidth; x++)
        {
         // gridY = new ArrayList<Square>();
            for (int y =0; y < gridHeight; y++)
            {
              //gridY.add(new Square(Settings.squareStartState, Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight));
              Square sqr = new Square(Settings.squareStartState, Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight);
              grid[x][y] = sqr;
            }
         // grid.add(gridY);
        }

  }
  public void fillRandom()
  {
    for(int x =0; x< gridWidth; x++)
    {
        for (int y =0; y < gridWidth; y++)
        {
         // Square sqr = new Square(randBool(), Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight);
          //grid[x][y] = sqr;
          grid[x][y].on = randBool();
        }
    }
  }

  private boolean randBool() //returns a random boolean value
  {
    return rand.nextBoolean();
  }

}

Additional output Output for a grid in with some missing blocks

Another output for different missing block still including 0,0 enter image description here

This is the settings class. Its from a PC version of this as I am temporarily unable to access the Android version of the file. The large majority of it is identical.

    import java.awt.Color;
class Settings
{
  public static int WIDTH = 600;
  public static int HEIGHT =400;
  public static int gridWidth = 3;
  public static int gridHeight = 3;
  public static int templateWidth = 2;
  public static int templateHeight = 2;
  public static Color colorSquareOn = Color.BLUE;
  public static Color colorSquareOff = Color.GREEN;
  public static Boolean squareStartState = false;
  public static int padding = 10;
  public static int gameSideLength = (int) ((WIDTH * .6) -padding); // needs to be changed to addapt based on size
  public static int squareWidth = (int) (((WIDTH * .6) -padding) / gridWidth);
  public static int squareHeight = (int) (((HEIGHT * .6)-padding) / gridHeight);
  public static int overlayPerTemplate = 1;
  public static int numOfTemplates = 4;

}
user1433734
  • 313
  • 1
  • 4
  • 12
  • Just to make sure it's clear, you expect to target.grid[x][y].minx to stay at the original value of 600 the whole time because that is indeed the minimum for the entire grid? – lgraham076 May 28 '14 at 20:41
  • that is the minimum for the object at location 0,0 in target.grid[x][y] There is no reason it should change because the screen size is constant. Also this function is only run once in the onCreate method. – user1433734 May 28 '14 at 20:44
  • can you try putting the "0-0" print next to the "x,y" print so we can get a better idea of WHEN the "0-0" minX value changes to 626. – Uwais A May 28 '14 at 20:54
  • Ok I threw up two additional outputs. As you can see it changes at different times. Also sometimes there is no issue at all. It's the randomness that really has me confused – user1433734 May 28 '14 at 21:05
  • It's not exactly an answer, but try using the debugger to track when minX changes and break at the line when it changes. After doing this you'll at least know the line that is changing the variable and then you can go from there. Use this answer to know how to track: http://stackoverflow.com/a/575368/2014236 – Uwais A May 28 '14 at 22:17
  • @Uwais A Ok so using the debugger It seems to be due to these two lines for x and y respectively. The weird thing was the more I used the debugger the less frequently the error would appear on square 0,0. `target.grid[x][y].maxX = target.grid[x][y].minX + squareWidth/4;` `target.grid[x][y].maxY = target.grid[x][y].minY + squareHeight/4; ` – user1433734 May 29 '14 at 00:05

3 Answers3

0

Try creating variables instead of calling things like Settings.WIDTH or Settings.padding.

Create a variable called width = Settings.WIDTH, padding = Settings.padding, squareWidth = Settings.squareWidth, squareHeight = Settings.squareHeight.

And then the loops would be like this:

for (int x=0; x<gameGrid.grid.length; x++) {
    for (int y= 0; y< gameGrid.grid.length; y++) {
        gameGrid.grid[x][y].minX =  (int)((width * .2) + padding + (squareWidth * x));
        gameGrid.grid[x][y].maxX = gameGrid.grid[x][y].minX + squareWidth;
        gameGrid.grid[x][y].minY = padding + (squareHeight * y);
        gameGrid.grid[x][y].maxY = gameGrid.grid[x][y].minY + squareHeight;

        target.grid[x][y].minX = (int)((width * .8) + padding + (squareWidth/4 * x));
        target.grid[x][y].maxX =  target.grid[x][y].minX + squareWidth/4;
        target.grid[x][y].minY = padding + (squareHeight/4 * y);
        target.grid[x][y].maxY = target.grid[x][y].minY + squareHeight/4;

        System.out.println("minX " + target.grid[x][y].minX);

    }
}

That way you are sure that the variables don't change while the loop is executing.

I don't know what types are Settings.WIDTH, Settings.padding, Settings.squareWidth, Settings.squareHeight, that's why I didn't write the whole function. Try that see how it goes.

ArianJM
  • 676
  • 12
  • 25
  • Ok so that didn't work. I first tried passing the values in. This seemed good, but on the 7th run it broke. Then I hard coded in some values and again the minX value at 0,0 changed mid loop. Interestingly enough after leaving the emulator open for about 1 min on the program it threw a "problem reading network stats" error "Java.lang.IllegalStateException: problem parsing line: null". – user1433734 May 28 '14 at 22:33
0

Not sure this will work but try

float tempMax = target.grid[x][y].minX + Settings.squareWidth/4.f;
target.grid[x][y].maxX = tempMax;

I think I may have had a similar problem in the past with arrays while making a game.

Uwais A
  • 737
  • 6
  • 23
  • Unfortunately, that did not work. Could you elaborate on what your thinking was. I am starting to suspect there must be a flaw in the way I am using array's because I can't find any error in the code itself. – user1433734 May 29 '14 at 19:44
  • It's annoying, I remember solving the problem but can't remember where in my code from a previous project it is. In my project, somehow the two arrays got their values 'tied together' when using the equals sign as you have done. So if maxX was set then minX would be set to the same value (I don't know why). It was a long-shot to be honest, but I too think the problem lies in your usage of arrays, I just can't see it. – Uwais A May 30 '14 at 00:45
  • Just found the problem with my old code - it's slightly different. It's when I wrote `mArray = anotherArray` that the values got tied and produce undesirable results. Still baffled. – Uwais A May 30 '14 at 00:50
  • Ok. So it seems to me that ,similar to your case, the array is causing the issue. I may tryout an arrayList of arrayLists and see if that helps. The syntax is worse but should still work I suppose. – user1433734 May 30 '14 at 19:30
  • Well. I switched all the Arrays for array Lists and it still exhibits the exact same behavior. I'm truly confused. – user1433734 May 30 '14 at 20:19
  • I modified the code to run on a PC to see if it was Android only behavior and it still messes up. – user1433734 Jun 02 '14 at 22:52
  • I'm now trying to make a replica of your setup - could you please post the Settings class as well? – Uwais A Jun 03 '14 at 23:07
  • I posted the settings class. Its from a PC version of this as I am temporarily unable to access the Android version of the file. The large majority of it is identical. I will be able to post the android version in a day or two. – user1433734 Jun 03 '14 at 23:59
  • Hey I actually solved it. The answer is posted. Thank you so much for all the help. If you hadn't helped me eliminate all the possible issue I would never have looked in the function I did. – user1433734 Jun 04 '14 at 00:22
0

Ok I solved it. Apparently in the simple function createTarget() where i gave the target grid random true/false values for each square. (Square.on = true) I had this line

target.grid[pos_x + x][pos_y + y] = templates[selectedTemplate].grid[x][y];
// It should have been this
target.grid[pos_x + x][pos_y + y].on = templates[selectedTemplate].grid[x][y].on;

The reason it worked some of the time was because the squares themselves were copied. This seems to have caused reference issues. By referring directly to the boolean variable the objects don't get linked in any way.

Big shout out to @Uwais A He helped me eliminate all other possible causes, so ,eventually, I had to look in the least likely parts of my code.

user1433734
  • 313
  • 1
  • 4
  • 12
  • Congrats - been stumped on this for days! So is this similar to what I mentioned in a comment, the `mArray = anotherArray` thing, where the two array objects get tied together and can no longer be changed individually? – Uwais A Jun 04 '14 at 00:25
  • @Uwais A Yes I believe so. When you initially posted that I could not find anything along those lines in my code because I kept looking in the wrong place. Thanks again for the help. – user1433734 Jun 04 '14 at 00:38
  • No problem - it took me a week or so to find the problem in my code and I will hopefully NEVER make this mistake again. – Uwais A Jun 04 '14 at 00:45
  • Yeah it's been about that long for me too. Im definitely going to be more careful in the future. – user1433734 Jun 04 '14 at 01:33