//Use the Memento Pattern.
//The Runner class creates the Game object and an array of Caretaker objects.
//Each Caretaker object will store an instance of the Memento object.
//The input data provided in the Simulator object is processed and at the same time, application state is stored by the Memento object.
int move;
Console.WriteLine("Let's practice TicTacToe");
Console.WriteLine("Commands are:\n1-9 for a position\nU-n where n is the number of moves to undo\nQ to end");
Game game = new Game();
Caretaker[] c = new Caretaker[10];
game.DisplayBoard();
move = 1;
Simulator simulator = new Simulator();
foreach (string command in simulator)
{
Console.WriteLine("\nMove {0} for {1}: {2}", move, game.Player, command);
if (command[0] == 'Q') break;
c[move] = new Caretaker();
c[move].Memento = game.Save();
if (command[0] == 'U')
{
int back = int.Parse(command.Substring(2, 1));
if (move - back > 0)
{
game.Restore(c[move - back].Memento);
}
else
{
Console.WriteLine("Too many moves back");
}
move = move - back - 1;
}
else
{
game.Play(int.Parse(command.Substring(0, 1)));
}
game.DisplayBoard();
move++;
}
Console.WriteLine("Thanks for playing!");