State.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'package:gobang/state/UserContext.dart';
  2. abstract class State {
  3. int _step = 0;
  4. int get step => _step;
  5. int _reg = 0;
  6. int get reg => _reg;
  7. UserContext _userContext;
  8. State(UserContext userContext) : _userContext = userContext;
  9. // 悔棋只能悔棋三次
  10. bool regretChess();
  11. // 认输10步之内不能认输
  12. bool surrender();
  13. play() {
  14. _step++;
  15. }
  16. }
  17. /// [StartState] 开始状态
  18. class StartState extends State {
  19. StartState(UserContext userContext) : super(userContext);
  20. // 悔棋只能悔棋三次
  21. @override
  22. bool regretChess() {
  23. return false;
  24. }
  25. @override
  26. bool surrender() {
  27. return false;
  28. }
  29. @override
  30. play() {
  31. super.play();
  32. if (_step >= 4) {
  33. _userContext.setState(MidState(_userContext)
  34. .._step = _step
  35. .._reg = _reg);
  36. }
  37. }
  38. }
  39. /// [MidState] 中场状态
  40. class MidState extends State {
  41. MidState(UserContext userContext) : super(userContext);
  42. @override
  43. int get _reg {
  44. return super._reg;
  45. }
  46. // 悔棋只能悔棋三次
  47. @override
  48. bool regretChess() {
  49. _reg++;
  50. if (_reg == 3) {
  51. print('切换到白热化阶段');
  52. _userContext.setState(EndState(_userContext)
  53. .._step = _step
  54. .._reg = _reg);
  55. }
  56. return true;
  57. }
  58. @override
  59. bool surrender() {
  60. return true;
  61. }
  62. }
  63. /// [EndState] 结尾状态
  64. class EndState extends State {
  65. EndState(UserContext userContext) : super(userContext);
  66. // 悔棋只能悔棋三次
  67. @override
  68. regretChess() {
  69. return false;
  70. }
  71. @override
  72. surrender() {
  73. return true;
  74. }
  75. }