Guest User

Snakes & Snakes RM

a guest
May 14th, 2023
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1.  
  2. function rollDice(){
  3.     $roll =  random_int(1,6);
  4.     //echo("Rolled: " . $roll . "\n");
  5.     return $roll;
  6. }
  7.  
  8. function runGame(){
  9.     $snakes = array(
  10.         14 => 4,
  11.         17 => 7,
  12.         31 => 9,
  13.         38 => 20,
  14.         54 => 34,
  15.         59 => 40,
  16.         62 => 19,
  17.         64 => 60,
  18.         67 => 51,
  19.         81 => 63,
  20.         84 => 28,
  21.         91 => 71,
  22.         93 => 73,
  23.         95 => 75,
  24.         99 => 78
  25.     );
  26.  
  27.     $moves = 0;
  28.     $fallen = 0;
  29.     $pos = 0;
  30.     for($pos = 0; $pos < 100; $pos += 0){
  31.         //Roll 6 to start
  32.         if($pos == 0){
  33.             if($moves == 0){
  34.                 if(rollDice() != 6 && rollDice() != 6 && rollDice() != 6){
  35.                     $moves ++;
  36.                     continue;
  37.                 }
  38.             }else{
  39.                 if(rollDice() != 6){
  40.                     $moves ++;
  41.                     continue;
  42.                 }
  43.             }
  44.         }
  45.         $roll = rollDice();
  46.         $pos += $roll;
  47.         //Hit 100 exactly, go back when overshooting
  48.         if($pos > 100){
  49.             //echo "From $pos to " . 100 - ($pos - 100);
  50.             $pos = 100 - ($pos-100);
  51.         }
  52.         $moves ++;
  53.         //Fall down the snake ladder
  54.         if( array_key_exists($pos, $snakes) ){
  55.             $moveTo = $snakes[$pos];
  56.             //echo "Oh no! Falling from $pos to $moveTo!\n";
  57.             $pos = $moveTo;
  58.             $fallen++;
  59.         }
  60.         if($roll == 6){
  61.             $moves --;
  62.             continue;
  63.         }
  64.     }
  65.     //echo "YOU WON! Turns: $moves and fallen $fallen times.";
  66.     return [$moves, $fallen];
  67. }
  68.  
  69. $sampleSize = 100000;
  70.  
  71. $totalMoves = [];
  72. $totalFalls = [];
  73. for($i = 0;$i < $sampleSize;$i ++){
  74.     $game = runGame();
  75.     $totalMoves[] = $game[0];
  76.     $totalFalls[] = $game[1];
  77. }
  78. $averageMoves = array_sum($totalMoves) / $sampleSize;
  79. $averageFalls = array_sum($totalFalls) / $sampleSize;
  80.  
  81. $maxMoves = max($totalMoves);
  82. $maxFalls = max($totalFalls);
  83.  
  84. $minMoves = min($totalMoves);
  85. $minFalls = min($totalFalls);
  86.  
  87. echo "Average turns: $averageMoves \n";
  88. echo "Average falls: $averageFalls \n";
  89. echo "Game with most turns: $maxMoves \n";
  90. echo "Game with most falls: $maxFalls \n";
  91. echo "Game with least turns: $minMoves \n";
  92. echo "Game with least falls: $minFalls \n";
Advertisement
Add Comment
Please, Sign In to add comment