player_driver.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import '../models/player.dart';
  2. import 'driver_online.dart';
  3. import 'driver_robot.dart';
  4. import 'driver_user.dart';
  5. abstract class PlayerDriver {
  6. final Player player;
  7. bool canBacktrace = true;
  8. // 认输
  9. static const rstGiveUp = 'giveup';
  10. // 提和
  11. static const rstRqstDraw = 'rqstrdraw';
  12. // 悔棋
  13. static const rstRqstRetract = 'rqstretract';
  14. // 同意提和
  15. static const rstDraw = 'draw';
  16. // 同意悔棋
  17. static const rstRetract = 'retract';
  18. static const rstActions = [
  19. rstGiveUp,
  20. rstRqstDraw,
  21. rstRqstRetract,
  22. rstDraw,
  23. rstRetract
  24. ];
  25. static bool isAction(String move) {
  26. return rstActions.contains(move) || move.contains(rstRqstDraw);
  27. }
  28. PlayerDriver(this.player);
  29. static PlayerDriver createDriver(
  30. Player manager, [
  31. DriverType type = DriverType.user,
  32. ]) {
  33. switch (type) {
  34. case DriverType.robot:
  35. return DriverRobot(manager);
  36. case DriverType.online:
  37. return DriverOnline(manager);
  38. default:
  39. return DriverUser(manager);
  40. }
  41. }
  42. /// 申请和棋
  43. Future<bool> tryDraw();
  44. /// 申请悔棋
  45. Future<bool> tryRetract();
  46. /// 获取走招
  47. Future<String?> move();
  48. /// 思考
  49. Future<String> ponder();
  50. /// 完成走招
  51. void completeMove(String move);
  52. @override
  53. String toString() => "$runtimeType ${player.team}";
  54. }
  55. class DriverType {
  56. final String type;
  57. static const user = DriverType('user');
  58. static const robot = DriverType('robot');
  59. static const online = DriverType('online');
  60. const DriverType(this.type);
  61. }