|
@@ -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();
|