GameViewModel.dart 1.5 KB

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