1 "use strict"; 2 3 //Feature: Create Passcode Ending 4 function getPasscode(availableRooms) 5 { 6 //reset the global password object 7 passcode = 8 { 9 reward: 100, 10 penalty: 25, 11 exitCode: "", //full unlock code 12 codes: [], //pieces of the code 13 rooms: [] //list of rooms with passcode pieces 14 }; 15 16 var numberOfCodes = 3; 17 var digitsPerCode = 3; 18 19 //create a set of hints 20 var clues = ["apples", "berries", "cherries", "dragon fruits", "emu berries", "forest strawberries", 21 "golden apples", "honeydews", "ilamas", "junglesop", "kumquats", "lemons", "mandarins", "nectarines", 22 "olives", "papaya", "quince", "rangpurs", "strawberries", "tomato", "vanilla", "watermelon", 23 "youngberry", "zucchini"]; 24 25 var hints = []; 26 for (var i = 0; i < numberOfCodes; i++) 27 { 28 var index = Math.floor(Math.random() * clues.length); 29 hints.push(clues[index]); 30 clues.splice(index, 1); 31 } 32 hints.sort(); //alphabetize the list 33 34 //get the codes we need 35 for (var i = 0; i < numberOfCodes; i++) 36 { 37 var code = ""; 38 for (var j = 0; j < digitsPerCode; j++) 39 { 40 code += Math.floor(Math.random() * 10).toString(); 41 } 42 var hint = code + " " + hints[i]; 43 passcode.codes.push(hint); 44 passcode.exitCode += code; 45 } 46 47 48 //create an array of available rooms 49 var roomsArray = []; 50 for (var roomName in availableRooms) 51 { 52 var roomToAdd = availableRooms[roomName]; 53 if (roomToAdd.brokenThing) 54 { 55 roomsArray.push(roomToAdd); 56 } 57 } 58 59 //put codes into rooms 60 for (var i = 0; i < passcode.codes.length; i++) 61 { 62 var index = Math.floor(Math.random() * roomsArray.length); 63 roomsArray[index].passcodeHint = passcode.codes[i]; 64 passcode.rooms.push(roomsArray[index]); 65 roomsArray.splice(index, 1); 66 } 67 return passcode; 68 } 69
70 function enterPasscode() 71 { 72 display.clear(); 73 var text = "Enter the correct passcode to exit the house."; 74 display.broken(text); 75 showInventory(true); //true disables item usage 76 77 player.guessPasscode = ""; 78 79 //make passcode buttons 80 var buttons = ""; 81 var digits = [7, 8, 9, 4, 5, 6, 1, 2, 3, 0]; 82 var toggle = false; 83 84 for (var i = 0; i < digits.length; i++) 85 { 86 buttons += "<button "; 87 88 if (toggle) 89 { 90 buttons += "class='even' "; 91 } 92 else 93 { 94 buttons += "class='odd' "; 95 } 96 toggle = !toggle; 97 98 buttons += "onclick='addToPasscode(" + digits[i] + ")'>" + digits[i] + "</button>"; 99 100 //insert a line break after 3 buttons 101 if (i % 3 === 2) 102 { 103 buttons += "<br>"; 104 } 105 } 106 107 buttons += "<br><button id='passcode' onclick='checkPasscode()'>Try Passcode</button>"; 108 109 display.navigation(buttons); 110 display.image(houseImages.exitSign); 111 } 112
113 function addToPasscode(digit) 114 { 115 player.guessPasscode += digit.toString(); 116 display.broken("<strong>Passcode:</strong> " + player.guessPasscode); 117 } 118
119 function checkPasscode() 120 { 121 var text = ""; 122 123 if (player.guessPasscode === passcode.exitCode) 124 { 125 alertGameWon(); 126 checkPlayAgain(); 127 } 128 else 129 { 130 text += "<h3>The door does not open. "; 131 text += "You lose " + passcode.penalty + " points.</h3>"; 132 player.score -= passcode.penalty; 133 display.clear(); 134 display.found(text); 135 136 var button = "<button class='text' onclick='moveToRoom()'>OK</button>"; 137 138 display.navigation(button); 139 display.image(houseImages.exitSign); 140 } 141 } 142