如何修复代码中的StackOverflowError? [英] How do I fix the StackOverflowError in my code?

查看:73
本文介绍了如何修复代码中的StackOverflowError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个java游戏。



有一个滑块(S)可以移动一个西北 - 南方的方块 - 东方向。有一个Diag(D)片可以在任何对角线方向上移动一个方格。还有一个Jumper(J)片,随机传送到电路板上的任何方块。滑块首先移动,然后是Diag,然后按顺序移动。如果一件落在另一件上,则后者从游戏中消失。例如,如果Jumper落在Diagonal上,那么Diagonal将在游戏剩余时间内移除。





I'm trying to make a java game.

There is a Slider(S) piece that can move one square in a north-west-south-east direction. There is a Diag(D) piece that can move one square in any diagonal direction. And there is a Jumper(J) piece that teleports randomly to any square on the board. Slider moves first, then Diag, and then Jumper in that order. If one piece lands on another, then the latter is eliminated from the game. For instance, if Jumper lands on Diagonal, then Diagonal is removed for the rest of the game.


. . . . .    
. . . . .     
. . s . d    
. . . . .    
. . . . j





由于我认为构造函数Collide(),我收到了stackoverflow错误。



这是我的代码:





I'm getting a stackoverflow error due to the constructor Collide() i think.

This is my code:

public class Collide {
	Diag diag = new Diag();
	Jumper jumper = new Jumper();
	Slider slider = new Slider();
	Piece s1, d1, j1;		//slider, diag, jumper pieces
	int count;				//counts number of turns taken
	public Collide(){
		//initialize pieces
		s1 = new Slider();
		d1 = new Diag();
		j1 = new Jumper();
	}
	
	public void play(){
		do{
			this.draw();
			slider.movePiece();
			diag.movePiece();
			jumper.movePiece();
			
		}
		while((jumper.alive && slider.alive) || (jumper.alive && diag.alive) || (diag.alive && slider.alive));
		
	}
	
	public static void main(String [] args){
		Collide collide = new Collide();
		collide.play();
	}
	public void draw(){
		char[][] arrayLengthD = new char[5][5];
			for(int i = 0; i<5; i++) {
				System.out.println();
					for(int j = 0; j<5; j++) {
						arrayLengthD[i][j] = '-';
						System.out.print(arrayLengthD[i][j] + " ");
						
						if(diag.xD==i && diag.yD ==j){
							arrayLengthD[i][j] = 'D';	
						}
						if(slider.xS==i && slider.yS==j){
							arrayLengthD[i][j] = 'S';
						}
						if(jumper.xJ==i && jumper.yJ==j){
							arrayLengthD[i][j] = 'S';
						}
						
						//if((y==i&&x!=j)||(x==j&&y!=i)) {}
				}   
			
		}
		System.out.println();
}
}

class Piece {
	int x, y;		//location of the piece on the board
	boolean alive = true;	//is the piece still alive?
	public void movePiece(){}
}
class Slider extends Piece {
	Diag diag = new Diag();
	Jumper jumper = new Jumper();
	int xS = (int)(Math.random()*4)+1;
	int yS = (int)(Math.random()*4)+1;
	int g;
	public void movePiece(){ //method to move Slider
		g = (int)(Math.random()*4)+1;
		switch(g){
			case 1: yS = yS - 1; break;
			case 2: yS = yS + 1; break;
			case 3: xS = xS - 1; break;
			case 4: xS = xS + 1; break;
		}
		if(xS==-1){
			xS = 1;
		}
		if(yS==-1){
			yS = 1;
		}
		if(xS==5){
			xS = 3;
		}
		if(yS==5){
			yS=3;
		}
		if(xS == diag.xD && yS == diag.yD){
			diag.alive=false;
		}
		if(xS == jumper.xJ && yS == jumper.yJ){
			jumper.alive=false;
		}
	}
	
}
class Diag extends Piece {
	Jumper jumper = new Jumper();
	Slider slider = new Slider();
	int xD=(int)(Math.random()*4)+1;
	int yD = (int)(Math.random()*4)+1;
	int g;
	public void movePiece(){ //method to move Diag
		g = (int)(Math.random()*4)+1;
		switch(g){
			case 1: xD = xD + 1; yD = yD + 1; break;
			case 2: xD = xD + 1; yD = yD - 1; break;
			case 3: xD = xD - 1; yD = yD + 1; break;
			case 4: xD = xD - 1; yD = yD - 1; break; 
		}
		if(xD==-1 && yD == -1){
			xD = 1;
			yD = 1;
		}	
		if(xD==-1){
			xD = xD + 2; 
			yD = yD + 1;
		}
		if(yD==-1){
			xD = xD + 1; 
			yD = yD + 2;
		}
		if(xD==5){
			xD = xD - 2; 
			yD = yD - 1;
		}
		if(yD==5){
			xD = xD - 1; 
			yD = yD - 2;
		}
		if(xD == slider.xS && yD == slider.yS){
			slider.alive=false;
		}
		if(xD == jumper.xJ && yD == jumper.yJ){
			jumper.alive=false;
		}

	}
}

class Jumper extends Piece{
	Diag diag = new Diag();
	Slider slider = new Slider();
	int xJ = (int)(Math.random()*4)+1;
	int yJ = (int)(Math.random()*4)+1;
	public void movePiece(){	//method to move Jumper
		int xJ = (int)(Math.random()*4) + 1;
		int yJ = (int)(Math.random()*4) + 1;
		if(xJ == diag.xD && yJ == diag.yD){
			diag.alive=false;
		}
		if(xJ == slider.xS && yJ == slider.yS){
			slider.alive=false;
		}

	}
}

推荐答案

这就像结果一样简单相互递归没有退出: http://en.wikipedia.org/wiki/Mutual_recursion [ ^ ]。



查看你的代码:创建一个 Jumper实例创建一个 Slider 的实例,但是创建 Slider 的实例会创建一个 Jumper 的实例。等等, ad infinitum ...



请注意,堆栈溢出异常几乎总是指示递归问题,一个或多个另一种形式。使用调试器很容易检测到这些问题。



-SA
This is as simple as the result mutual recursion without an exit from it: http://en.wikipedia.org/wiki/Mutual_recursion[^].

Look at your code: creation of an instance of Jumper creates an instance of Slider, but creation of an instance of Slider creates an instance of Jumper. And so on, ad infinitum

Note that the stack overflow exception is nearly always an indication of a problem with recursion, of one or another form. And such problems are pretty easy detected with the debugger.

—SA


这篇关于如何修复代码中的StackOverflowError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆