作业是创建一个反向游戏。我让它工作,除了涉及改变多个芯片的动作
x o o o o o o @ <-- the @ is x's move that crashes the game.
,在这种情况下程序崩溃。崩溃发生在isPlayable()
的某处。
我哪里错了?
//there are 7 more such methods for different directions
// (down, left, right, diagonals).
public void searchN(int x, int y, int increment){
if(x-increment>=0){
if(this.isPlayed(x-increment,y)){
if(increment>1 && chips[x-increment][y]==turnColor){
//turnColor is an int value 1 or 2
// (0 represents unplayed space in chips[][])
// 1 corresponding to white, 2 corresponding to black.
playable[0] = true;
leads[0] = false;
} else {
if(chips[x-increment][y]!=turnColor){
leads[0]=true;
}
}
}else
leads[0]=false;
}else
leads[0]=false;
}
public boolean isPlayable(int x, int y){
this.searchN(x,y,1); //7 other directions are searched
while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
||leads[5]||leads[6]||leads[7]){
int i = 2;
if(leads[0]) // 7 other directions are searched given that their marker is true.
this.searchN(x,y,i);
}
if(playable[0]||playable[1]||playable[2]||playable[3]||playable[4]
||playable[5]||playable[6]||playable[7])
return true;
else
return false;
}
根据你的评论,听起来你正在经历一个挂起,而不是崩溃。当程序挂起时,您应该寻找程序可能无限期"卡住"的地方。isPlayable
的主要嫌疑人是你的while
循环。只要这八个布尔值中的任何一个为真,它就永远不会完成。
我会添加一些日志记录,这样你就可以看到发生了什么:
while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
||leads[5]||leads[6]||leads[7]){
System.out.println("leads[0]: " + leads[0]);
System.out.println("leads[1]: " + leads[1]);
// etc.
int i = 2;
if(leads[0]) // 7 other directions are searched given that their marker is true.
this.searchN(x,y,i);
}
一旦你确认了这是问题所在,开始查看你的搜索方法,找出为什么会发生