hid_msrpuwgj56ccf29 1 an în urmă
părinte
comite
e1e80eaac7
2 a modificat fișierele cu 161 adăugiri și 0 ștergeri
  1. 74 0
      task/ai-tslang/task-tsgame.js
  2. 87 0
      task/ai-tslang/task-tsgame.ts

+ 74 - 0
task/ai-tslang/task-tsgame.js

@@ -0,0 +1,74 @@
+var Player = /** @class */ (function () {
+    function Player() {
+        this.life = 3;
+        this.choices = ['石头', '剪刀', '布'];
+    }
+    Player.prototype.makeChoice = function () {
+        return Math.floor(Math.random() * 3) + 1;
+    };
+    return Player;
+}());
+var Game = /** @class */ (function () {
+    function Game() {
+        this.players = [new Player(), new Player()];
+    }
+    Game.prototype.playGame = function () {
+        console.log('游戏开始!');
+        while (this.players[0].life > 0 && this.players[1].life > 0) {
+            this.playRound();
+        }
+        this.endGame();
+    };
+    Game.prototype.playRound = function () {
+        var player1Choice = this.players[0].makeChoice();
+        var player2Choice = this.players[1].makeChoice();
+        var result = this.getResult(player1Choice, player2Choice);
+        this.printRoundResult(player1Choice, player2Choice, result);
+        if (result === 1) {
+            this.players[1].life--;
+        }
+        else if (result === -1) {
+            this.players[0].life--;
+        }
+        this.printLifeStatus();
+    };
+    Game.prototype.getResult = function (choice1, choice2) {
+        if (choice1 === choice2) {
+            return 0;
+        }
+        else if ((choice1 === 1 && choice2 === 2) ||
+            (choice1 === 2 && choice2 === 3) ||
+            (choice1 === 3 && choice2 === 1)) {
+            return 1;
+        }
+        else {
+            return -1;
+        }
+    };
+    Game.prototype.printRoundResult = function (choice1, choice2, result) {
+        console.log('对局开始:');
+        console.log("\u73A9\u5BB61\u9009\u62E9\u4E86".concat(this.players[0].choices[choice1 - 1]));
+        console.log("\u73A9\u5BB62\u9009\u62E9\u4E86".concat(this.players[1].choices[choice2 - 1]));
+        if (result === 0) {
+            console.log('平局!');
+        }
+        else if (result === 1) {
+            console.log('玩家1赢了!');
+        }
+        else {
+            console.log('玩家2赢了!');
+        }
+    };
+    Game.prototype.printLifeStatus = function () {
+        console.log("\u73A9\u5BB61\u751F\u547D\u503C\uFF1A".concat(this.players[0].life));
+        console.log("\u73A9\u5BB62\u751F\u547D\u503C\uFF1A".concat(this.players[1].life));
+        console.log('----------------------');
+    };
+    Game.prototype.endGame = function () {
+        console.log('游戏结束!');
+    };
+    return Game;
+}());
+// 创建游戏实例并开始对战
+var game = new Game();
+game.playGame();

+ 87 - 0
task/ai-tslang/task-tsgame.ts

@@ -0,0 +1,87 @@
+class Player {
+    life: number;
+    choices: string[];
+  
+    constructor() {
+      this.life = 3;
+      this.choices = ['石头', '剪刀', '布'];
+    }
+  
+    makeChoice(): number {
+      return Math.floor(Math.random() * 3) + 1;
+    }
+  }
+  
+  class Game {
+    players: Player[];
+  
+    constructor() {
+      this.players = [new Player(), new Player()];
+    }
+  
+    playGame(): void {
+      console.log('游戏开始!');
+      while (this.players[0].life > 0 && this.players[1].life > 0) {
+        this.playRound();
+      }
+      this.endGame();
+    }
+  
+    playRound(): void {
+      const player1Choice = this.players[0].makeChoice();
+      const player2Choice = this.players[1].makeChoice();
+  
+      const result = this.getResult(player1Choice, player2Choice);
+      this.printRoundResult(player1Choice, player2Choice, result);
+  
+      if (result === 1) {
+        this.players[1].life--;
+      } else if (result === -1) {
+        this.players[0].life--;
+      }
+  
+      this.printLifeStatus();
+    }
+  
+    getResult(choice1: number, choice2: number): number {
+      if (choice1 === choice2) {
+        return 0;
+      } else if (
+        (choice1 === 1 && choice2 === 2) ||
+        (choice1 === 2 && choice2 === 3) ||
+        (choice1 === 3 && choice2 === 1)
+      ) {
+        return 1;
+      } else {
+        return -1;
+      }
+    }
+  
+    printRoundResult(choice1: number, choice2: number, result: number): void {
+      console.log('对局开始:');
+      console.log(`玩家1选择了${this.players[0].choices[choice1 - 1]}`);
+      console.log(`玩家2选择了${this.players[1].choices[choice2 - 1]}`);
+  
+      if (result === 0) {
+        console.log('平局!');
+      } else if (result === 1) {
+        console.log('玩家1赢了!');
+      } else {
+        console.log('玩家2赢了!');
+      }
+    }
+  
+    printLifeStatus(): void {
+      console.log(`玩家1生命值:${this.players[0].life}`);
+      console.log(`玩家2生命值:${this.players[1].life}`);
+      console.log('----------------------');
+    }
+  
+    endGame(): void {
+      console.log('游戏结束!');
+    }
+  }
+  
+  // 创建游戏实例并开始对战
+  const game = new Game();
+  game.playGame();