Checkerboard.dart 1.0 KB

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