private static TreeLinkNode deserializeTree(Pair<int[], Integer> pair) { int[] array = pair.getElement0(); int curIndex = pair.getElement1(); if (curIndex < array.length) { if (array[curIndex] != '#') { TreeLinkNode treeLinkNode = new TreeLinkNode(array[curIndex]); pair.setElement1(curIndex + 1); treeLinkNode.left = deserializeTree(pair); treeLinkNode.right = deserializeTree(pair); return treeLinkNode; } else { pair.setElement1(curIndex + 1); return null; } } else { return null; } }
public void settingBoarders(Pair pair, int countryID) { OwnList activeFields = new OwnList(); OwnList aboutToAdd = new OwnList(); OwnList countryPairs = new OwnList(); double probability = this.countrySize; Random rand = new Random(); activeFields.add(pair); countryPairs.add(pair); while (!activeFields.isEmpty()) { ListIterator<Pair> activeFieldIterator = activeFields.listIterator(); Pair nextPair; while (activeFieldIterator.hasNext()) { nextPair = activeFieldIterator.next(); activeFieldIterator.remove(); // checking up, down, left, right for possible neighbours // up int x = nextPair.getElement0(); int y = nextPair.getElement1() - 1; if (x >= 0 && y >= 0 && x < this.width && y < this.height && puzzle.getCountry(x, y) < 0) { if (rand.nextDouble() <= probability) { aboutToAdd.add(new Pair(x, y)); if (!countryPairs.contains(new Pair(x, y))) { countryPairs.add(new Pair(x, y)); } } } // down x = nextPair.getElement0(); y = nextPair.getElement1() + 1; if (x >= 0 && y >= 0 && x < this.width && y < this.height && puzzle.getCountry(x, y) < 0) { if (rand.nextDouble() <= probability) { aboutToAdd.add(new Pair(x, y)); if (!countryPairs.contains(new Pair(x, y))) { countryPairs.add(new Pair(x, y)); } } } // left x = nextPair.getElement0() - 1; y = nextPair.getElement1(); if (x >= 0 && y >= 0 && x < this.width && y < this.height && puzzle.getCountry(x, y) < 0) { if (rand.nextDouble() <= probability) { aboutToAdd.add(new Pair(x, y)); if (!countryPairs.contains(new Pair(x, y))) { countryPairs.add(new Pair(x, y)); } } } // right x = nextPair.getElement0() + 1; y = nextPair.getElement1(); if (x >= 0 && y >= 0 && x < this.width && y < this.height && puzzle.getCountry(x, y) < 0) { if (rand.nextDouble() <= probability) { aboutToAdd.add(new Pair(x, y)); if (!countryPairs.contains(new Pair(x, y))) { countryPairs.add(new Pair(x, y)); } } } activeFields.remove(nextPair); } // reducing the probability of adding a field for the next iteration probability = probability * probability; ListIterator<Pair> addIterator = aboutToAdd.listIterator(); while (addIterator.hasNext()) { activeFields.add(addIterator.next()); addIterator.remove(); } aboutToAdd.clear(); } // Adding the new country with its ID to the Puzzle Pair[] countryPairsArray = countryPairs.toArray(); puzzle.setCountry(countryPairsArray, countryID); // Randomly pick a Field in the country to define the circle there. The CircleID equals the // CountryID. Value is not set yet. puzzle.setCircle(countryPairsArray[rand.nextInt(countryPairsArray.length)], -1, countryID); }