function iterateKeys(availableRooms, keys, testAllRooms) 409 { 410 //Note: testAllRooms is a Boolean; player will start over and over in each room instead of randomly 411 //To use this, you must send in a room set, keys, and true. You can use [] to send in no keys. 412 //iterateKeys(rooms, [], true); //this would test the global rooms object, no keys to start, making sure all rooms are tested as starting positions 413 414 /* ************************************************************************************* 415 //For this function to work, the *** checkKeys *** "return" values need to be changed. 416 //Go into the checkKeys() function and... 417 // Change all "return false;" to "return 0;" 418 // Change all "return true;" to "return 1;", except... 419 // Set the "return true;" in the nested loop, when all rooms are unlocked, to "return i;", the iterator 420 //**DON'T USE QUOTES WHEN CHANGING THOSE RETURN VALUES.** 421 ************************************************************************************* */ 422 423 //If you don't change those values, you will just get the elapsed time and the open-all-rooms success rate. 424 //Using these values should let the rest of the code to function without problems. 425 //But ideally, change them back to Booleans when you're finished testing with this function. 426 427 //How many runs do you want to do? 428 var iterations = 10000; //The higher the number, the more likely the browser will crash or hang. 429 430 //Make sure you have a set to test. If you don't send in keys, the player won't start with any. 431 availableRooms = availableRooms || rooms; 432 keys = keys || []; 433 434 //For tracking room counts in the house and places player started in 435 var startRooms = []; 436 var successRooms = []; 437 var successes = 0; 438 439 //To pick random starting rooms 440 var roomArray = []; 441 var index = 0; 442 443 //trackers to count total and average iterations per solution check 444 var its; //number of iterations returned from the checkKey function 445 var max = 0; 446 var total = 0; 447 448 //to track the amount of time passed for the entire iteration count 449 var start = new Date(); 450 451 //get the list of rooms we can pull from easily 452 for (var r in availableRooms) 453 { 454 roomArray.push(availableRooms[r]); 455 } 456 457 //run the iterations 458 for (var i = 0; i < iterations; i++) 459 { 460 if (testAllRooms) 461 { 462 //cycle through each room 463 index = i % roomArray.length; 464 } 465 else 466 { 467 //pick a random room 468 index = Math.floor(Math.random() * roomArray.length); 469 } 470 //set the player into the room 471 player.currentRoom = roomArray[index]; 472 473 //keep track of which player we put the player into, but just one copy 474 if (startRooms.indexOf(player.currentRoom.name) === -1) 475 { 476 startRooms.push(player.currentRoom.name); 477 } 478 479 //reset the player's starting keys for each iteration, if any 480 player._tempkeys = keys; 481 482 //"its" is the number of iterations returned by the checkKeys function 483 //if you changed the return values to 0/1/i as described at the top of this function 484 its = checkKeys(availableRooms, player.currentRoom.name, false); 485 486 //if tou left the true/false return values, we need to skip the math of these counters 487 if (typeof its === "number") 488 { 489 total += its; //total number of iterations so we can report the average later 490 max = Math.max(its, max); //maximum number of iterations from the whole set we're testing 491 } 492 493 //if "its" is a number greater than zero or the Boolean true, then this evaluates as true 494 //it means the doors were all unlocked 495 if (its) 496 { 497 successes++; //number of times the doors were were all opened 498 499 //track where the player started so you can figure out a good place to place the player if you need help 500 //once you know the successful rooms, set one to the player for your game and turn off randomizePlayerStart 501 //or, convert this list of rooms to a rooms set and send that to the randomizePlayerStart to still have some randomization 502 if (successRooms.indexOf(player.currentRoom.name) === -1) 503 { 504 successRooms.push(player.currentRoom.name ); 505 } 506 } 507 }