Java – Randomly varying variable values. In simple android gameplay

Randomly varying variable values. In simple android gameplay… here is a solution to the problem.

Randomly varying variable values. In simple android gameplay

I have this app game which creates two 3×3 square grids at the beginning. One of my functions setGridDimensions() goes in and assigns minimum and maximum X/Y values (based on screen size) to each square in the grid. It is then used to draw the grid. Strangely, sometimes the x or y value of one square block randomly switches to another side block, causing my mesh to appear hollow. I can’t seem to figure out why this is the case. Any help appreciated.
Note I know the drawing function works, that’s why it doesn’t show. Some changes occur before the x/y value is called.

Instance

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

Here is my function code

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 print when at least one missing square block is located at position 0,0 of my array. I believe the 0-0 output should be the same as the first minX output.
As you can see, the minX value has been changed from 600 to 626 (I have no representative to post the picture).

Output

Minimum 600

Minimum 600

Minimum 600

Minimum 626

Minimum 626

Minimum 626

Minimum X 652

Minimum X 652

Minimum X 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 of a different missing block still includes 0,0
enter image description here

This is the setup class. It’s from the PC version because I can’t access the Android version of the file temporarily. Most of them are the same.

    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;

}

Solution

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

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

Then the loop will look 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);

}
}

This way you can ensure that the variable does not change when the loop executes.

I

don’t know what type is Settings.WIDTH, Settings.padding, Settings.squareWidth, Settings.squareHeight, which is why I didn’t write the entire function. Try it to see how it works.

Related Problems and Solutions