hid_msrpuwgj56ccf29 1 year ago
parent
commit
11f031d23a
3 changed files with 81 additions and 81 deletions
  1. 0 0
      task/ai-frontend/task-comp.html
  2. 1 1
      task/ai-tslang/task-tsgame.js
  3. 80 80
      task/ai-tslang/task-tsgame.ts

+ 0 - 0
task/ai-frontend/task-comp.html


+ 1 - 1
task/ai-tslang/task-tsgame.js

@@ -13,7 +13,7 @@ var Game = /** @class */ (function () {
         this.players = [new Player(), new Player()];
     }
     Game.prototype.playGame = function () {
-        console.log('游戏开始~');
+        console.log('游戏开始');
         while (this.players[0].life > 0 && this.players[1].life > 0) {
             this.playRound();
         }

+ 80 - 80
task/ai-tslang/task-tsgame.ts

@@ -1,87 +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();
+    life: number;
+    choices: string[];
+  
+    constructor() {
+      this.life = 3;
+      this.choices = ['石头', '剪刀', '布'];
     }
-    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--;
+  
+    makeChoice(): number {
+      return Math.floor(Math.random() * 3) + 1;
     }
-
-    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;
+  
+  class Game {
+    players: Player[];
+  
+    constructor() {
+      this.players = [new Player(), new Player()];
     }
-  }
-
-  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赢了!');
+  
+    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('游戏结束!');
     }
   }
-
-  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();
+  
+  // 创建游戏实例并开始对战
+  const game = new Game();
+  game.playGame();