• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

memento design pattern

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can I use memento pattern to store the chess games on every move position?

If so can I assume that the condition to use the memento pattern is

1) Where I can able to get back one of its previous states
2) The level of persistence will be minimum(i.e the number of records to persist/ remember the state will be minimum).

Regards,
M.S.Raman
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but you could also just serialize the object itself. i.e. something called a ChessMove or ChessPosition object.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think memento stores the whole state of the game prior to every move. Does that sound right? That would make it dead easy to back up one move by popping the state off a stack.

One alternative is to create a compensating transaction for every move. If I move a pawn forward, I put a "move pawn backward" command on a stack. If I move and take something I put a "move back and restore the other piece" command on the stack. Far less simple, but might use a lot less stack space. Or not.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need a little more clarification on how you propose to use memento.


One alternative is to create a compensating transaction for every move.


I don't think it would require saving a compensating transaction for every move. Each move would just store its position. You could go back a move by popping off the stack. A push to the stack would move the chess piece to the next position. Each player could have their own stack and the game could contain both stacks. Saving both stacks would save the games state. All you would have to know is which player made the first move and the whole game could be replayed move by move by replaying the stacks.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve , Stan for replying my query.

I think both methods can be useful for storing the game positions. I don't find any difference between this two methods.

Regards,
M.S.Raman





Originally posted by steve souza:
We need a little more clarification on how you propose to use memento.


I don't think it would require saving a compensating transaction for every move. Each move would just store its position. You could go back a move by popping off the stack. A push to the stack would move the chess piece to the next position. Each player could have their own stack and the game could contain both stacks. Saving both stacks would save the games state. All you would have to know is which player made the first move and the whole game could be replayed move by move by replaying the stacks.

 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Steve and I are saying the same thing. For a simple move one square forward the compensating transaction would just be the prior position. (Compensating transaction is just way too big a word for that .) If I took a piece, it might be that plus the piece that should go where I just came from. So is the state stored for every move: prior position, current position, zero or one pieces removed by my move?

Just firing random neurons now ... for checkers a move could have several hops and take several pieces. You might need a chain of these things.
 
reply
    Bookmark Topic Watch Topic
  • New Topic