Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var RockPaperScissors = (function () {
- var t = 'Tie',
- c = 'Computer wins',
- p = 'Player wins',
- winningMap = [
- [t, c, p],
- [p, t, c],
- [c, p, t]
- ],
- choices = ["Rock", "Paper", "Scissors"];
- var getComputerChoice = function () {
- return Math.floor(Math.random() * 3);
- };
- var getWinner = function (playerChoice, computerChoice) {
- return winningMap[playerChoice][computerChoice];
- };
- function RockPaperScissors(playerChoice) {
- this.playerChoice = playerChoice;
- this.computerChoice = getComputerChoice();
- this.winner = getWinner(this.playerChoice, this.computerChoice);
- }
- RockPaperScissors.prototype.toString = function () {
- return this.winner + " [Computer: " + choices[this.computerChoice] + ", Player: " + choices[this.playerChoice] + "]";
- };
- return RockPaperScissors;
- })();
- $('#choice').on('change', function () {
- var $input = $(this);
- if ($input.val() === '-1') return;
- var rockPaperScissors = new RockPaperScissors($input.val());
- $('#message').html(rockPaperScissors.toString());
- });
- var winningMap = {Rock: "Scissors", Paper: "Rock", Scissors: "Paper"};
- var getWinner = function (playerChoice, computerChoice) {
- if(playerChoice === computerChoice){
- return "Tie";
- }
- if(computerChoice === winningMap[playerChoice]){
- return "Player Wins";
- }
- return "Computer Wins";
- };
- var stringMapping = ["Rock", "Scissors", "Paper"];
- var getComputerChoice = function () {
- return stringMapping[ Math.floor(Math.random() * 3) ];
- };
- function RockPaperScissors (){
- var getComputerChoice = function(){ ... }; //= private variable
- this.getWinner = function(){ ... }; //= public variable
- };
- var actualObject = new RockPaperScissors();
- actualObject.method();
- actualObject['method']();
- if ($input.val() === '-1') { return; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement