Guest User

Untitled

a guest
Oct 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. //Player object with number and total of Pogs properties
  2. var Player = function() {
  3. this.name = prompt("What is your name?");
  4. this.totalPogs = 25;
  5. this.numPogs = 0;
  6. while (this.numPogs > this.totalPogs || this.numPogs <= 0) {
  7. this.numPogs = parseInt(prompt("How many pogs would you like to play with, " + this.name + "? Pick a number between 0 and " + this.totalPogs + "."), 10);
  8. }
  9. };
  10. //Creates player 1
  11. var player1 = new Player();
  12.  
  13. //Creates player 2
  14. var player2 = new Player();
  15.  
  16. //Pogs object
  17. var Pogs = function() {
  18. this.gameSet = player1.numPogs + player2.numPogs;
  19. };
  20.  
  21. //Creates set of pogs for game
  22. var pogs = new Pogs();
  23.  
  24. //Updates player pog totals to initialize game
  25. var updatePogCount = function(player) {
  26. player.totalPogs = player.totalPogs - player.numPogs;
  27. return player.totalPogs;
  28. };
  29.  
  30. //Determines if side is up or down
  31. var side = function() {
  32. var side = Math.floor(Math.random() * 100);
  33. if (side < 50) {
  34. side = "Down";
  35. }
  36. else if (side >= 50) {
  37. side = "Up";
  38. }
  39. return side;
  40. };
  41.  
  42. //determines how many pogs are keepers
  43. var keepers = function() {
  44. var round = pogs.gameSet;
  45. var keep = 0;
  46. for (i = 0; i < round; i++) {
  47. side();
  48. if (side() === "Up") {
  49. keep += 1;
  50. }
  51. }
  52. return keep;
  53. };
  54.  
  55. //Plays one turn of pogs
  56. var turn = function(player) {
  57. var won = keepers();
  58. player.totalPogs = player.totalPogs + won;
  59. alert(player.name + ", you won " + won + " pogs and you now have a total of " + player.totalPogs + " pogs!");
  60. pogs.gameSet = pogs.gameSet - won;
  61. };
  62.  
  63. //Updates number of pogs in play for subsequent rounds
  64. var updateNumPogs = function(player) {
  65. player.numPogs = parseInt(prompt("How many pogs would you like to play with, " + player.name + "? Pick a number between 0 and " + player.totalPogs + "."), 10);
  66. };
  67.  
  68.  
  69. //Plays more games of pogs between two players
  70. var playAgain = function() {
  71. var response = prompt("Would you like to play again, " + player1.name + " and " + player2.name + " ? Yes or No.");
  72. if (response === "Yes") {
  73. pogs = new Pogs();
  74. updateNumPogs(player1);
  75. updateNumPogs(player2);
  76. game();
  77. }
  78. else {
  79. return;
  80. }
  81. };
  82.  
  83. //Plays one full game of pogs
  84. var game = function() {
  85. updatePogCount(player1);
  86. updatePogCount(player2);
  87. while (pogs.gameSet > 0) {
  88. turn(player1);
  89. turn(player2);
  90. }
  91. if (player1.totalPogs > player2.totalPogs) {
  92. alert(player1.name + ", congratulations, you beat " + player2.name + "!");
  93. }
  94. else if (player2.totalPogs > player1.totalPogs) {
  95. alert(player2.name + ", congratulations, you beat " + player1.name + "!");
  96. }
  97. else {
  98. alert("It was a tie!");
  99. }
  100. playAgain();
  101. };
  102.  
  103. game();​
Add Comment
Please, Sign In to add comment