LEGENDBOSS123

Buckshot Roulette Simulation Javascript

May 11th, 2025 (edited)
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Buckshot Roulette Simulation; Author: LEGENDBOSS123
  2. // Write your strats below
  3. /* -------------------------------------------------------------------- */
  4. // bulletsLeft is the number of bullets left in the game.
  5. // loadedBulletsLeft is the number of loaded bullets left in the game.
  6. // started is a boolean representing if your player started the game.
  7. // Player 1 always goes first, if you want random starting order, set RANDOM_STARTING_ORDER to true.
  8. /* -------------------------------------------------------------------- */
  9.  
  10. var player1Name = "Strat 1";
  11. var player2Name = "Strat 2";
  12.  
  13. var strat1 = function (bulletsLeft, loadedBulletsLeft, started) {
  14.     return SHOOT;
  15. }
  16.  
  17. var strat2 = function (bulletsLeft, loadedBulletsLeft, started) {
  18.     if (bulletsLeft % 2 == 0) {
  19.         return CALL_BLANK;
  20.     }
  21.     return SHOOT;
  22. }
  23.  
  24.  
  25. const NUMBER_GAMES = 1_000_000;
  26. const SHOOT = 0;
  27. const CALL_BLANK = 1;
  28. const NUMBER_BULLETS = 5;
  29. const NUMBER_LOADED_BULLETS = 1;
  30. const RANDOM_STARTING_ORDER = false;
  31.  
  32. /* --------------------------------------------------------------*/
  33. const Player = class {
  34.     constructor(name, strategy) {
  35.         this.name = name;
  36.         this.strategy = strategy;
  37.         this.wins = 0;
  38.         this.lost = false;
  39.         this.started = false;
  40.     }
  41.  
  42. }
  43.  
  44. const player1 = new Player(player1Name, strat1);
  45. const player2 = new Player(player2Name, strat2);
  46.  
  47.  
  48.  
  49.  
  50.  
  51. function fillLoadedBullets(bullets) {
  52.     var loadedBulletsCount = 0;
  53.     while (loadedBulletsCount < NUMBER_LOADED_BULLETS) {
  54.         var bulletIndex = Math.floor(Math.random() * NUMBER_BULLETS);
  55.         if (bullets[bulletIndex] == 0) {
  56.             bullets[bulletIndex] = 1;
  57.             loadedBulletsCount++;
  58.         }
  59.     }
  60. }
  61.  
  62. function getNextBullet(bullets) {
  63.     return bullets.pop();
  64. }
  65.  
  66. function playGame(players) {
  67.     var gameOver = false;
  68.     var currentPlayerIndex = Math.floor(Math.random() * players.length);
  69.     if(!RANDOM_STARTING_ORDER) {
  70.         currentPlayerIndex = 0;
  71.     }
  72.     var currentPlayer = players[currentPlayerIndex];
  73.     var otherPlayer = players[(currentPlayerIndex + 1) % players.length];
  74.     for (var p of players) {
  75.         p.lost = false;
  76.     }
  77.     currentPlayer.started = true;
  78.     otherPlayer.started = false;
  79.     var bullets = Array(NUMBER_BULLETS).fill(0);
  80.     fillLoadedBullets(bullets);
  81.     var loadedBulletsLeft = NUMBER_LOADED_BULLETS;
  82.     var bulletsLeft = NUMBER_BULLETS;
  83.  
  84.     while (!gameOver) {
  85.         var decision = currentPlayer.strategy(bulletsLeft, loadedBulletsLeft, currentPlayer.started);
  86.         var bullet = getNextBullet(bullets);
  87.         if (bullet == 1) {
  88.             loadedBulletsLeft--;
  89.         }
  90.         bulletsLeft--;
  91.  
  92.         if (decision == SHOOT) {
  93.             if (bullet == 1) {
  94.                 otherPlayer.lost = true;
  95.                 gameOver = true;
  96.                 currentPlayer.wins++;
  97.             }
  98.             else {
  99.                 var temp = currentPlayer;
  100.                 currentPlayer = otherPlayer;
  101.                 otherPlayer = temp;
  102.             }
  103.         }
  104.         else if (decision == CALL_BLANK) {
  105.             if (bullet == 1) {
  106.                 currentPlayer.lost = true;
  107.                 gameOver = true;
  108.                 otherPlayer.wins++;
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114. for (var i = 0; i < NUMBER_GAMES; i++) {
  115.     playGame([player1, player2]);
  116. }
  117.  
  118. console.log("-- WINRATE --");
  119. console.log(player1.name + ": " + (player1.wins / NUMBER_GAMES * 100).toFixed(0) + "%");
  120. console.log(player2.name + ": " + (player2.wins / NUMBER_GAMES * 100).toFixed(0) + "%");
Advertisement
Add Comment
Please, Sign In to add comment