State.dart 1.4 KB

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