Jump to content
  • Advertisement
Sign in to follow this  
xidis

weird bug - dont know if its compiler or code

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The entire bug revolves around this statement:
// WEIRD COMPILER BUG HERE - IF YOU TAKE AWAY THE DEBUG STATEMENT, NOTHING WORKS
  else { debug("  ");
    return false; }

The function debug:
void debug(std::string s) { std::cout << s << std::endl;}

When I compile all my code with the statement like it is, everything works fine. But when I remove the brackets and the debug statement, the code compiles yet nothing works right. Any ideas. Also, I will post more code if it is necessary.

Share this post


Link to post
Share on other sites
Advertisement
Here is the entire function:

bool chessBoard::movePiece(coord oldLocation, coord newLocation, bool owner, bool aiFlag)
{
//boolean representing the type of move carried out
bool normMove;

//std::cout << "owner: " << std::boolalpha << owner << std::endl;

// make sure the piece belongs to the owner
if(getPiece(oldLocation) == 0 || getPiece(oldLocation)->getOwner() != owner) {
//debug("moving piece is = 0 or pieces owner is not equal to owner of board");
return false;
}

//temporarary place to hold chesspiece to be deleted if player is not in
//check at the end of their turn (under a normal move) or a list of chessPieces
//to be restored if player castles during there move and is still in check
chessPiece* toBeDeleted = 0;
chessPiece** restore = 0;

//check to see if a normal move is valid
if(threaten(oldLocation, newLocation)) {
//debug("valid normal move");
if(getPiece(newLocation) == 0) {
getPiece(newLocation) = getPiece(oldLocation);
getPiece(newLocation)->setNewPosition(newLocation);
getPiece(newLocation)->setMoved();
getPiece(oldLocation) = 0;
}
else {
toBeDeleted = getPiece(newLocation);
getPiece(newLocation) = getPiece(oldLocation);
getPiece(newLocation)->setNewPosition(newLocation);
getPiece(newLocation)->setMoved();
getPiece(oldLocation) = 0;
}
}


//variables such as moved and currentLocation
else if(castlePossible(oldLocation, newLocation))
{
//debug("castle is possible");
restore = new chessPiece*[8];
for(int i = 0; i < 8; ++i)
{
if(owner && getPiece(coord(i, 0)) != 0) restore = getPiece(coord(i, 0))->clone();
else if(!owner && getPiece(coord(i, 7)) != 0) restore = getPiece(coord(i, 7))->clone();
else restore = 0;
}
castle(oldLocation, newLocation);
}

// WEIRD COMPILER BUG HERE - IF YOU TAKE AWAY THE DEBUG STATEMENT, NOTHING WORKS
else { debug(" ");
return false; }

//debug("got here");
// a player cannot end their turn in check
// tests to see if a player is in check at the end
// of their turn. If so, resets the board to the
// way it was previously
if(check(owner))
{
//debug("owner is in check");
if(toBeDeleted == 0 && restore == 0)
{
getPiece(oldLocation) = getPiece(newLocation);
getPiece(oldLocation)->unsetMoved();
getPiece(oldLocation)->setNewPosition(oldLocation);
getPiece(newLocation) = 0;
}
else if(toBeDeleted != 0)
{
getPiece(oldLocation) = getPiece(newLocation);
getPiece(oldLocation)->setNewPosition(oldLocation);
getPiece(newLocation) = toBeDeleted;
toBeDeleted = 0;
}

else if(restore != 0) {
// restore row of board to original
//debug("entered restore for castle");
for(int i = 0; i < 8; ++i)
{
if(owner) {delete getPiece(coord(i, 0)); getPiece(coord(i, 0)) = restore; }
else {delete getPiece(coord(i, 7)); getPiece(coord(i, 7)) = restore; }
delete[] restore;
restore = 0;
}
//debug("king is in check, not a valid move");
}
return false;
}

// move is fine, player is not in check, proceed normally
else
{
//debug("owner is not in check");
if(toBeDeleted != 0)
{
delete toBeDeleted;
toBeDeleted = 0;
}
else if(restore != 0)
{
//debug("deleting stuff in variable restore");
for(int i = 0; i < 8; ++i)
{
delete restore;
restore = 0;
}
delete[] restore;
}
pawnConverter(owner, aiFlag);
return true;
}
//debug("should not have gotten here");
}



If the problem is in the if statement, I dont see where

Share this post


Link to post
Share on other sites
I believe that the problem is your


// else
// {
// debug(" ");
// return false;
// }




and


// debug("should not have gotten here");






That is, you have some cases in your function where you don't return anything whatsoever.
If the "returns" of your function only says whether the move is allowed or not, just put either " return true;" or "return false" in the very end, incase you have built it so that it MUST go into one of the if cases when the move is valid/invalid.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!