The Artima Developer Community
Sponsored Link

Java Answers Forum
Connect 4 game

3 replies on 1 page. Most recent reply: Dec 5, 2003 3:41 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Mohd

Posts: 1
Nickname: raiz
Registered: Dec, 2003

Connect 4 game Posted: Dec 2, 2003 9:36 PM
Reply to this message Reply
Advertisement
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);

if (winningcolumn == -1)

return super.play(board);

else

return winningcolumn ;}


}

-------------------------------------------------


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Connect 4 game Posted: Dec 3, 2003 5:57 AM
Reply to this message Reply
Hi,

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++;


Adam

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Connect 4 game Posted: Dec 3, 2003 2:54 PM
Reply to this message Reply
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 findWinningMove always 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.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Connect 4 game Posted: Dec 5, 2003 3:41 AM
Reply to this message Reply
Well spotted Joe.

Adam

Flat View: This topic has 3 replies on 1 page
Topic: Screen resolution Previous Topic   Next Topic Topic: Connection reset by peer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use