State.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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).._step = _step.._reg = _reg);
  34. }
  35. }
  36. }
  37. /// [MidState] 中场状态
  38. class MidState extends State {
  39. MidState(UserContext userContext) : super(userContext);
  40. @override
  41. int get _reg{
  42. return super._reg;
  43. }
  44. // 悔棋只能悔棋三次
  45. @override
  46. bool regretChess(){
  47. _reg++;
  48. if(_reg == 3) {
  49. print('切换到白热化阶段');
  50. _userContext.setState(EndState(_userContext).._step = _step.._reg = _reg);
  51. }
  52. return true;
  53. }
  54. @override
  55. bool surrender() {
  56. return true;
  57. }
  58. }
  59. /// [EndState] 结尾状态
  60. class EndState extends State {
  61. EndState(UserContext userContext) : super(userContext);
  62. // 悔棋只能悔棋三次
  63. @override
  64. regretChess(){
  65. return false;
  66. }
  67. @override
  68. surrender() {
  69. return true;
  70. }
  71. }