ランダムな(0と2の間の)数で二次元配列を塗りつぶすアルゴリズムを行う必要があります。唯一の条件は、同じ数を水平方向と垂直方向に3回以上持つことができることです。例なぜ私のアルゴリズムはstackoverflow例外を返すのですか?
[0,0,1,2,1
0,1,2,1,2
1,2,0,1,0]
については は
[0,0,0,2,1
0,1,2,1,2
1,2,0,1,0]
または
[0,0,1,2,1
0,1,1,1,2
1,2,1,1,0]
が間違ってokです。
public class CandyHelper {
private final int minimumChainLength = 3;
private final int searchChainSize = 3;
public Candy[][] randomInit(int rowCount, int columnCount) {
Candy[][] map = new Candy[rowCount][columnCount];
for (int row = 0; row < rowCount; ++row) {
for (int column = 0; column < columnCount; ++column) {
// Fill square with random candy.
Random rand = new Random();
int value = rand.nextInt(3);
Candy candy = new Candy();
candy.setType(value);
map[row][column] = candy;
}
}
if (findHint(map)) {
//System.out.println("Winning conditions");
map = null;
map = randomInit(rowCount, columnCount);
} else {
System.out.println("no wining");
}
return map;
}
// Function which searches for match of at least `MinimumChainLength`.
private boolean findHint(Candy[][] map) {
int rowCount = map.length;
int columnCount = map.length;
List<Candy> hintMove = new ArrayList<Candy>();
// Search rows.
for (int row = 0; row < rowCount; ++row) {
// Search for chain.
for (int chainStart = 0; chainStart < columnCount - searchChainSize; ++chainStart) {
// Add initial cell in chain.
hintMove.clear();
hintMove.add(map[row][chainStart]);
for (int nextInChain = chainStart + 1; nextInChain < columnCount; ++nextInChain) {
if (map[row][nextInChain].getType() == hintMove.get(0).getType()) {
hintMove.add(map[row][nextInChain]);
} else {
break;
}
}
// Was a chain found?
if (hintMove.size() >= minimumChainLength)
return true;
}
}
// Search columns.
for (int column = 0; column < columnCount; ++column) {
// Search for chain.
for (int chainStart = 0; chainStart < rowCount - searchChainSize; ++chainStart) {
// Add initial cell in chain.
hintMove.clear();
hintMove.add(map[chainStart][column]);
for (int nextInChain = chainStart + 1; nextInChain < rowCount; ++nextInChain) {
if (map[nextInChain][column].getType() == hintMove.get(0).getType()) {
hintMove.add(map[nextInChain][column]);
} else {
break;
}
}
// Was a chain found?
if (hintMove.size() >= minimumChainLength)
return true;
}
}
// No chain was found, so clear hint.
hintMove.clear();
return false;
}
}
と私のPOJO:
だからここに私のアルゴリズムである
public class Candy {
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "Candy{" + "type=" + type + '}';
}
}
私は、スタックオーバーフローエラーを取得を開始10×10の配列から開始します。 修正するにはどうすればよいですか?
ありがとうございます。
スタックトレース全体を投稿してください – Frakcool
スタックオーバーフローは、自分自身を何度も呼び出すメソッドを探して、スタックが使い果たされるまで、スタックにさらに多くのフレームを追加します。それを探してください。 (PS - "Candy"?これはあなたの文脈に意味がありますか?) – duffymo
問題ではありませんが、生成する数字ごとに新しい 'ランダム'を作成したくありません。 – John3136