100 prisoners: Difference between revisions

SuperCollider solution to Rosetta Code TASK: 100 prisoners
(Add Ecstasy example)
(SuperCollider solution to Rosetta Code TASK: 100 prisoners)
Line 6,588:
<pre>Optimal: 31.26 %
Random: 0 %</pre>
 
 
 
 
=={{header|SuperCollider }}==
{{works with|SuperCollider|3.13.0}}
Submitted to Rosetta Code 2024-06-08 by: MusicCoder.
<syntaxhighlight lang="SuperCollider">
// ==========================================================================
// START-SuperCollider solution to Rosetta Code TASK: 100 prisoners ## BY: MusicCoder : 2024-06-08 ##
// ==========================================================================
 
(
/* ## BY: MusicCoder : 2024-06-08 ##
https://rosettacode.org/wiki/100_prisoners
THE TASK: Simulate the 100-prisoners game and compare two strategies
1: random choice, 2: follow the cards.
 
*** My preferance - make a function to play the game
... and feed it a function for each strategy ***
Use 0..99 rather than 1..100 for player, card and drawer numbers
 
What can a player possibly know?
1. Player number
2. Current turn number
3. Previous drawer picked : -1 means no previous as yet
4. Previous card value : -1 means no previous as yet
5. Number of drawers ( PROBABLY == number of players)
6. Number of turns
 
--- These parameters should be good for a lot of different strategies ---
 
Statergy function will get called with values for these six parameters
Statergy function should return a drawerNumber
*/
// =============================================================================
// *** THE REQUIRED STRATEGY FUNCTIONS ***
// =============================================================================
// strategyRandom: Pick a drawer number at random
var strategyRandom = {
|turnNumber, previousDrawer, previousCard, playerNumber, numDrawers, numTurns|
 
var drawerPicked = numDrawers.rand;
drawerPicked;
};
// =============================================================================
// strategyFollow: Start at drawerNumber = playerNumber, then follow the cardNumbers
var strategyFollow = {
|turnNumber, previousDrawer, previousCard, playerNumber, numDrawers, numTurns|
 
var drawerPicked = if ( (previousCard == -1), {playerNumber}, {previousCard});
drawerPicked;
};
 
// =============================================================================
/* ### playGame: Function to play the game ###
// =============================================================================
Two loops: outer loop over players, inner loop over turns for each player
The core of the inner loop calls the supplied stratergy function
... passing in values for the six parameters:
turnNumber, previousDrawer, previousCard, playerNumber, numDrawers, numTurns
The stratergy function responds with the number of the drawer it has picked
... for the current turn
*/
var playGame = {
|strategy, strategyName="", numPlayers=100, numDrawers=100, numTurns=50, numSims=10000|
var percentWins=0;
var countWins=0;
var drawers=Array.iota(numPlayers); // fill array with player numbers 0..99
 
numSims.do{
var playerWon = true; // as soon as one player fails - exit this simulation
var playerNumber = 0; // first player
 
drawers = drawers.scramble; // randomly reorder the cards in the drawers
 
while(// loop until the last player or until any player fails
{playerNumber<numPlayers && playerWon},
{ // *** call the supplied strategy function numTurns times --- for the current player***
var drawerPicked = -1;
var cardPicked = -1;
var turnNumber = 0;
var found = false;
 
while ( // loop through numTurns, but exit if player card found
{(turnNumber<numTurns) && not(found)},
{
drawerPicked = strategy.( // call the supplied strategy function
turnNumber, drawerPicked, cardPicked, playerNumber, numDrawers, numTurns);
 
cardPicked = drawers[drawerPicked];
 
found = playerNumber == cardPicked;
 
turnNumber = turnNumber + 1;
};);
 
playerWon = found; // did this player find his card
 
playerNumber = playerNumber + 1; // move to the next player
};); // end of INNER while loop
 
if (playerWon) {countWins = countWins + 1};
}; // end of OUTER while loop -- end of all simulations
 
percentWins = 100 * (countWins / numSims);
postf("For: % number of wins was=% in % simulations = %\\% wins\n",
strategyName, countWins, numSims, percentWins);
};
playGame.(strategyRandom, "strategyRandom");
playGame.(strategyFollow, "strategyFollow");
"\n";// interpreter posts the value of the last expression - so make that just a blank line
// =============================================================================
)
 
// ==========================================================================
// **END-SuperCollider solution to Rosetta Code TASK: 100 prisoners ## BY: MusicCoder : 2024-06-08 ##
// ==========================================================================
</syntaxhighlight>
{{out}}
<pre>
For: strategyRandom number of wins was=0 in 10000 simulations = 0.0% wins
For: strategyFollow number of wins was=3051 in 10000 simulations = 30.51% wins
</pre>
 
 
 
 
=={{header|Swift}}==
10

edits