/** * 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)); } } } }
private void onFieldClick(Field f) { if (f == null || fieldCreationFragment == null || fieldCreationFragment.getZahlenschlange() == null) { return; } if (clickCount < MAX_CLICKS) { clickCount++; } updateClickCount(); Zahlenschlange gameLogic = fieldCreationFragment.getZahlenschlange(); int index = f.getIndex(); int y = index / difficulty; index = index % difficulty; int x = index; Position end = gameLogic.getPathEnd(); if (end.getX() == x && end.getY() == y) { gameLogic.undo(); updateGridView(); return; } Result res = gameLogic.makeStepTo(x, y); if (res.isOk() || res.isGameFinished()) { updateGridView(); if (res.isGameFinished()) { finishGame(true); } } else if (res.isNotAllowed()) { showToast(getString(R.string.operation_not_allowed)); } }