Hi,
Warning this is a long question!
I am implementing a Solitaire card game in C++ on Win32, and after asking this question, it's becoming clear that I may need a bit of guidance regarding actual class design rather than implementation details.
I am using a model view Controller pattern to implement the game. The model is the game, cards, columns and move history. The view is responsible for painting to screen and the control responsible for handling messages and the timer.
The aspect of the game that I am currently trying to implement is the Action history, which belongs to the Model
- I want to be able to "undo" and "redo" any Action
.
I describe a Move
object as an atomic move of a single card from one CardPile
to another. And I described an Action
as consisting of one or more Moves
. e.g. a deal will be 10 Moves
from the Deck
to a particular Column
. ( Deck
and Column
are simply specializations of CardPile
).
I define the Action as a deque of Moves, and have provided some functions to GetCurrentMove() and to Advance() a move when it has been performed.
class Action
{
public:
void SetMoves( std::deque<Move> dmoves){ _m_deque = dmoves; }
void Advance();
std::deque<Move>::const_iterator GetCurrentMove();
private:
std::deque<Move> _m_deque;
std::deque<Move>::const_iterator currentmove;
};
When dealing (or setting up, or undoing), these Move
objects are data for an animation. I need to be able to access a single Move
object at a time. I retrieve the Move
, parse it into x,y co-ords and then kick off an animation to move a card from one place to another on screen. When the card has reached its destination, I then pull another Move
from the deque.
I have been advised by others with more experience, not to store iterators inside the Action
class. The STL doesn't do this and there are good reasons apparently.
But my question is - don't I have to store iterators inside the Action
class?
You see both my Model and View need access to the current Move
, so where else can I store that iterator that refers to the current Move
... inside the Controller?
My game animation is based (very broadly) on this model:
void control.game_loop( message )
{
switch( message )
{
case TIMER:
{
if( view.CardHasReachedDestination() )
{
game.AdvanceAnimation();
if( !game.AnimationFinished() )
view.Parse(game.GetNextMove());
}
view.MoveCardXY();
view.PaintToTheScreen();
controller.StartTheTimerAgain();
}
}
}
Best wishes,
BeeBand