-1
import java.lang.reflect.Array;
import java.util.*;
public class TMS {
private static int row = 5, col = 5;
private static String[][] board = new String[row][col];
private static String[][] board_copy = new String[row][col];
public static void main(String[] args) {
for(String[] array:board) {
Arrays.fill(array, "_");
}
create_mines();
Scanner input = new Scanner(System.in);
int inpx = 0, inpy;
while(inpx != 69){
show_board();
inpx = input.nextInt();
inpy = input.nextInt();
if(board_copy[inpx][inpy] == "*"){
System.out.println("YOU LOOSE");
break;
}
check_move(inpx, inpy);
}
}
public static void show_board() {
for(String[] row: board){
for(String element: row){
System.out.print(element+"\t");
}System.out.println();
}
}
public static void create_mines() {
Random rand = new Random();
rand.nextInt();
for(String[] array:board_copy) {
Arrays.fill(array, "_");
}
board_copy[1][1] = "*";
board_copy[3][1] = "*";
board_copy[3][3] = "*";
board_copy[2][4] = "*";
}
public static void check_move(int posx, int posy){
int mines = 0;
if(posx-1 >= 0 && posy-1 >= 0)
mines = (board_copy[posx-1][posy-1] == "*")? (mines+1):mines;
if(posx >= 0 && posy-1 >= 0)
mines = (board_copy[posx][posy-1] == "*")? (mines+1):mines;
if(posx+1 < row && posy+1 < col)
mines = (board_copy[posx+1][posy+1] == "*")? (mines+1):mines;
if(posx-1 >= 0 && posy >= 0)
mines = (board_copy[posx-1][posy] == "*")? (mines+1):mines;
if(posx+1 < row && posy >= 0)
mines = (board_copy[posx+1][posy] == "*")? (mines+1):mines;
if(posx >= 0 && posy+1 < col)
mines = (board_copy[posx][posy+1] == "*")? (mines+1):mines;
if(posx-1 >= 0 && posy+1 < col)
mines = (board_copy[posx-1][posy+1] == "*")? (mines+1):mines;
if(posx+1 < row && posy-1 >= 0)
mines = (board_copy[posx+1][posy-1] == "*")? (mines+1):mines;
board[posx][posy] = Integer.toString(mines);
if(mines == 0){
if((posx-1) >= 0 && (posy-1) >= 0) {
System.out.println((posx-1)+" "+(posy-1));
check_move((posx - 1), (posy - 1));
}
if(posx >= 0 && (posy-1) >= 0) {
System.out.println((posx)+" "+(posy-1));
check_move(posx, (posy - 1));
}
if((posx+1) < row && (posy+1) < col) {
System.out.println((posx+1)+" "+(posy+1));
check_move((posx + 1), (posy + 1));
}
if((posx-1) >= 0 && posy >= 0) {
System.out.println((posx-1)+" "+(posy));
check_move((posx - 1), posy);
}
if((posx+1) < row && posy >= 0) {
System.out.println((posx+1)+" "+(posy));
check_move((posx + 1), posy);
}
if(posx >= 0 && (posy+1) < col) {
System.out.println((posx)+" "+(posy+1));
check_move(posx, (posy + 1));
}
if((posx-1) >= 0 && (posy+1) < col) {
System.out.println((posx-1)+" "+(posy+1));
check_move((posx - 1), (posy + 1));
}
if((posx+1) < row && (posy-1) >= 0) {
System.out.println((posx+1)+" "+(posy-1));
check_move((posx + 1), (posy - 1));
}
}
}
}
明らかに、私は再帰中にcheck_moveメソッドを無限に呼び出しています。なぜそれが無限に続くのか分かりません。エラーメッセージは次のとおりです。Javaプログラムで無限再帰エラーを見つけるには?
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Integer.toString(Integer.java:402)
at TMS.check_move(TMS.java:70)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
at TMS.check_move(TMS.java:79)
at TMS.check_move(TMS.java:95)
.
.
.
.
(GOES ON FOREVER)
あなたはstackoverflow例外を取得している必要があります。原因を見つけるのに役立つstackoverflowにつながるスタックトレースを投稿する場合。 – SomeDude
私はJavaの初心者ですので、私は全く考えていません – phantom
このコードを使ってデバッグするのに十分な診断スキルがない場合は、より簡単なものから始めることをお勧めします - 基本的には、デバッグする方法を知っている。小さなステップを踏んで始めましょう。 –