Advertisement
YaroslavPodorvanov

PlayRedWithBlue

Feb 6th, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // declare const Math: any;
  2.  
  3. class Power {
  4.     private from: number;
  5.     private to: number;
  6.     private distance: number;
  7.  
  8.     constructor(from: number, to: number) {
  9.         this.from = from;
  10.         this.to = to;
  11.         this.distance = this.to - this.from + 1;
  12.     }
  13.  
  14.     // https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript
  15.     public random(): number {
  16.         return this.from + Math.floor(Math.random() * this.distance);
  17.     }
  18. }
  19.  
  20. class Result {
  21.     public readonly red: number;
  22.     public readonly blue: number;
  23.     public readonly tie: number;
  24.     public readonly total: number;
  25.  
  26.     constructor(red: number, blue: number, tie: number, total: number) {
  27.         this.red = red;
  28.         this.blue = blue;
  29.         this.tie = tie;
  30.         this.total = total;
  31.     }
  32. }
  33.  
  34. class Player {
  35.     private life: number;
  36.     private readonly power: Power;
  37.  
  38.     constructor(life: number, power: Power) {
  39.         this.life = life;
  40.         this.power = power;
  41.     }
  42.  
  43.     public nextPower(): number {
  44.         return this.power.random();
  45.     }
  46.  
  47.     public changeLife(diff: number) {
  48.         this.life += diff;
  49.     }
  50.  
  51.     public alive() {
  52.         return this.life > 0;
  53.     }
  54. }
  55.  
  56. class PlayerBuilder {
  57.     private life: number;
  58.     private readonly power: Power;
  59.  
  60.     constructor(life: number, from: number, to: number) {
  61.         this.life = life;
  62.         this.power = new Power(from, to);
  63.     }
  64.  
  65.     public build(): Player {
  66.         return new Player(this.life, this.power);
  67.     }
  68. }
  69.  
  70. function kick(a: Player, b: Player) {
  71.     a.changeLife(-b.nextPower());
  72. }
  73.  
  74. function test(count: number, redPlayerBuilder: PlayerBuilder, bluePlayerBuilder: PlayerBuilder): Result {
  75.     const winCountMap = {
  76.         "red": 0,
  77.         "blue": 0,
  78.     };
  79.  
  80.     let tieCount = 0;
  81.  
  82.     for (let i = 0; i < count; i++) {
  83.         const redPlayer = redPlayerBuilder.build();
  84.         const bluePlayer = bluePlayerBuilder.build();
  85.  
  86.         while (redPlayer.alive() && bluePlayer.alive()) {
  87.             kick(redPlayer, bluePlayer);
  88.             kick(bluePlayer, redPlayer);
  89.         }
  90.  
  91.         const red = redPlayer.alive();
  92.         const blue = bluePlayer.alive();
  93.  
  94.         if (red) {
  95.             winCountMap["red"] += 1;
  96.         } else if (blue) {
  97.             winCountMap["blue"] += 1;
  98.         } else {
  99.             tieCount += 1;
  100.         }
  101.     }
  102.  
  103.     return new Result(
  104.         winCountMap["red"],
  105.         winCountMap["blue"],
  106.         tieCount,
  107.         count
  108.     );
  109. }
  110.  
  111. (function () {
  112.     const tries = [
  113.         1,
  114.         2,
  115.         4,
  116.         8,
  117.         16,
  118.         32,
  119.         64,
  120.         100,
  121.         1000,
  122.         10000,
  123.         100000,
  124.     ];
  125.  
  126.     const redPlayerBuilder = new PlayerBuilder(100, 10, 15);
  127.     const bluePlayerBuilder = new PlayerBuilder(90, 8, 20);
  128.  
  129.     for (let i = 0; i < tries.length; i++) {
  130.         const count = tries[i];
  131.  
  132.         const result = test(
  133.             count,
  134.             redPlayerBuilder,
  135.             bluePlayerBuilder,
  136.         );
  137.  
  138.         console.log(`Count: ${count}, red: ${result.red}, blue: ${result.blue}, tie: ${result.tie}, red: ${result.red * 100 / count} %`);
  139.     }
  140. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement