Hi My name is raiz, I need some help in solving the flollowing error messege:
java.lang.StackOverflowError
Can anyone help me please?
I beleive the problem is in my do while loop but I don't know where exactly as I have tried everything that I know it's possible and nothing :( somehow I think my java maybe is doing it by itself like it's aliiiiiiiive or somerthning........... lol
Anyway, can anyone point out the problem and explain it to me plzzzzzzzzzzzzzzzz? at my SmartPlayer class I have this code
-------------------------------------------------------- public class SmartPlayer extends Player
{
// Connect the SmarterPlayer class to the Player class by declaring the super class which is
// the Player class
public SmartPlayer(Piece newPiece)
{
super(newPiece);
}
// Choosing the best move toward winning status as long as the game is still runnning
public int findWinningMove(Piece piece, Board board)
{
int column=0;
boolean value = false;
while (column <=board.N && value)
{
if(ConnectFour.canPlacePiece(board,column))
{
ConnectFour.placePiece(board,column, piece);
if(ConnectFour.gameOver(board))
{
ConnectFour.removePiece(board,column);
value= true;
}
}
else
column++;
}
if (value)
return column;
else
return -1;
}
// Choose column method in this class will return the value of the findWinningMove method.
public int chooseColumn(Board board)
{ int winningcolumn = findWinningMove((ConnectFour.opponentPiece(piece)), board);
At a guess, you are right about "the problem is in my while loop". Perhaps you need to answer the question of what happens when you canPlacePiece but it is not gameOver.
To clarify,
while (column <=board.N && value) {
if(ConnectFour.canPlacePiece(board,column)) {
ConnectFour.placePiece(board,column, piece);
if(ConnectFour.gameOver(board)) {
ConnectFour.removePiece(board,column);
value= true;
}
// should there be an else statement here???
else {
// this will be called if you can place a piece but
// the game is not over (i.e. not a winning move)
}
}
else
column++;
It's tough to say, because I don't know what's going on in the Player class's play method, but I think that it's likely that you have an infinite loop going on.
Perhaps the problem lies in that findWinningMovealways returns -1. It never enters the body of the while loop, because the value of value is set to false, causing the loop's condition-check to fail.