Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /* Get Programming with JavaScript
  12. * Listing 9.12
  13. * A Player constructor function
  14. */
  15.  
  16. // The spacer namespace
  17.  
  18. var spacer = {
  19. blank: function () {
  20. return "";
  21. },
  22.  
  23. newLine: function () {
  24. return "\n";
  25. },
  26.  
  27. line: function (length, character) {
  28. var longString = "****************************************";
  29. longString += "----------------------------------------";
  30. longString += "========================================";
  31. longString += "++++++++++++++++++++++++++++++++++++++++";
  32. longString += " ";
  33.  
  34. length = Math.max(0, length);
  35. length = Math.min(40, length);
  36. return longString.substr(longString.indexOf(character), length);
  37. },
  38.  
  39. wrap : function (text, length, character) {
  40. var padLength = length - text.length - 3;
  41. var wrapText = character + " " + text;
  42. wrapText += spacer.line(padLength, " ");
  43. wrapText += character;
  44. return wrapText;
  45. },
  46.  
  47. box: function (text, length, character) {
  48. var boxText = spacer.newLine();
  49. boxText += spacer.line(length, character) + spacer.newLine();
  50. boxText += spacer.wrap(text, length, character) + spacer.newLine();
  51. boxText += spacer.line(length, character) + spacer.newLine();
  52. return boxText;
  53. }
  54. };
  55.  
  56.  
  57. // The Place constructor
  58.  
  59. var Place = function (title, description) {
  60. var newLine = spacer.newLine();
  61.  
  62. this.title = title;
  63. this.description = description;
  64. this.items = [];
  65. this.exits = [];
  66.  
  67. this.getItemsInfo = function () {
  68. var itemsString = "Items: " + newLine;
  69. this.items.forEach(function (item) {
  70. itemsString += " - " + item;
  71. itemsString += newLine;
  72. });
  73. return itemsString;
  74. };
  75.  
  76. this.getExitsInfo = function () {
  77. var exitsString = "Exits from " + this.title;
  78. exitsString += ":" + newLine;
  79.  
  80. this.exits.forEach(function (exit) {
  81. exitsString += " - " + exit.title;
  82. exitsString += newLine;
  83. });
  84.  
  85. return exitsString;
  86. };
  87.  
  88. this.getTitleInfo = function () {
  89. return spacer.box(
  90. this.title,
  91. this.title.length + 4,
  92. "="
  93. );
  94. };
  95.  
  96. this.getInfo = function () {
  97. var infoString = this.getTitleInfo();
  98. infoString += this.description;
  99. infoString += newLine + newLine;
  100. infoString += this.getItemsInfo() + newLine;
  101. infoString += this.getExitsInfo();
  102. infoString += spacer.line(40, "=") + newLine;
  103. return infoString;
  104. };
  105.  
  106.  
  107. this.showInfo = function () {
  108. console.log(this.getInfo());
  109. };
  110.  
  111. this.addItem = function (item) {
  112. this.items.push(item);
  113. };
  114.  
  115. this.addExit = function (exit) {
  116. this.exits.push(exit);
  117. };
  118. };
  119.  
  120.  
  121. // The Player constructor
  122.  
  123. var Player = function (name, health) {
  124. var newLine = spacer.newLine();
  125.  
  126. this.name = name;
  127. this.health = health;
  128. this.items = [];
  129. this.place = null;
  130.  
  131. this.addItem = function (item) {
  132. this.items.push(item);
  133. };
  134.  
  135. this.getNameInfo = function () {
  136. return this.name;
  137. };
  138.  
  139. this.getHealthInfo = function () {
  140. return this.name + " has health " + this.health;
  141. };
  142.  
  143. this.getPlaceInfo = function () {
  144. return this.name + " is in " + this.place.title;
  145. };
  146.  
  147. this.getItemsInfo = function () {
  148. var itemsString = "Items:" + newLine;
  149.  
  150. this.items.forEach(function (item, i) {
  151. itemsString += " - " + item + newLine;
  152. });
  153.  
  154. return itemsString;
  155. };
  156.  
  157. this.getInfo = function (character) {
  158. var place = this.getPlaceInfo();
  159. var health = this.getHealthInfo();
  160. var longest = Math.max(place.length, health.length) + 4;
  161.  
  162. var info = spacer.box(this.getNameInfo(), longest, character);
  163. info += spacer.wrap(place, longest, character);
  164. info += spacer.newLine() + spacer.wrap(health, longest, character);
  165. info += newLine + spacer.line(longest, character);
  166.  
  167. info += newLine;
  168. info += " " + this.getItemsInfo();
  169. info += newLine;
  170. info += spacer.line(longest, character);
  171. info += newLine;
  172.  
  173. return info;
  174. };
  175.  
  176. this.showInfo = function (character) {
  177. console.log(this.getInfo(character));
  178. };
  179. };
  180.  
  181.  
  182. // Testing Player
  183.  
  184. var library = new Place(
  185. "The Old Library",
  186. "You are in a library. Dusty books line the walls."
  187. );
  188.  
  189. var player1 = new Player("Kandra", 50);
  190. player1.place = library;
  191. player1.addItem("a rusty key");
  192. player1.addItem("The Sword of Doom");
  193.  
  194. player1.showInfo("=");
  195.  
  196.  
  197.  
  198. /* Further Adventures
  199. *
  200. * 1) Test out the constructor by
  201. * creating a couple of Player objects.
  202. *
  203. * 2) Add items for each player.
  204. *
  205. * 3) Display each player's info.
  206. *
  207. * 4) Write a method to drop the last
  208. * item in a player's items array.
  209. *
  210. */
  211. </script>
  212.  
  213.  
  214.  
  215. <script id="jsbin-source-javascript" type="text/javascript">/* Get Programming with JavaScript
  216. * Listing 9.12
  217. * A Player constructor function
  218. */
  219.  
  220. // The spacer namespace
  221.  
  222. var spacer = {
  223. blank: function () {
  224. return "";
  225. },
  226.  
  227. newLine: function () {
  228. return "\n";
  229. },
  230.  
  231. line: function (length, character) {
  232. var longString = "****************************************";
  233. longString += "----------------------------------------";
  234. longString += "========================================";
  235. longString += "++++++++++++++++++++++++++++++++++++++++";
  236. longString += " ";
  237.  
  238. length = Math.max(0, length);
  239. length = Math.min(40, length);
  240. return longString.substr(longString.indexOf(character), length);
  241. },
  242.  
  243. wrap : function (text, length, character) {
  244. var padLength = length - text.length - 3;
  245. var wrapText = character + " " + text;
  246. wrapText += spacer.line(padLength, " ");
  247. wrapText += character;
  248. return wrapText;
  249. },
  250.  
  251. box: function (text, length, character) {
  252. var boxText = spacer.newLine();
  253. boxText += spacer.line(length, character) + spacer.newLine();
  254. boxText += spacer.wrap(text, length, character) + spacer.newLine();
  255. boxText += spacer.line(length, character) + spacer.newLine();
  256. return boxText;
  257. }
  258. };
  259.  
  260.  
  261. // The Place constructor
  262.  
  263. var Place = function (title, description) {
  264. var newLine = spacer.newLine();
  265.  
  266. this.title = title;
  267. this.description = description;
  268. this.items = [];
  269. this.exits = [];
  270.  
  271. this.getItemsInfo = function () {
  272. var itemsString = "Items: " + newLine;
  273. this.items.forEach(function (item) {
  274. itemsString += " - " + item;
  275. itemsString += newLine;
  276. });
  277. return itemsString;
  278. };
  279.  
  280. this.getExitsInfo = function () {
  281. var exitsString = "Exits from " + this.title;
  282. exitsString += ":" + newLine;
  283.  
  284. this.exits.forEach(function (exit) {
  285. exitsString += " - " + exit.title;
  286. exitsString += newLine;
  287. });
  288.  
  289. return exitsString;
  290. };
  291.  
  292. this.getTitleInfo = function () {
  293. return spacer.box(
  294. this.title,
  295. this.title.length + 4,
  296. "="
  297. );
  298. };
  299.  
  300. this.getInfo = function () {
  301. var infoString = this.getTitleInfo();
  302. infoString += this.description;
  303. infoString += newLine + newLine;
  304. infoString += this.getItemsInfo() + newLine;
  305. infoString += this.getExitsInfo();
  306. infoString += spacer.line(40, "=") + newLine;
  307. return infoString;
  308. };
  309.  
  310.  
  311. this.showInfo = function () {
  312. console.log(this.getInfo());
  313. };
  314.  
  315. this.addItem = function (item) {
  316. this.items.push(item);
  317. };
  318.  
  319. this.addExit = function (exit) {
  320. this.exits.push(exit);
  321. };
  322. };
  323.  
  324.  
  325. // The Player constructor
  326.  
  327. var Player = function (name, health) {
  328. var newLine = spacer.newLine();
  329.  
  330. this.name = name;
  331. this.health = health;
  332. this.items = [];
  333. this.place = null;
  334.  
  335. this.addItem = function (item) {
  336. this.items.push(item);
  337. };
  338.  
  339. this.getNameInfo = function () {
  340. return this.name;
  341. };
  342.  
  343. this.getHealthInfo = function () {
  344. return this.name + " has health " + this.health;
  345. };
  346.  
  347. this.getPlaceInfo = function () {
  348. return this.name + " is in " + this.place.title;
  349. };
  350.  
  351. this.getItemsInfo = function () {
  352. var itemsString = "Items:" + newLine;
  353.  
  354. this.items.forEach(function (item, i) {
  355. itemsString += " - " + item + newLine;
  356. });
  357.  
  358. return itemsString;
  359. };
  360.  
  361. this.getInfo = function (character) {
  362. var place = this.getPlaceInfo();
  363. var health = this.getHealthInfo();
  364. var longest = Math.max(place.length, health.length) + 4;
  365.  
  366. var info = spacer.box(this.getNameInfo(), longest, character);
  367. info += spacer.wrap(place, longest, character);
  368. info += spacer.newLine() + spacer.wrap(health, longest, character);
  369. info += newLine + spacer.line(longest, character);
  370.  
  371. info += newLine;
  372. info += " " + this.getItemsInfo();
  373. info += newLine;
  374. info += spacer.line(longest, character);
  375. info += newLine;
  376.  
  377. return info;
  378. };
  379.  
  380. this.showInfo = function (character) {
  381. console.log(this.getInfo(character));
  382. };
  383. };
  384.  
  385.  
  386. // Testing Player
  387.  
  388. var library = new Place(
  389. "The Old Library",
  390. "You are in a library. Dusty books line the walls."
  391. );
  392.  
  393. var player1 = new Player("Kandra", 50);
  394. player1.place = library;
  395. player1.addItem("a rusty key");
  396. player1.addItem("The Sword of Doom");
  397.  
  398. player1.showInfo("=");
  399.  
  400.  
  401.  
  402. /* Further Adventures
  403. *
  404. * 1) Test out the constructor by
  405. * creating a couple of Player objects.
  406. *
  407. * 2) Add items for each player.
  408. *
  409. * 3) Display each player's info.
  410. *
  411. * 4) Write a method to drop the last
  412. * item in a player's items array.
  413. *
  414. */</script></body>
  415. </html>
Add Comment
Please, Sign In to add comment