GameViewModel.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:gobang/bridge/ChessShape.dart';
  2. import 'package:gobang/flyweight/Chess.dart';
  3. import 'package:gobang/flyweight/ChessFlyweightFactory.dart';
  4. import 'package:gobang/state/State.dart';
  5. import 'package:gobang/state/UserContext.dart';
  6. class GameViewModel {
  7. GameViewModel._();
  8. static GameViewModel? _gameViewModel;
  9. static getInstance() {
  10. if (_gameViewModel == null) {
  11. _gameViewModel = GameViewModel._();
  12. }
  13. return _gameViewModel;
  14. }
  15. UserContext _userContext = UserContext();
  16. Chess play() {
  17. _userContext.play();
  18. Chess chess;
  19. /// 设置棋子外观
  20. ChessShape shape = RectShape();
  21. chess = ChessFlyweightFactory.getInstance().getChess("white");
  22. chess.chessShape = shape;
  23. return chess;
  24. }
  25. bool undo() {
  26. return _userContext.regretChess();
  27. }
  28. get state{
  29. if(_userContext.state is StartState){
  30. return "热身阶段,不能悔棋,不能投降";
  31. } else if(_userContext.state is MidState) {
  32. return "入神阶段,可以悔棋且剩余${3 - _userContext.state.rect}次,可以投降";
  33. } else if(_userContext.state is EndState) {
  34. return "白热化阶段,悔棋次数已用完,但可以投降";
  35. }
  36. }
  37. void reset() {
  38. _userContext.reset();
  39. }
  40. }