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
Another output for different missing block still including 0,0
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;
}