Originator.dart 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'package:gobang/flyweight/Chess.dart';
  2. import 'package:gobang/flyweight/Position.dart';
  3. import 'package:gobang/memorandum/CareTaker.dart';
  4. import 'Memo.dart';
  5. class Originator {
  6. Originator._();
  7. static Originator? _originator;
  8. static getInstance(){
  9. if(_originator == null){
  10. _originator = Originator._();
  11. }
  12. return _originator;
  13. }
  14. bool _canAdd = true; //是否需要添加
  15. List<Position> _state = [];
  16. List<Position> get state{
  17. return _state;
  18. }
  19. CareTaker _careTaker = CareTaker();
  20. add(Position position) {
  21. if(_canAdd) { //因为每次渲染完默认会把最后一个下棋的位置添加上,但在悔棋阶段最后一个是不需要,因此需要这个判断。
  22. _state.add(position);
  23. _careTaker.add(_save());
  24. }
  25. _canAdd = true;
  26. }
  27. _from(Memo memo) {
  28. this._state = memo.state;
  29. }
  30. Memo _save() {
  31. return new Memo()..state.addAll(this._state);
  32. }
  33. clean(){
  34. _state = [];
  35. }
  36. undo() {
  37. Memo memo = _careTaker.getLast();
  38. _from(memo);
  39. _canAdd = false;
  40. }
  41. }