Original Post
EDIT: I'm starting to think that the problem is actually with the c_player::AddPiece function (the last one in this post). It's a very short function, could someone take a look and tell me if it's unsafe? Ok, basically I want to copy my c_map class. This is done by passing a pointer of a c_map, and all the information is read from that pointer and based on that, the new map stats are set. c_map class has an array of pointer to the c_piece class, and two c_player members (c_player player[0] and player[1]). The class c_player has only one member, and that is an array of c_pieces. Basically, each player has it's pieces and the map (or board) has an array for each square. If there is no piece in that square, the pointer there will point at a special type of piece, the c_piece no_piece, that is a member of map itself. Also, c_piece has only two member, one integer representing the player it belongs to (can be 1, 2 or 0 for the special case no_piece) and one enum representing the piece type (it's not chess but think of it as knight, bishop, queen, etc). The pieces pointer array is declared as c_pieces *pieces[6][6], and points to pieces inside c_player.(std::vector pieces). Well, currently I changes it into boost::array hoping that would solve the problem, but it didn't worked (it is declared now as boost::array,6> pieces;). However, the code I have currently for copying one map into another will corrupt the pieces array for some unknown reason. It basically has two nested loops (i an j, both from 0 to 5), and for each board square, I'll check if it points to no_piece (in this case the map being built will point to it's own no_piece), or if it's a player piece. If it's a player piece, it will make a copy of it, and send it to the corresponding player, and then add that piece just created into the board, at it's place (actually, it's place will point to it). However, after a few loop iterations, the *pieces[6][6] array will get corrupted. The houd 0,0, for instance, that previously pointed into a piece from player 1, of type (any), will now point into a piece that looks like random memory ({alive=true type=-17891602 player=-17891602 }), and all squares will eventually go bad (but they are initially set correctly). Any ideas? Thank you! By the way, here is the copy function: and the header: I think these are working correctly, but if you want to take a look, there are the functions used in the copy function: [Edited by - algumacoisaqualquer on January 14, 2007 3:04:45 PM]
void c_map::copy(c_map *map)
{
int total_pieces1 = 0; //total pieces looks dangerous, but should be working fine
int total_pieces2 = 0;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
if(map->pieces[j]->player == 0)
{
pieces[j] = &(no_piece);
}
if(map->pieces[j]->player == 1)
{
piece_type type = map->pieces[j]->type;
c_piece new_piece(type, 1);
player[0].AddPiece(new_piece);
pieces[j] = &player[0].pieces[total_pieces1];//pieces is a array of pointer, we tell pieces[j] to point into the last piece we have created for this player
total_pieces1++;
}
if(map->pieces[j]->player == 2)
{
piece_type type = map->pieces[j]->type;
c_piece new_piece(type, 2);
player[1].AddPiece(new_piece);
pieces[j] = &player[1].pieces[total_pieces2];
total_pieces2++;
}
}
}
this->player1_turn = map->player1_turn;
this->how_is_it_going = map->how_is_it_going;
}
class c_piece
{
public:
bool alive;
piece_type type;
int player;
void Remove();
c_piece();
c_piece(piece_type piece, int p_player);
};
class c_player
{
public:
std::vector<c_piece> pieces;
c_player();
void Reset();
void AddPiece(c_piece piece);
void copy(c_player *player);
};
class c_map
{
c_player player[2];
game_state how_is_it_going;
board_state MovementState(int start, int end);
board_state DoMovement(int start, int end); //DoMovement just moves them
public:
board_state MovePieces(int start, int end); //MovePieces checks if movement is legal
bool IsMovementLegal(int start, int end);
bool player1_turn;
c_piece no_piece;
//c_piece *pieces[6][6];
boost::array<boost::array<c_piece*,6>,6> pieces;//Changed to boost, but made no difference
c_map();
board_state SetMovement(int start, int end);
game_state GetGameState();
c_piece GetPiece(int position);
void Initiate();
void copy(c_map *map);
};
//c_piece constructor
c_piece::c_piece(piece_type piece, int p_player)
{
alive = true;
type = piece;
player = p_player;
}
void c_player::AddPiece(c_piece piece)
{
c_piece new_piece(piece.type, piece.player);
pieces.push_back(new_piece); //this is an std::vector - he is copying new_piece, not just adding by reference, right?
// I mean, new_piece will get deleted once this function is over, could this be causing the problem?
}