mrCage
Member
- Joined
- Jun 17, 2006
- Messages
- 655
Hi
As part of my new FMC website i wrote a PHP scrambler, so that i can generate scrambles automatically and insert to the database, one scramble for each week. I do not prefill the database with scrambles. I just generate new ones when the current last scramble is expired on accessing the site. Anyhow i just wanted to show the code for the scrambler - not the complete website
Note that it does generate valid scrambles without sequences that can eb simplified - like U' D2 U. This is done through simple axis control. I first select a random axis and do either 1 or 2 turns on this axis (1 or 2 randomly). Then select a DIFFERENT axis and 1 or 2 turns again. If i already have length-1 turns in the scramble a single turn is generated for the last axis. This is also how the official .js WCA scrambler works. Feel free to use my scrambler for any website that may make use of it I know the code is not very elegant, but it works!!
It should be easy to translate the code to other programming languages ...
Also it is not hard to extend to bigger cubes. Note how i get bias to doing a single turn on same axis at a time, through the numturns array. This may be adjusted as needed.
- Per
As part of my new FMC website i wrote a PHP scrambler, so that i can generate scrambles automatically and insert to the database, one scramble for each week. I do not prefill the database with scrambles. I just generate new ones when the current last scramble is expired on accessing the site. Anyhow i just wanted to show the code for the scrambler - not the complete website
Code:
function makeScramble($length)
{
/*
$faces = array('U','D,'F','B','R','L')
$taken = array(0,0,0,0,0,0,0);
$numturns = array(1,1,1,1,1,1,1,2,2);
$axis = array(1,1,1,2,2,3,3);
$modifiers = array('',','\'','2');
*/
$faces[1] = 'U';
$faces[2] = 'D';
$faces[3] = 'F';
$faces[4] = 'B';
$faces[5] = 'R';
$faces[6] = 'L';
$taken[1] = 0;
$taken[2] = 0;
$taken[3] = 0;
$taken[4] = 0;
$taken[5] = 0;
$taken[6] = 0;
$numturns[1] = 1;
$numturns[2] = 1;
$numturns[3] = 1;
$numturns[4] = 1;
$numturns[5] = 1;
$numturns[6] = 1;
$numturns[7] = 2;
$numturns[8] = 2;
$modifiers[1] = '';
$modifiers[2] = '2';
$modifiers[3] = '\'';
$axis[1] = 1;
$axis[2] = 1;
$axis[3] = 2;
$axis[4] = 2;
$axis[5] = 3;
$axis[6] = 3;
$prevaxis = -1;
$scramble='';
$totturns = 0;
do {
//select current axis to work on
do {
$curraxis = rand(1,3);
} while ($curraxis == $prevaxis);
$prevaxis = $curraxis; //update previous axis
//select number of turns on this axis
if ($totturns<($length-1)) {
$num = $numturns[rand(1,8)];
} else {
$num = 1;
}
for ($j=1; $j<=$num; $j++) {
do {
$faceindex=rand(1,6);
$modifierindex=rand(1,3);
} while (($axis[$faceindex] != $curraxis) || ($taken[$faceindex] == 1));
$taken[$faceindex] = 1;
$turn = $faces[$faceindex].$modifiers[$modifierindex]." ";
$scramble = $scramble.$turn;
$totturns++;
}
for ($j=1; $j<=6; $j++) {
$taken[$j] = 0;
}
} while ($totturns<$length);
return $scramble;
}
$mylength = 30;
$myscramble = makeScramble($mylength);
...
(applet and/or database code ??)
...
...
Note that it does generate valid scrambles without sequences that can eb simplified - like U' D2 U. This is done through simple axis control. I first select a random axis and do either 1 or 2 turns on this axis (1 or 2 randomly). Then select a DIFFERENT axis and 1 or 2 turns again. If i already have length-1 turns in the scramble a single turn is generated for the last axis. This is also how the official .js WCA scrambler works. Feel free to use my scrambler for any website that may make use of it I know the code is not very elegant, but it works!!
It should be easy to translate the code to other programming languages ...
Also it is not hard to extend to bigger cubes. Note how i get bias to doing a single turn on same axis at a time, through the numturns array. This may be adjusted as needed.
- Per
Last edited: