/** Updates the gridLayout with the actual game data if existing. */ private void updateGridView() { if (fieldCreationFragment == null || fieldCreationFragment.getZahlenschlange() == null) { return; } Zahlenschlange gameLogic = fieldCreationFragment.getZahlenschlange(); int index = 0; for (int y = 0; y < difficulty; y++) { for (int x = 0; x < difficulty; x++) { View v = gameSquareGridLayout.getChildAt(index); if (v instanceof Field) { Field f = (Field) v; f.setNumber(gameLogic.getValue(x, y)); f.setDirection(Field.DIRECTION_NONE); f.setDirection2(Field.DIRECTION_NONE); f.setBodyStyle(Field.BODY_NORMAL); } index++; } } Field first = (Field) gameSquareGridLayout.getChildAt(0); Field last = (Field) gameSquareGridLayout.getChildAt(index - 1); first.setBodyStyle(Field.BODY_TAIL); last.setBodyStyle(Field.BODY_HEAD); drawPath(gameLogic.getPath()); }
/** * Draws the given path on the gridLayout's fields. * * @param path The path to draw */ private void drawPath(List<Position> path) { for (int i = 0; i < path.size(); i++) { Position p = path.get(i); int index = p.getX() + p.getY() * difficulty; View v = gameSquareGridLayout.getChildAt(index); if (v != null && v instanceof Field) { Field f = (Field) v; Position before = null; Position after = null; if (index == 0) { f.setBodyStyle(Field.BODY_TAIL); } else if (index == (difficulty * difficulty) - 1) { f.setBodyStyle(Field.BODY_HEAD); } else { f.setBodyStyle(Field.BODY_NORMAL); } if (i > 0) { before = path.get(i - 1); } if (i < path.size() - 1) { after = path.get(i + 1); } int xDif; int yDif; if (before != null) { xDif = p.getX() - before.getX(); // -Rechts +Links yDif = p.getY() - before.getY(); // -Unten +Oben f.setDirection(getDirectionFromDif(xDif, yDif)); } if (after != null) { xDif = p.getX() - after.getX(); // -Rechts +Links yDif = p.getY() - after.getY(); // -Unten +Oben f.setDirection2(getDirectionFromDif(xDif, yDif)); } } } }
/** * Change the squareGrids's dimension * * @param dimension Dimension the squareGrid should have. */ private void setGridToSize(int dimension) { gameSquareGridLayout.removeAllViews(); gameSquareGridLayout.setColumnCount(dimension); gameSquareGridLayout.setRowCount(dimension); for (int y = 0; y < dimension; y++) { for (int x = 0; x < dimension; x++) { Field tmpField = new Field(this); tmpField.setIndex(x + y * dimension); tmpField.setOnClickListener(this); tmpField.setBackgroundColor(Color.WHITE); // Update border tmpField.setBorderWidth(getDIP(1)); // Update border gameSquareGridLayout.addView(tmpField); } } }