Skip to content

Instantly share code, notes, and snippets.

@fallazc
Created October 28, 2017 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallazc/2b2074a802dff4ef8e8b5ddb51d8e469 to your computer and use it in GitHub Desktop.
Save fallazc/2b2074a802dff4ef8e8b5ddb51d8e469 to your computer and use it in GitHub Desktop.
Function pour initialiser une grille qui va occuper tout l'espace disponible
/**
*
* {@inheritDoc}
*/
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//Toutes les valeur sont en pixel, donc assure toi de les convertir en dp
//pour ne pas avoir de mauvaise surprise sur differents appareils
final int startX = 20;
final int offset = 20;
int x = startX;
int y = 20;
//Calcule la longueur et largeur d'un rectangle en se tenant en compte
//les parametres ci-dessus
int rectWidth = (w - (2 * x) - offset * (gameModel.N_RECTS_PER_ROW - 1))
/ gameModel.N_RECTS_PER_ROW;
int rectHeight = (h - (2 * y) - offset * (gameModel.N_RECTS_PER_ROW - 1))
/ gameModel.N_RECTS_PER_ROW;
byte maxValue = 9;
Random rand = new Random();
for (int r = 0; r < gameModel.N_RECTS_PER_ROW; r++) {
for (int c = 0, value, xMax, yMax;
c < gameModel.N_RECTS_PER_ROW;
c++) {
value = rand.nextInt(maxValue) + 1;
if(rand.nextBoolean())
value *= -1;
xMax = x + rectWidth;
yMax = y + rectHeight;
gameModel.RECT_LIST[r][c] = new MaxitCell(x, y, xMax, yMax, String.valueOf(value));
x = x + rectWidth + offset;
}
y = y + rectHeight + offset;
x = startX;
}
gameModel.updateHighlighter();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment