Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. var RockPaperScissors = (function () {
  2. var t = 'Tie',
  3. c = 'Computer wins',
  4. p = 'Player wins',
  5. winningMap = [
  6. [t, c, p],
  7. [p, t, c],
  8. [c, p, t]
  9. ],
  10. choices = ["Rock", "Paper", "Scissors"];
  11.  
  12. var getComputerChoice = function () {
  13. return Math.floor(Math.random() * 3);
  14. };
  15.  
  16. var getWinner = function (playerChoice, computerChoice) {
  17. return winningMap[playerChoice][computerChoice];
  18. };
  19.  
  20. function RockPaperScissors(playerChoice) {
  21. this.playerChoice = playerChoice;
  22. this.computerChoice = getComputerChoice();
  23. this.winner = getWinner(this.playerChoice, this.computerChoice);
  24. }
  25.  
  26. RockPaperScissors.prototype.toString = function () {
  27. return this.winner + " [Computer: " + choices[this.computerChoice] + ", Player: " + choices[this.playerChoice] + "]";
  28. };
  29.  
  30. return RockPaperScissors;
  31.  
  32. })();
  33.  
  34. $('#choice').on('change', function () {
  35. var $input = $(this);
  36.  
  37. if ($input.val() === '-1') return;
  38.  
  39. var rockPaperScissors = new RockPaperScissors($input.val());
  40.  
  41. $('#message').html(rockPaperScissors.toString());
  42. });
  43.  
  44. var winningMap = {Rock: "Scissors", Paper: "Rock", Scissors: "Paper"};
  45.  
  46. var getWinner = function (playerChoice, computerChoice) {
  47. if(playerChoice === computerChoice){
  48. return "Tie";
  49. }
  50. if(computerChoice === winningMap[playerChoice]){
  51. return "Player Wins";
  52. }
  53. return "Computer Wins";
  54. };
  55.  
  56. var stringMapping = ["Rock", "Scissors", "Paper"];
  57.  
  58. var getComputerChoice = function () {
  59. return stringMapping[ Math.floor(Math.random() * 3) ];
  60. };
  61.  
  62. function RockPaperScissors (){
  63. var getComputerChoice = function(){ ... }; //= private variable
  64. this.getWinner = function(){ ... }; //= public variable
  65. };
  66.  
  67. var actualObject = new RockPaperScissors();
  68. actualObject.method();
  69. actualObject['method']();
  70.  
  71. if ($input.val() === '-1') { return; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement