game_view_model.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:gobang/bridge/chess_shape.dart';
  2. import 'package:gobang/bridge/circle_shape.dart';
  3. import 'package:gobang/bridge/rect_shape.dart';
  4. import 'package:gobang/flyweight/chess.dart';
  5. import 'package:gobang/flyweight/chess_flyweight_factory.dart';
  6. import 'package:gobang/state/game_state.dart';
  7. import 'package:gobang/state/user_context.dart';
  8. class GameViewModel {
  9. GameViewModel._();
  10. static final GameViewModel instance = GameViewModel._();
  11. static GameViewModel getInstance() => instance;
  12. final UserContext _userContext = UserContext();
  13. Chess play() {
  14. _userContext.play();
  15. return ChessFlyweightFactory.getInstance().getChess('white');
  16. }
  17. ChessShape shapeFor(bool useCircle) =>
  18. useCircle ? CircleShape() : RectShape();
  19. bool undo() => _userContext.regretChess();
  20. String get state {
  21. final s = _userContext.state;
  22. if (s is StartState) {
  23. return '热身阶段,不能悔棋,不能投降';
  24. }
  25. if (s is MidState) {
  26. return '入神阶段,可以悔棋且剩余${3 - s.reg}次,可以投降';
  27. }
  28. if (s is EndState) {
  29. return '白热化阶段,悔棋次数已用完,但可以投降';
  30. }
  31. return '';
  32. }
  33. void reset() => _userContext.reset();
  34. bool surrender() => _userContext.surrender();
  35. }