/** Move the snake by one field. */ public void move() { currentDirection = nextDirection; Field newHead = grid.getFromDirection(head, currentDirection); if (newHead.getState().equals(State.TAIL)) { collision.set(true); return; } boolean grow = false; if (newHead.getState().equals(State.FOOD)) { grow = true; } Field lastField = head; for (int i = 0; i < tail.size(); i++) { Field f = tail.get(i); lastField.changeState(State.TAIL); tail.set(i, lastField); lastField = f; } if (grow) { grow(lastField); addPoints(); } else { lastField.changeState(State.EMPTY); } setHead(newHead); }
/** Die Sterne die unlösbar sind werden hier in eine HashMap eingetragen */ public void calculatedUnsolvableStars() { HashMap<Integer, Integer> unsolvableStar = null; _unsolvableStars = new HashMap<Integer, HashMap<Integer, Integer>>(); for (int iY = 0; iY < _oBoard.getHeight(); iY++) { for (int iX = 0; iX < _oBoard.getWidth(); iX++) { unsolvableStar = new HashMap<Integer, Integer>(); Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.STAR && _tmpDiff[iY][iX] != eStatesDiff.ISSTAR) { unsolvableStar.put(iY, iX); _unsolvableStars.put(_unsolvableStars.size(), unsolvableStar); } } } }
public void write() { String bomb = ""; String state = ""; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (field.isBomb(new Point(j, i))) { bomb += "1 "; } else { bomb += "0 "; } state += field.getState(new Point(j, i)) + " "; } bomb += "\n"; state += "\n"; } ta.append("bombの配置\n"); ta.append(bomb); ta.append("各セルのstate\n"); ta.append(state); }
/** * Konstruktor der Klasse CheckEditorBoardDifficulty Das Editor Board wird nach der Schwirigkeit * geprüft * * @return BOARD_DIFFICULTY_EASY = "einfach" - Hier wird angegeben, dass das Board die * Schwirigkeit leicht hat * @return BOARD_DIFFICULTY_MEDIUM = Hier wird angegeben, dass das Board die Schwirigkeit mittel * hat * @return BOARD_DIFFICULTY_HARD = Hier wird angegeben, dass das Board die Schwirigkeit schwer hat * @return BOARD_DIFFICULTY_NOT_SOLVABLE = Hier wird angegeben, dass das Board die Schwirigkeit * unlösbar hat */ public String checkDifficulty() { /** Dekleration der Variabeln für die Funktion checkDifficulty */ _tmpDiff = new eStatesDiff[_oBoard.getHeight()][_oBoard.getWidth()]; boolean isSolvable = false; boolean doMediumSearch = false; boolean doHardSearch = false; boolean isSolved = false; boolean hasChanged = true; int tmpStarCount = 0; int tmpEmptyFields = 0; int counter = 0; boolean foundEmptySpot = false; int foundSpotX = -1; int foundSpotY = -1; int adder = 0; // go through logicuntil game is solved or stop when no changes were made in last run while (!isSolved && hasChanged) { hasChanged = false; for (int iY = 0; iY < _oBoard.getHeight(); iY++) { int rowStars = getCalculatedRowStars(iY, doMediumSearch); for (int iX = 0; iX < _oBoard.getWidth(); iX++) { int columnStars = getCalculatedColumnStars(iX, doMediumSearch); Field oField = _oBoard.getField(iY, iX); if (0 == columnStars || 0 == rowStars || isArrow(oField.getState())) { boolean tmpChanged = updateTmpFields(iY, iX, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } if (!isArrowPointingAtField(iY, iX)) { boolean tmpChanged = updateTmpFields(iY, iX, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } } tmpEmptyFields = 0; for (int iX2 = 0; iX2 < _oBoard.getWidth(); iX2++) { if (_tmpDiff[iY][iX2] != eStatesDiff.BLOCKED && _tmpDiff[iY][iX2] != eStatesDiff.ISSTAR) { tmpEmptyFields++; } } if (tmpEmptyFields == rowStars) { for (int iX2 = 0; iX2 < _oBoard.getWidth(); iX2++) { if (_tmpDiff[iY][iX2] != eStatesDiff.BLOCKED) { boolean tmpChanged = updateTmpFields(iY, iX2, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } } } } } tmpEmptyFields = 0; for (int iX = 0; iX < _oBoard.getWidth(); iX++) { tmpEmptyFields = 0; int columnStars = getCalculatedColumnStars(iX, doMediumSearch); for (int iY = 0; iY < _oBoard.getHeight(); iY++) { if (_tmpDiff[iY][iX] != eStatesDiff.BLOCKED && _tmpDiff[iY][iX] != eStatesDiff.ISSTAR) { tmpEmptyFields++; } } if (tmpEmptyFields == columnStars) { for (int iY2 = 0; iY2 < _oBoard.getWidth(); iY2++) { if (_tmpDiff[iY2][iX] != eStatesDiff.BLOCKED) { boolean tmpChanged = updateTmpFields(iY2, iX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } } } } } // cross calculation for (int iX = 0; iX < _oBoard.getWidth(); iX++) { tmpEmptyFields = 0; for (int iY = 0; iY < _oBoard.getHeight(); iY++) { foundEmptySpot = false; foundSpotX = -1; foundSpotY = -1; Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_SW) { adder = 1; while (((iY + adder) < _oBoard.getHeight()) && ((iX - adder) >= 0)) { if (_tmpDiff[iY + adder][iX - adder] != eStatesDiff.BLOCKED && _tmpDiff[iY + adder][iX - adder] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX - adder; foundSpotY = iY + adder; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } adder++; } } else if (oField.getState() == eStates.ARROW_SE) { adder = 1; while (((iY + adder) < _oBoard.getHeight()) && ((iX + adder) < _oBoard.getWidth())) { if (_tmpDiff[iY + adder][iX + adder] != eStatesDiff.BLOCKED && _tmpDiff[iY + adder][iX + adder] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX + adder; foundSpotY = iY + adder; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } adder++; } } else if (oField.getState() == eStates.ARROW_NW) { adder = 1; while (((iY - adder) >= 0) && ((iX - adder) >= 0)) { if (_tmpDiff[iY - adder][iX - adder] != eStatesDiff.BLOCKED && _tmpDiff[iY - adder][iX - adder] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX - adder; foundSpotY = iY - adder; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } adder++; } } else if (oField.getState() == eStates.ARROW_NE) { adder = 1; while (((iY - adder) >= 0) && ((iX + adder) < _oBoard.getWidth())) { if (_tmpDiff[iY - adder][iX + adder] != eStatesDiff.BLOCKED && _tmpDiff[iY - adder][iX + adder] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX + adder; foundSpotY = iY - adder; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } adder++; } } if (foundEmptySpot) { boolean tmpChanged = updateTmpFields(foundSpotY, foundSpotX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } foundSpotX = -1; foundSpotY = -1; } } } if (doHardSearch) { boolean upCheckDone = false; boolean downCheckDone = false; foundEmptySpot = false; foundSpotX = -1; foundSpotY = -1; for (int iX = 0; iX < _oBoard.getWidth(); iX++) { upCheckDone = false; downCheckDone = false; int columnStars = getCalculatedColumnStars(iX, doHardSearch); if (1 <= columnStars) { // up check for (int iY = 0; iY < _oBoard.getHeight(); iY++) { foundEmptySpot = false; Field oField = _oBoard.getField(iY, iX); if (!upCheckDone && oField.getState() == eStates.ARROW_N) { upCheckDone = true; for (int iTmpY = 0; iTmpY < iY; iTmpY++) { if (_tmpDiff[iTmpY][iX] != eStatesDiff.BLOCKED && _tmpDiff[iTmpY][iX] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX; foundSpotY = iTmpY; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } } if (foundEmptySpot) { boolean tmpChanged = updateTmpFields(foundSpotY, foundSpotX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } foundSpotX = -1; foundSpotY = -1; } } } // down check for (int iY = _oBoard.getHeight() - 1; iY >= 0; iY--) { foundEmptySpot = false; Field oField = _oBoard.getField(iY, iX); if (!downCheckDone && oField.getState() == eStates.ARROW_S) { downCheckDone = true; for (int iTmpY = iY + 1; iTmpY < _oBoard.getHeight(); iTmpY++) { if (_tmpDiff[iTmpY][iX] != eStatesDiff.BLOCKED && _tmpDiff[iTmpY][iX] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iX; foundSpotY = iTmpY; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } } if (foundEmptySpot) { boolean tmpChanged = updateTmpFields(foundSpotY, foundSpotX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } foundSpotX = -1; foundSpotY = -1; } } } } } for (int iY = 0; iY < _oBoard.getHeight(); iY++) { upCheckDone = false; downCheckDone = false; int rowStars = getCalculatedRowStars(iY, doHardSearch); if (1 <= rowStars) { // up check for (int iX = 0; iX < _oBoard.getWidth(); iX++) { foundEmptySpot = false; Field oField = _oBoard.getField(iY, iX); if (!upCheckDone && oField.getState() == eStates.ARROW_W) { upCheckDone = true; for (int iTmpX = 0; iTmpX < iX; iTmpX++) { if (_tmpDiff[iY][iTmpX] != eStatesDiff.BLOCKED && _tmpDiff[iY][iTmpX] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iTmpX; foundSpotY = iY; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } } if (foundEmptySpot) { boolean tmpChanged = updateTmpFields(foundSpotY, foundSpotX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } foundSpotX = -1; foundSpotY = -1; } } } // up check for (int iX = _oBoard.getWidth() - 1; iX >= 0; iX--) { foundEmptySpot = false; Field oField = _oBoard.getField(iY, iX); if (!downCheckDone && oField.getState() == eStates.ARROW_E) { downCheckDone = true; for (int iTmpX = iX + 1; iTmpX < _oBoard.getWidth(); iTmpX++) { if (_tmpDiff[iY][iTmpX] != eStatesDiff.BLOCKED && _tmpDiff[iY][iTmpX] != eStatesDiff.ISSTAR) { if (!foundEmptySpot) { foundSpotX = iTmpX; foundSpotY = iY; foundEmptySpot = true; } else { foundSpotX = -1; foundSpotY = -1; foundEmptySpot = false; break; } } } if (foundEmptySpot) { boolean tmpChanged = updateTmpFields(foundSpotY, foundSpotX, eStatesDiff.ISSTAR); if (!hasChanged) { hasChanged = tmpChanged; } foundSpotX = -1; foundSpotY = -1; } } } } } if (!hasChanged) { boolean tmpChanged = blockSpecialAlgorigthm(); if (!hasChanged) { hasChanged = tmpChanged; } } } isSolved = isSolved(); counter++; if (!isSolved && !doMediumSearch && (counter > 1 || !hasChanged)) { // its medium doMediumSearch = true; hasChanged = true; } if (!isSolved && doMediumSearch && !doHardSearch && (counter > 5 || !hasChanged)) { // its medium doHardSearch = true; hasChanged = true; } } if (isSolved && !doMediumSearch && counter < 2) { return BOARD_DIFFICULTY_EASY; } else if (isSolved && counter < 6) { return BOARD_DIFFICULTY_MEDIUM; } else if (isSolved) { return BOARD_DIFFICULTY_HARD; } else { calculatedUnsolvableStars(); return BOARD_DIFFICULTY_NOT_SOLVABLE; } }
private boolean blockSpecialAlgorigthm() { boolean hasChanged = false; boolean containsUpwardArrow = false; boolean containsDownwardArrow = false; for (int iX = 0; iX < _oBoard.getWidth(); iX++) { System.out.println("1"); int columnStars = getCalculatedColumnStars(iX, true); if (1 == columnStars) { containsUpwardArrow = false; containsDownwardArrow = false; for (int iY = 0; iY < _oBoard.getHeight(); iY++) { System.out.println("2"); Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_N) { containsDownwardArrow = true; } if (oField.getState() == eStates.ARROW_S) { containsUpwardArrow = true; } } if (containsUpwardArrow) { // collect Fields that are Pointed by Arrow for (int iY = _oBoard.getHeight() - 1; iY >= 0; iY--) { System.out.println("3"); Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_S) { for (int iY2 = iY; iY2 >= 0; iY2--) { System.out.println("4"); boolean tmpChanged = updateTmpFields(iY2, iX, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } break; } } } if (containsDownwardArrow) { // collect Fields that are Pointed by Arrow for (int iY = 0; iY < _oBoard.getHeight(); iY++) { System.out.println("5"); Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_N) { for (int iY2 = iY; iY2 < _oBoard.getHeight(); iY2++) { System.out.println("6"); boolean tmpChanged = updateTmpFields(iY2, iX, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } break; } } } } } for (int iY = 0; iY < _oBoard.getHeight(); iY++) { int rowStars = getCalculatedColumnStars(iY, true); if (1 == rowStars) { containsUpwardArrow = false; containsDownwardArrow = false; for (int iX = 0; iX < _oBoard.getWidth(); iX++) { Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_W) { containsDownwardArrow = true; } if (oField.getState() == eStates.ARROW_E) { containsUpwardArrow = true; } } if (containsUpwardArrow) { // collect Fields that are Pointed by Arrow for (int iX = _oBoard.getWidth() - 1; iX >= 0; iX--) { Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_E) { for (int iX2 = iX; iX2 >= 0; iX2--) { boolean tmpChanged = updateTmpFields(iY, iX2, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } break; } } } if (containsDownwardArrow) { // collect Fields that are Pointed by Arrow for (int iX = 0; iX < _oBoard.getWidth(); iX++) { Field oField = _oBoard.getField(iY, iX); if (oField.getState() == eStates.ARROW_W) { for (int iX2 = iX; iX2 < _oBoard.getHeight(); iX2++) { boolean tmpChanged = updateTmpFields(iY, iX2, eStatesDiff.BLOCKED); if (!hasChanged) { hasChanged = tmpChanged; } } break; } } } } } return hasChanged; }