GameViewModel.dart 1.5 KB

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