Advertisement
yakovmonarh

Хранитель (Memento)

Sep 10th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. class Program
  9. {
  10.    static void Main(string[] args)
  11.    {
  12.     Hero hero = new Hero();
  13.     hero.Shoot();
  14.     GameHistory game = new GameHistory();
  15.     game.History.Push(hero.SaveState());
  16.     hero.Shoot();
  17.     hero.RestoreState(game.History.Pop());
  18.     hero.Shoot();
  19.    
  20.     Console.ReadLine();
  21.    }
  22. }
  23.  
  24. class Hero
  25. {
  26.     private int patrons = 10;
  27.     private int lavel = 5;
  28.    
  29.     public void Shoot()
  30.     {
  31.         if(patrons > 0)
  32.         {
  33.             patrons--;
  34.             Console.WriteLine("Производим выстрел. Патронов осталось {0}", patrons);
  35.         }
  36.         else{
  37.             Console.WriteLine("Патронов больше нет");
  38.         }
  39.     }
  40.    
  41.     public HeroMomento SaveState()
  42.     {
  43.         Console.WriteLine("Сохранение игры. Патронов: {0} Жизней: {1}", patrons, lavel);
  44.         return new HeroMomento(patrons, lavel);
  45.     }
  46.    
  47.     public void RestoreState(HeroMomento momento)
  48.     {
  49.         this.patrons = momento.Patrons;
  50.         this.lavel = momento.Lavel;
  51.         Console.WriteLine("Востановление. Патронов {0} Жизней {1}", patrons, lavel);
  52.     }
  53. }
  54.  
  55. class HeroMomento
  56. {
  57.     public int Patrons{get; private set;}
  58.     public int Lavel{get; private set;}
  59.    
  60.     public HeroMomento(int patrons, int lavel)
  61.     {
  62.         Patrons = patrons;
  63.         Lavel = lavel;
  64.     }
  65. }
  66.  
  67. class GameHistory
  68. {
  69.     public Stack<HeroMomento> History {get; private set;}
  70.     public GameHistory()
  71.     {
  72.         History = new Stack<HeroMomento>();
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement