Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Player object with number and total of Pogs properties
- var Player = function() {
- this.name = prompt("What is your name?");
- this.totalPogs = 25;
- this.numPogs = 0;
- while (this.numPogs > this.totalPogs || this.numPogs <= 0) {
- this.numPogs = parseInt(prompt("How many pogs would you like to play with, " + this.name + "? Pick a number between 0 and " + this.totalPogs + "."), 10);
- }
- };
- //Creates player 1
- var player1 = new Player();
- //Creates player 2
- var player2 = new Player();
- //Pogs object
- var Pogs = function() {
- this.gameSet = player1.numPogs + player2.numPogs;
- };
- //Creates set of pogs for game
- var pogs = new Pogs();
- //Updates player pog totals to initialize game
- var updatePogCount = function(player) {
- player.totalPogs = player.totalPogs - player.numPogs;
- return player.totalPogs;
- };
- //Determines if side is up or down
- var side = function() {
- var side = Math.floor(Math.random() * 100);
- if (side < 50) {
- side = "Down";
- }
- else if (side >= 50) {
- side = "Up";
- }
- return side;
- };
- //determines how many pogs are keepers
- var keepers = function() {
- var round = pogs.gameSet;
- var keep = 0;
- for (i = 0; i < round; i++) {
- side();
- if (side() === "Up") {
- keep += 1;
- }
- }
- return keep;
- };
- //Plays one turn of pogs
- var turn = function(player) {
- var won = keepers();
- player.totalPogs = player.totalPogs + won;
- alert(player.name + ", you won " + won + " pogs and you now have a total of " + player.totalPogs + " pogs!");
- pogs.gameSet = pogs.gameSet - won;
- };
- //Updates number of pogs in play for subsequent rounds
- var updateNumPogs = function(player) {
- player.numPogs = parseInt(prompt("How many pogs would you like to play with, " + player.name + "? Pick a number between 0 and " + player.totalPogs + "."), 10);
- };
- //Plays more games of pogs between two players
- var playAgain = function() {
- var response = prompt("Would you like to play again, " + player1.name + " and " + player2.name + " ? Yes or No.");
- if (response === "Yes") {
- pogs = new Pogs();
- updateNumPogs(player1);
- updateNumPogs(player2);
- game();
- }
- else {
- return;
- }
- };
- //Plays one full game of pogs
- var game = function() {
- updatePogCount(player1);
- updatePogCount(player2);
- while (pogs.gameSet > 0) {
- turn(player1);
- turn(player2);
- }
- if (player1.totalPogs > player2.totalPogs) {
- alert(player1.name + ", congratulations, you beat " + player2.name + "!");
- }
- else if (player2.totalPogs > player1.totalPogs) {
- alert(player2.name + ", congratulations, you beat " + player1.name + "!");
- }
- else {
- alert("It was a tie!");
- }
- playAgain();
- };
- game();
Add Comment
Please, Sign In to add comment