GameViewModel.dart 1.4 KB

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