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