Hi. I would like help on undoing and redoing using the memento design pattern. I want to save a state that a board is in. Like a chessboard. If the user uses the undo option a previous board should be loaded and all of the states that the pieces was in. And the redo option should redo a previous move. I'm in quite a hurry to finish this project. This is about all I still have to do. Any help or suggestions on how to do it would be appreciated.

I forgot to mention that the undo choice may only be used once, and the redo as well.
This code might help you to help me... :) I'm not that good in C++(or programming for that matter), so if there are dumb mistakes, please tell me about it.

void LayoutMemento::CurrentLayout(Piece*** currentBoard)
{
	ListNode *newNode;
	ListNode *nodePtr;

	newNode = new ListNode;

	newNode->piece = currentBoard;
	newNode->next = NULL;

	if(!head)
	{
		head = newNode;
	}
	else
	{
		nodePtr = head;

		Piece*** tempCurrent;
		Piece*** currentCopy;

		currentCopy = currentBoard;

		for(int i = 0; i < 9; i++)
		{
			for(int j = 0; j < 9; i++)
			{
				if(currentBoard[i][j] != NULL)
				{
					tempCurrent[i][j] = currentCopy[i][j];
				}
			}
		}

		while(nodePtr->next)
		{
			nodePtr = nodePtr->next;
		}

		nodePtr->next = newNode;
	}
}

void LayoutMemento::display() const
{
	ListNode *nodePtr;

	nodePtr = head;

	while(nodePtr)
	{
		cout << nodePtr->piece << endl;
		nodePtr = nodePtr->next;
	}
}

void LayoutMemento::getNext(Piece*** nextBoard)
{
	Piece*** tempNext;
	Piece*** nextCopy;

	nextCopy = nextBoard;

	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; i++)
		{
			tempNext[i][j] = nextCopy[i][j];
		}
	}
}

void LayoutMemento::getPrevious(Piece*** previousPiece)
{
	Piece*** tempPrevious;
	Piece*** previousCopy;

	previousCopy = previousPiece;

	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; i++)
		{
				tempPrevious[i][j] = previousCopy[i][j];
		}
	}
}

To accomplish the undo option all you need to do is make a copy of the chessboard before any move is made. After the move then use that copy for the undo option.

Thanks for the reply.

It is so simple to say it, but I struggle with programming. Sorry for the lines and lines of code, but please look through it and tell me if I'm at least in the right direction. And if you may, point out where improvements can be made or code be taken out.

I appreciate any reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.