123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import 'package:gobang/state/UserContext.dart';
- abstract class State {
- int _step = 0;
- int get step => _step;
- int _reg = 0;
- int get reg => _reg;
- UserContext _userContext;
- State(UserContext userContext) : _userContext = userContext;
- // 悔棋只能悔棋三次
- bool regretChess();
- // 认输10步之内不能认输
- bool surrender();
- play() {
- _step++;
- }
- }
- /// [StartState] 开始状态
- class StartState extends State {
- StartState(UserContext userContext) : super(userContext);
- // 悔棋只能悔棋三次
- @override
- bool regretChess() {
- return false;
- }
- @override
- bool surrender() {
- return false;
- }
- @override
- play() {
- super.play();
- if (_step >= 4) {
- _userContext.setState(MidState(_userContext)
- .._step = _step
- .._reg = _reg);
- }
- }
- }
- /// [MidState] 中场状态
- class MidState extends State {
- MidState(UserContext userContext) : super(userContext);
- @override
- int get _reg {
- return super._reg;
- }
- // 悔棋只能悔棋三次
- @override
- bool regretChess() {
- _reg++;
- if (_reg == 3) {
- print('切换到白热化阶段');
- _userContext.setState(EndState(_userContext)
- .._step = _step
- .._reg = _reg);
- }
- return true;
- }
- @override
- bool surrender() {
- return true;
- }
- }
- /// [EndState] 结尾状态
- class EndState extends State {
- EndState(UserContext userContext) : super(userContext);
- // 悔棋只能悔棋三次
- @override
- regretChess() {
- return false;
- }
- @override
- surrender() {
- return true;
- }
- }
|