Guest User

Untitled

a guest
Jul 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. // Instructions
  2. var inquirer = require("inquirer");
  3. // Over the course of this assignment you are going to put together a function which uses constructors and user input to create and manage a team of players.
  4.  
  5. // Start out by creating a constructor function called “Player” with the following properties and methods...
  6.  
  7. function Player(name, position, offense, defense) {
  8. // name: Property which contains the player’s name
  9. this.name = name;
  10. // position: Property which holds the player’s position
  11. this.position = position;
  12. // offense: Property which is a value between 1 and 10 to show how good this player is on offense
  13. this.offense = offense;
  14. // defense: Property which is a value between 1 and 10 to show how good this player is on defense
  15. this.defense = defense;
  16. // goodGame: Method which increases either the player’s offense or defense property based upon a coinflip.
  17. this.goodGame = function () {
  18. var coinflip = Math.floor(Math.random() * Math.floor(2))
  19. if (coinflip === 1) {
  20. this.offense++
  21. }
  22. else {
  23. this.defense++
  24. }
  25.  
  26. }
  27. // badGame: Method which decreases either the player’s offense or defense property based upon a coinflip.
  28. this.badGame = function () {
  29. var coinflip = Math.floor(Math.random() * Math.floor(2))
  30. if (coinflip === 1) {
  31. this.offense--
  32. }
  33. else {
  34. this.defense--
  35. }
  36.  
  37. }
  38. // printStats: Method which prints all of the player’s properties to the screen
  39. this.printStats = function () {
  40. console.log(JSON.stringify(this))
  41. }
  42. }
  43.  
  44.  
  45.  
  46. var starters = [];
  47. var subs = [];
  48. var team = [];
  49. var positions = ["forward", "back", "striker", "goalkeeper"]
  50.  
  51.  
  52.  
  53.  
  54. // Now create a program which allows the user to create 3 unique players; 2 starters and a sub. It should take as user input the name, position, offense, and defense of each player.
  55.  
  56. var createPlayer = function () {
  57. // if the length of the team array is 5 or higher, no more questions will be asked
  58. if (starters.length + subs.length < 5) {
  59. console.log("\nNEW PLAYER!\n");
  60. //inArray(where, stateArr)
  61. inquirer.prompt([
  62. {
  63. name: "name",
  64. message: "Player's Name: "
  65. }, {
  66. type: "list",
  67. name: "position",
  68. message: "Player's position: ",
  69. choices: positions
  70. }, {
  71. name: "offense",
  72. message: "Player's Offensive Ability: ",
  73. //valide is set to javascript object which is a function, it looks at the value that is passed
  74. validate: function (value) {
  75.  
  76. if (isNaN(value) === false && parseInt(value) > 0 && parseInt(value) <= 10) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. }, {
  82. name: "defense",
  83. message: "Player's Defensive Ability: ",
  84. validate: function (value) {
  85. if (isNaN(value) === false && parseInt(value) > 0 && parseInt(value) <= 10) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. ]).then(function (answers) {
  92. // runs the constructor and places the new player object into the variable player.
  93. // turns the offense and defense variables into integers as well with parseInt
  94. var player = new Player(answers.name, answers.position, parseInt(answers.offense), parseInt(answers.defense));
  95. // adds a player to the starters array if there are less than five player objects in it.
  96. // otherwise adds the newest player object to the subs array
  97. if (starters.length < 3) {
  98. starters.push(player);
  99. team.push(player);
  100. console.log(player.name + " added to the starters");
  101. }
  102. else {
  103. subs.push(player);
  104. team.push(player);
  105. console.log(player.name + " added to the subs");
  106. }
  107. // runs the createPlayer function once more
  108. createPlayer();
  109. });
  110. }
  111. else {
  112. // loops through the team array and calls printStats() for each object it contains
  113.  
  114. for (var i = 0; i < team.length; i++) {
  115. team[i].printStats();
  116. }
  117. playgame()
  118. }
  119. };
  120.  
  121. // calls the function createPlayer() to start the code
  122.  
  123. //HEY DUMMY ADD BACK IN
  124.  
  125. createPlayer();
  126.  
  127.  
  128. // Once all of the players have been created, print their stats.
  129.  
  130. // HINT: Remember to use recursion when looping with inquirer! Otherwise your program might not return the prompts as you expect.
  131.  
  132.  
  133. // Instructions
  134.  
  135. // Once your code is functioning properly, move on to create a function called “playGame” which will be run after all of the players
  136. //have been created and will do the following:
  137.  
  138. // Run 5 times. Each time the function runs:
  139. // Two random numbers between 1 and 20 are rolled and compared against the starters’ offensive and defensive stats
  140. // If the first number is lower than the sum of the team’s offensive stat, add one point to the team’s score.
  141. // If the second number is higher than the sum of the team’s defensive stat, remove one point from the team’s score.
  142. // After the score has been changed, prompt the user to allow them to substitute a player from within the starters array with the player from within the subs array.
  143. // After the game has finished (been run 5 times):
  144. // If the score is positive, run goodGame for all of the players currently within the starters array.
  145. // If the score is negative, run badGame for all of the players currently within the starters array.
  146. // If the score is equal to zero, do nothing with the starters.
  147. // Give the user a message about if they won, and the status of their starters.
  148. // After printing the results, ask the user if they would like to play again.
  149. // Run playGame from the start once more if they do.
  150. // End the program if they don’t.
  151. // HINT: It has been a while since we have worked with random numbers explicitly. If you are having troubles,
  152. //look up Math.random on Google and you should find some resources to help.
  153.  
  154. var teamScore = 0;
  155. var count = 0;
  156.  
  157.  
  158. function playgame() {
  159. if (count < 5) {
  160.  
  161. //sets randome numberes to offense and defense
  162. var offenseNum = Math.floor(Math.random() * Math.floor(20))
  163. var defenseNum = Math.floor(Math.random() * Math.floor(20))
  164. var teamOffense = 0;
  165. var teamDefense = 0;
  166. // var currentStarters = [];
  167. //var currentStarters = ["no"];
  168. //var currentSubs = ["no"];
  169. // var currentSubs = [];
  170.  
  171. //sets offense and defesnse sum for team current team starters
  172. for (i = 0; i < starters.length; i++) {
  173. teamOffense += starters[i].offense
  174. teamDefense += starters[i].defense
  175.  
  176. }
  177.  
  178.  
  179. if (offenseNum < teamOffense) {
  180. teamScore++
  181.  
  182. }
  183.  
  184. if (defenseNum > teamDefense) {
  185. teamScore--
  186.  
  187. }
  188.  
  189. console.log("Current score: " +teamScore)
  190.  
  191. //PROMPT TO SEE IF A SUB IS NEEDED
  192. inquirer.prompt([
  193. {
  194. type:"confirm",
  195. name: "confirmSub",
  196. message:"Do you need to sub someone out?"
  197. }
  198.  
  199.  
  200. ]).then(function (answers) {
  201. //IF SUB IS NEEDED THIS WORKS, OTHERWISE GO TO AROUND LINE
  202. if(answers.confirmSub === true){
  203. //PROMPT TO SEE WHO TO BE SUBBED OUT AND IN
  204. inquirer.prompt([
  205.  
  206. {
  207. type: "list",
  208. message: "Who to remove?",
  209. choices: starters,
  210. name: "startersList",
  211. },
  212. {
  213. type: "list",
  214. message: "Who to be subbed in??",
  215. choices: subs,
  216. name: "subList"
  217. },
  218.  
  219. ]).then(function (subSpecifics) {
  220. //since the value being passed by the answer is only the name, per inquirier documentation, we need to find the actual OBJECT
  221. //this grabs the index number of a matching name value of one of the objects
  222. for(i = 0; i < subs.length; i ++){
  223. if(subSpecifics.subList === subs[i].name){
  224. indexOfSub = i
  225. }
  226. }
  227.  
  228. for(i = 0; i < starters.length; i ++){
  229. if(subSpecifics.startersList === starters[i].name){
  230. indexOfStarter = i
  231. }
  232. }
  233.  
  234. //creates a temporary array to manipulate
  235. var tempSub = subs
  236. var tempStarters = starters
  237. //grabs the sub and starter object to be spliced and pushed
  238. var trueSub = subs[indexOfSub]
  239. var trueStarter = starters[indexOfStarter]
  240. //splices the truesub and pushes the true starter to the temporary array
  241. tempSub.splice(indexOfSub, 1)
  242. tempSub.push(trueStarter)
  243. //splices the truestarter and pushes the truesub to the teomprary array
  244. tempStarters.splice(indexOfStarter, 1)
  245. tempStarters.push(trueSub)
  246. //sets the starter and subs to the manipulated array
  247. subs = tempSub
  248. starters = tempStarters
  249.  
  250. endGameCheck()
  251. })
  252.  
  253. }
  254. //IF NO SUB IS NEEDED PROCEED TO THE THIS STEP
  255. else{
  256. endGameCheck()
  257. }
  258.  
  259. })
  260.  
  261. }
  262. //IF COUNT = 5 PROCEED DTO THIS STEP
  263. else{
  264. inquirer.prompt([
  265. {
  266. type:"confirm",
  267. name: "playAgain",
  268. message:"Play again?"
  269. }
  270.  
  271.  
  272. ]).then(function (answers) {
  273. if(answers.playAgain === true){
  274. count = 0
  275. teamScore = 0
  276. playgame()
  277. }
  278. })
  279.  
  280. }
  281. //THIS IS THE FUNCTION TO CHECK IF THE GAME WAS WON, PROBABLY COULD LIVE SOMEWHERE ELSE, BUT OH WELL.
  282. function endGameCheck(){
  283. if(teamScore > 0){
  284. console.log("You won!")
  285. for(i=0; i < starters.length; i ++){
  286. starters[i].goodGame()
  287. starters[i].printStats()
  288.  
  289. }
  290. }
  291. else if(teamScore < 0){
  292. console.log("You lost! ")
  293. for(i=0; i < starters.length; i ++){
  294. starters[i].badGame()
  295. starters[i].printStats()
  296. }
  297. }
  298. else{
  299. starters[i].printStats()
  300. }
  301. count++
  302. playgame()
  303. }
  304.  
  305. }
Add Comment
Please, Sign In to add comment