textBasicMain.js
1    "use strict"; 
2    var rooms; 
3    var player; 
4    startGame(); 
5     
6
function startGame() 7 { 8 //reset the global rooms and player objects 9 rooms = getRooms(); 10 player = getPlayer(); 11 12 //This explains the game to a new player 13 var text = "Welcome to the Room Adventure!"; 14 text += " You are " + player.name; 15 text += " and you are in a house"; 16 text += " where many things are broken."; 17 text += " Go from room to room"; 18 text += " to find the items you need"; 19 text += " to fix what's broken."; 20 text += " Earn points for fixing things."; 21 text += " There are " + player.itemsLeftToFix; 22 text += " things that need to be fixed."; 23 text += " You start in the "; 24 text += player.currentRoom.name + "."; 25 text += " Good luck!"; 26 alert(text); 27 28 //move the player into their current room 29 //to display the description and start the game 30 moveToRoom(); 31 } 32
33
function getPlayer() 34 { 35 var player = 36 { 37 name: "Lica", 38 score: 0, 39 currentRoom: rooms["living room"], 40 inventory: [], 41 itemsLeftToFix: 10 42 }; 43 return player; 44 } 45
46
function moveToRoom() 47 { 48 textRoomDescription(); 49 isThereAnItem(); 50 fixBrokenThing(); 51 showScore(); 52 showInventory(); 53 54 if (checkGameOver()) //watch the parentheses 55 { 56 alertGameWon(); 57 checkPlayAgain(); 58 } 59 else //ask for a direction 60 { 61 var direction = getDirection(); 62 if (direction) 63 { 64 player.currentRoom = rooms[direction]; 65 moveToRoom(); 66 } 67 } 68 } 69
70
function textRoomDescription() 71 { 72 var text = ""; 73 text += "You are in the "; 74 text += player.currentRoom.name + ". "; 75 text += player.currentRoom.description; 76 alert(text); 77 } 78
79
function isThereAnItem() 80 { 81 var item = player.currentRoom.itemFound; 82 if (item) 83 { 84 alert("You found the " + item + "!"); 85 player.inventory.push(item); 86 player.currentRoom.itemFound = null; 87 } 88 } 89
90
function fixBrokenThing() 91 { 92 //helper variables to make the code easier to read 93 var brokenThing = player.currentRoom.brokenThing; 94 var fixWith = player.currentRoom.fixWith; 95 96 //test: Is there a broken thing? 97 if (brokenThing) 98 { 99 //get ready to announce there's a broken thing 100 var text = "There is a broken "; 101 text += brokenThing + " in this room. "; 102 103 //helper variable 104 var index = player.inventory.indexOf(fixWith); 105 106 //test: if fixWith is NOT in inventory 107 if (index === -1) 108 { 109 text += "You need the " + fixWith; 110 text += " to fix it."; 111 } 112 else //the item IS in the inventory 113 { 114 text += "You fixed the " + brokenThing; 115 text += " with the " + fixWith + "!"; 116 text += " You earn "; 117 text += player.currentRoom.points; 118 text += " points."; 119 120 player.currentRoom.brokenThing = null; 121 player.score += player.currentRoom.points; 122 player.itemsLeftToFix--; 123 player.inventory.splice(index, 1); 124 } 125 alert(text); 126 } 127 } 128
129
function showScore() 130 { 131 player.score = Math.max(0, player.score); 132 alert("Score: " + player.score); 133 } 134
135
function showInventory() 136 { 137 var text = "Inventory: "; 138 139 var length = player.inventory.length; 140 141 for (var i = 0; i < length; i++) 142 { 143 text += "["; 144 text += player.inventory[i]; 145 text += "] "; 146 } 147 148 alert(text); 149 } 150
151
function checkGameOver() 152 { 153 return player.itemsLeftToFix === 0; 154 } 155
156
function alertGameWon() 157 { 158 var text = "Congratulations, " + player.name +"! "; 159 text += "You fixed everything in the house! "; 160 text += "You should be proud of yourself! "; 161 text += "You finished the game with a score of "; 162 text += player.score + " points! "; 163 text += "Play again soon!"; 164 alert(text); 165 } 166
167
function checkPlayAgain() 168 { 169 var text = "Would you like to play again? "; 170 text += "Click OK to replay. "; 171 text += "Click CANCEL to end. "; 172 173 var again = confirm(text); 174 if (again) 175 { 176 startGame(); 177 } 178 } 179
180
function getDirection() 181 { 182 var text = "Which way do you want to go? "; 183 var direction; 184 185 while (!direction) 186 { 187 text += "There are exits: "; 188 var north = player.currentRoom["north"]; 189 if (rooms[north]) 190 { 191 text += " north "; 192 } 193 var south = player.currentRoom["south"]; 194 if (rooms[south]) 195 { 196 text += " south "; 197 } 198 var east = player.currentRoom["east"]; 199 if (rooms[east]) 200 { 201 text += " east "; 202 } 203 var west = player.currentRoom["west"]; 204 if (rooms[west]) 205 { 206 text += " west "; 207 } 208 var northeast = player.currentRoom["northeast"]; 209 if (rooms[northeast]) 210 { 211 text += " northeast "; 212 } 213 var southeast = player.currentRoom["southeast"]; 214 if (rooms[southeast]) 215 { 216 text += " southeast "; 217 } 218 var northwest = player.currentRoom["northwest"]; 219 if (rooms[northwest]) 220 { 221 text += " northwest "; 222 } 223 var southwest = player.currentRoom["southwest"]; 224 if (rooms[southwest]) 225 { 226 text += " southwest "; 227 } 228 229 direction = prompt(text); 230 direction = direction.toLowerCase(); 231 if (direction === "name") 232 { 233 continue; 234 } 235 236 var exitTo = player.currentRoom[direction]; 237 238 if (rooms[exitTo]) 239 { 240 //we CAN go this way, send back the exitTo 241 return exitTo; 242 } 243 else if (direction === "quit") 244 { 245 break; 246 } 247 text = "You can't go " + direction + ". "; 248 text += "Please try again. "; 249 text += "Use compass points like north."; 250 direction = null; 251 } 252 } 253
254
function getRooms() 255 { 256 var livingRoom = 257 { 258 name: "living room", 259 brokenThing: "fireplace screen", 260 description: "A cozy room with a fireplace.", 261 fixWith: "new wire", 262 points: 25, 263 itemFound: "batteries", 264 north: "dining room", 265 south: null, 266 east: "hallway", 267 west: null 268 }; 269 270 var diningRoom = 271 { 272 name: "dining room", 273 description: "A great place to enjoy a meal.", 274 brokenThing: "chandelier", 275 fixWith: "light bulb", 276 points: 15, 277 itemFound: "new wire", 278 north: null, 279 south: "living room", 280 east: "kitchen", 281 west: null 282 }; 283 284 var kitchen = 285 { 286 name: "kitchen", 287 description: "It needs a little attention, but the kitchen has everything you need to have a snack or host a huge party.", 288 brokenThing: "faucet", 289 fixWith: "wrench", 290 points: 35, 291 itemFound: "package with color ink", 292 north: null, 293 south: "hallway", 294 east: "pantry", 295 west: "dining room" 296 }; 297 298 var hallway = 299 { 300 name: "hallway", 301 description: "The hallway helps make the house feel grand, though the old carpet curls up and it's easy to trip over.", 302 brokenThing: "rug", 303 fixWith: "special carpet tape", 304 points: 45, 305 itemFound: "light bulb", 306 north: "kitchen", 307 south: "basement", 308 east: "office", 309 west: "living room", 310 northeast: "bathroom", 311 southeast: "den" 312 }; 313 314 var bathroom = 315 { 316 name: "bathroom", 317 description: "You take pride in your pristine bathroom. It's a relaxing place to take care of necessities.", 318 brokenThing: "mirror", 319 fixWith: "new mirror", 320 points: 20, 321 itemFound: "screwdriver", 322 north: null, 323 south: null, 324 east: null, 325 west: null, 326 southwest: "hallway" 327 }; 328 329 var office = 330 { 331 name: "office", 332 description: "This place is a mess. It's a wonder you ever get any work done in here.", 333 brokenThing: "color printer", 334 fixWith: "package with color ink", 335 points: 40, 336 itemFound: "garbage bag", 337 north: null, 338 south: null, 339 east: null, 340 west: "hallway" 341 }; 342 343 var basement = 344 { 345 name: "basement", 346 description: "You hide your eyes behind your hands so you don't have to see everything that's out of place down here.", 347 brokenThing: "door hinge", 348 fixWith: "screwdriver", 349 points: 30, 350 itemFound: "catnip", 351 north: "hallway", 352 south: null, 353 east: null, 354 west: null 355 }; 356 357 var den = 358 { 359 name: "den", 360 description: "The den is a comfortable spot to watch TV and catch up on the latest movies.", 361 brokenThing: "TV remote", 362 fixWith: "batteries", 363 points: 10, 364 itemFound: "wrench", 365 northwest: "hallway", 366 north: null, 367 south: "cat den", 368 east: null, 369 west: null 370 }; 371 372 var catDen = 373 { 374 name: "cat den", 375 description: "An offshoot of another room, the cat den is a place the cats come to play, nap, and meow merrily.", 376 brokenThing: "cat toy", 377 fixWith: "catnip", 378 points: 100, 379 itemFound: "new mirror", 380 north: "den", 381 south: null, 382 east: null, 383 west: null 384 }; 385 386 var pantry = 387 { 388 name: "pantry", 389 description: "You have all shelves organized so you can find the food supplies you need.", 390 brokenThing: "box of spaghetti", 391 fixWith: "garbage bag", 392 points: 15, 393 itemFound: "special carpet tape", 394 north: null, 395 south: null, 396 east: null, 397 west: "kitchen" 398 }; 399 400 var rooms = {}; 401 rooms[livingRoom.name] = livingRoom; 402 rooms[diningRoom.name] = diningRoom; 403 rooms[kitchen.name] = kitchen; 404 rooms[hallway.name] = hallway; 405 rooms[bathroom.name] = bathroom; 406 rooms[office.name] = office; 407 rooms[basement.name] = basement; 408 rooms[den.name] = den; 409 rooms[catDen.name] = catDen; 410 rooms[pantry.name] = pantry; 411 412 return rooms; 413 } 414