Looking at your code:
Code:
if(mortals->next != NULL) // ... check that there are actually mortals in play.
{
while(angels->next != NULL)
{
angels->move();
if(angels->readxc() == 0) angels->editcoords(1,0); // fix coord == 0
if(angels->readyc() == 0) angels->editcoords(0,1); // fix coord == 0
if(angels->readxc() > BOARD_X) angels->editcoords(-1,0);
if(angels->readyc() > BOARD_Y) angels->editcoords(0,-1);
gotoxy(angels->readxc(), angels->readyc()); // go to new coords
cout << "O";
angels = angels->next; // next node
}
}
What if mortals or angels is NULL to begin with? Of course, that shouldn't cause it to segfault at the "next node" line. But it's still something to consider.
Anyway . . . I think this code is a little mixed-up.
Code:
for(int i = 0; i < NUMBER_MORTALS; i++)
{
mortals->create(rand() % PIECE_SPREAD + 1, rand() % PIECE_SPREAD + 1); // create at a random position
mortals->next = new Mortal; // Current mortal's next pointer now points to a new Mortal
mortals->next->prev = mortals; // The new node's previous pointer points to the current node
// DEBUG
/*
cout << "Mortal created at " << mortals->readxc() << ", " << mortals->readyc() << ".\n"; // DEBUG LINE
Sleep(100); // DEBUG LINE
*/
mortals = mortals->next; // Move to the newly-created Mortal node
Sleep(1);
}
The call to mortals->create() initializes the mortals object -- the already-existing mortals object, mind. Then you go and add a new object, which is not initialized -- and guess what? You never set its next pointer, so it's probably set to something strange.
I think you meant to call mortals->next->create().
On a side note, create() would work very well as a constructor.
[edit] Okay, I guess me finding (possible?) segfaults in your code isn't going to help you find them, either.
How did I find it? I figured that angels->next must be invalid. So the first place to check was where angels->next was initialized. That led me to the block of code I posted.
I guess the rest was being suspicious about whether angels->next->next really was initialized.
[Assuming my analysis is right, of course, and that it actually isn't.]
[/edit]