task-tsgame.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // 玩家
  2. var Player = /** @class */ (function () {
  3. function Player(name) {
  4. this.name = name;
  5. this.hp = 5;
  6. this.power = 1;
  7. this.defense = 0;
  8. this.chargeCount = 0;
  9. this.reboundUsed = false;
  10. }
  11. Player.prototype.attack = function (target) {
  12. target.receiveDamage(this.power);
  13. console.log("".concat(this.name, "\u53D1\u52A8\u4E86\u653B\u51FB\uFF01"));
  14. };
  15. Player.prototype.charge = function () {
  16. this.chargeCount++;
  17. console.log("".concat(this.name, "\u5F00\u59CB\u84C4\u529B\uFF01"));
  18. };
  19. Player.prototype.defenseMove = function () {
  20. this.defense++;
  21. console.log("".concat(this.name, "\u91C7\u53D6\u4E86\u9632\u5FA1\u59FF\u6001\uFF01"));
  22. };
  23. Player.prototype.ultimateStrike = function (target) {
  24. if (this.chargeCount === 2) {
  25. target.receiveDamage(999); // 直接秒杀
  26. this.chargeCount = 0;
  27. console.log("".concat(this.name, "\u4F7F\u7528\u4E86\u7EC8\u6781\u5927\u62DB\uFF01").concat(target.name, "\u88AB\u51FB\u8D25\u4E86\uFF01"));
  28. }
  29. else if (this.chargeCount === 1) {
  30. target.receiveDamage(this.power * 2); // 大招伤害为攻击的双倍
  31. this.chargeCount = 0;
  32. console.log("".concat(this.name, "\u91CA\u653E\u4E86\u5927\u62DB\uFF01").concat(target.name, "\u53D7\u5230\u4E86").concat(this.power * 2, "\u70B9\u4F24\u5BB3\uFF01"));
  33. }
  34. else {
  35. console.log("".concat(this.name, "\u5C1A\u672A\u84C4\u529B\u8DB3\u591F\uFF0C\u65E0\u6CD5\u91CA\u653E\u5927\u62DB\uFF01"));
  36. }
  37. };
  38. Player.prototype.reboundMove = function () {
  39. if (this.reboundUsed) {
  40. console.log("".concat(this.name, "\u672C\u5C40\u5DF2\u7ECF\u4F7F\u7528\u8FC7\u53CD\u5F39\u4E86\uFF01"));
  41. }
  42. else {
  43. this.reboundUsed = true;
  44. console.log("".concat(this.name, "\u91CA\u653E\u4E86\u53CD\u5F39\u6280\u80FD\uFF01"));
  45. }
  46. };
  47. Player.prototype.receiveDamage = function (damage) {
  48. var actualDamage = Math.max(damage - this.defense, 0); // 考虑防御
  49. this.hp -= actualDamage;
  50. console.log("".concat(this.name, "\u53D7\u5230\u4E86").concat(actualDamage, "\u70B9\u4F24\u5BB3\uFF01"));
  51. if (this.hp <= 0) {
  52. console.log("".concat(this.name, "\u88AB\u51FB\u8D25\u4E86\uFF01"));
  53. }
  54. };
  55. return Player;
  56. }());
  57. // 游戏主逻辑
  58. function playGame() {
  59. var player1 = new Player('玩家1');
  60. var player2 = new Player('玩家2');
  61. while (player1.hp > 0 && player2.hp > 0) {
  62. // 玩家1的回合
  63. console.log('\n===== 玩家1的回合 =====');
  64. player1.attack(player2);
  65. // 玩家2的回合
  66. console.log('\n===== 玩家2的回合 =====');
  67. player2.charge();
  68. // 玩家1的回合
  69. console.log('\n===== 玩家1的回合 =====');
  70. player1.defenseMove();
  71. // 玩家2的回合
  72. console.log('\n===== 玩家2的回合 =====');
  73. player2.ultimateStrike(player1);
  74. // 重置防御
  75. player1.defense = 0;
  76. player2.defense = 0;
  77. // 判断玩家1是否被击败
  78. if (player1.hp <= 0) {
  79. break;
  80. }
  81. // 玩家1的回合
  82. console.log('\n===== 玩家1的回合 =====');
  83. player1.charge();
  84. // 玩家2的回合
  85. console.log('\n===== 玩家2的回合 =====');
  86. player2.attack(player1);
  87. // 玩家1的回合
  88. console.log('\n===== 玩家1的回合 =====');
  89. player1.reboundMove();
  90. // 判断玩家2是否被击败
  91. if (player2.hp <= 0) {
  92. break;
  93. }
  94. }
  95. }
  96. // 运行游戏
  97. playGame();