I'm new to c++ programming and I'm trying to make a chessgame in which I have classes that represent chesspieces on a chessboard. I've made instances of chesstiles on the chessboard that contain instances of chesspieces. If I shift one chessPiece from one chesstile to another one is this good practice? or should I refrain from turning pointers into nullprt?
std::map<TLocation, ChessTile*> ChessTiles;
bool ChessBoard::MoveFromTo(TArray<FVector2D> moves, int32 fromX, int32 fromY, int32 toX, int32 toY)
{
if (moves.Constains(FVector2D(toX,toY)))
{
ChessTile* chessTileFrom = ChessTiles.find({ fromX, fromY })->second;
ChessPiece* chessPieceFrom = chessTileFrom->chessPiece;
ChessTile* chessTileTo = ChessTiles.find({ toX, toY })->second;
chessTileTo->chessPiece = chessPieceFrom;
chessTileFrom->chessPiece = nullptr;
return true;
}
return false;
}
I'm expecting this to make sure that the chesspiece wont be recognized by multiple tiles. As far as I know this should work, I am not getting any errors. But I'm just not sure if this is good practice or if I should not do this. It's hard to find it anywhere but that might be because I'm not at that level of coding yet.
Thanks in advance!