function randInt(minInclusive, maxEclusive) {
return Math.floor(minInclusive + (maxEclusive - minInclusive) * Math.random());
}
function randomDirection(inputbuffer) {
var initialDir = randInt(0, 4);
var dir;
for (dir = initialDir; dir < initialDir + 4; dir++) {
// we can only go into a direction that has more than just a wall
if (inputbuffer[dir % 4] != 'W') {
break;
}
}
return dir % 4;
}
function longestDirection(inputbuffer) {
var dir = 0;
var length = inputbuffer[0].length;
for (var i = 1; i < 4; i++) {
if (inputbuffer[i].length > length) {
length = inputbuffer[i].length;
dir = i;
}
}
return dir;
}
function moveToEnd(input) {
var move = '';
for (var i = 0; i < input.length - 1; i++)
move += 'F';
switch (input.charAt(input.length - 1)) {
case 'B':
if (randInt(0, 2) == 0) {
move += 'R';
} else {
move += 'L';
}
break;
case 'L':
move += 'L';
break;
case 'R':
move += 'R';
break;
}
return move;
}
function moveToLeft(input) {
var move = '';
for (var i = 0; i < input.length; i++) {
switch (input.charAt(i)) {
case 'B':
case 'L':
return move + 'L';
default:
if (i < input.length - 1)
move += 'F';
}
}
return move;
}
function moveToRight(input) {
var move = '';
for (var i = 0; i < input.length; i++) {
switch (input.charAt(i)) {
case 'B':
case 'R':
return move + 'R';
default:
if (i < input.length - 1)
move += 'F';
}
}
return move;
}
var steps = ['F', 'R', 'T', 'L'];
var inputbuffer = [];
var input = readline();
if (input == 'Quit') quit();
if (input != 'Start') inputbuffer.push(input);
for (var i = 0; i < 150; i++)
{
// read 5 lines of input
for (var j = inputbuffer.length; j < 5; j++) {
input = readline();
if (input == 'Quit') quit();
inputbuffer.push(input);
}
// select a direction to walk into
var dir;
// randomly choose a method
var method = randInt(0, 2);
switch (method) {
case 0: // method: random corridor
dir = randomDirection(inputbuffer);
break;
case 1: // method: take longest corridor
dir = longestDirection(inputbuffer);
break;
}
var move = steps[dir];
input = inputbuffer[dir];
input = input.substring(0, input.length - 1); // chop off trailing 'W'
// randomly choose a method
var method = randInt(0, 3);
switch (method) {
case 0: // method: walk to end of hallway and turn left or right if possible
move += moveToEnd(input);
break;
case 1: // method: walk until it is possible to turn LEFT or when you reach the end of the corridor
move += moveToLeft(input);
break;
case 2: // method: walk until it is possible to turn RIGHT or when you reach the end of the corridor
move += moveToRight(input);
break;
}
print(move);
inputbuffer = [];
}
syntax highlighted by Code2HTML, v. 0.9.1