Advertisement
BOT_Yokel

ICS3U1 Unit 2 Activity 4: Question 1

Jul 21st, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date: July 21, 2017
  2. // Purpose: To play the game Rock-Paper-Scissors
  3.  
  4. // This line makes the button, btnPlay wait for a mouse click
  5. // When the button is clicked, the determineEligibility function is called
  6. btnPlay.addEventListener(MouseEvent.CLICK, playGame);
  7.  
  8. // This line makes the button, btnNewGame wait for a mouse click
  9. // When the button is clicked, the newGame function is called
  10. btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
  11.  
  12. // Declare the global variables
  13. var ties:int = 0;
  14. var wins:int = 0;
  15. var losses:int = 0;
  16.  
  17. // This is the playGame function
  18. // e:MouseEvent is the click event experienced by the button
  19. // void indicates that the function does not return a value
  20. function playGame(e:MouseEvent):void
  21. {
  22.     // declare the variables
  23.     var userThrow:int;      // the value of the user's throw. 1-Rock 2-Paper 3-Scissors
  24.     var computerThrow:int;  // the value of the computer's throw. 1-Rock 2-Paper 3-Scissors
  25.     var outcome:String;     // the outcome of the game
  26.    
  27.     userThrow = int(radRock.group.selectedData);    // get the user's throw
  28.     computerThrow = randomWholeNumber(3,1); // get the computer's throw
  29.     // Detemine the outcome
  30.     outcome = determineOutcome(userThrow, computerThrow);
  31.     lblOutcome.text = outcome; 
  32.     // Update the score
  33.     // write code here to update the wins, losses and ties
  34.     if (computerThrow == 1 && userThrow == 1)
  35.     {
  36.         ties += 1
  37.     }
  38.     else if (computerThrow == 1 && userThrow  == 2)
  39.     {
  40.         wins += 1
  41.     }
  42.     else if (computerThrow == 1 && userThrow == 3)
  43.     {
  44.         losses += 1
  45.     }
  46.     else if (computerThrow == 2 && userThrow == 1)
  47.     {
  48.         losses += 1
  49.     }
  50.     else if (computerThrow == 2 && userThrow == 2)
  51.     {
  52.         ties += 1
  53.     }
  54.     else if (computerThrow == 2 && userThrow == 3)
  55.     {
  56.         wins += 1
  57.     }
  58.     else if (computerThrow == 3 && userThrow == 1)
  59.     {
  60.         wins += 1
  61.     }
  62.     else if (computerThrow == 3 && userThrow == 2)
  63.     {
  64.         losses += 1
  65.     }
  66.     else if (computerThrow == 3 && userThrow == 3)
  67.     {
  68.         ties += 1
  69.     }
  70.     lblScore.text = "Wins: " + wins.toString() + "  Losses: " + losses.toString() + "  Ties: " + ties.toString();
  71. }
  72.  
  73. // This is the newGame function
  74. // e:MouseEvent is the click event experienced by the button
  75. // void indicates that the function does not return a value
  76. function newGame(e:MouseEvent):void
  77. {
  78.     // write code here to reset the values to start a series of games
  79.     ties = 0
  80.     wins = 0
  81.     losses = 0
  82.     lblOutcome.text = ""
  83.     lblScore.text = "Wins: " + wins.toString() + "  Losses: " + losses.toString() + "  Ties: " + ties.toString();
  84. }
  85.  
  86. // This is function randomWholeNumber
  87. // highNumber – the maximum value desired
  88. // lowNumber – the minimum value desired
  89. // returns – a random whole number from highNumber to lowNumber inclusive
  90. function randomWholeNumber(highNumber:int,lowNumber:int):int
  91. {
  92.     return Math.floor((3 - 1 + 1) * Math.random() + 1);
  93. }
  94.  
  95. // This is function determineOutcome.
  96. // It determines the outcome of the game.
  97. // u - the user;s throw value
  98. // c - the computer;s throw value
  99. // returns - a string with a message of the outcome
  100. function determineOutcome(u:int,c:int):String
  101. {
  102.     // write code here to return an appropriate message for the outcome of the game
  103.    
  104.     if (c == 1 && u == 1)
  105.     {
  106.         return "Tie, the computer and the user both threw rock!"
  107.     }
  108.     else if (c == 1 && u  == 2)
  109.     {
  110.         return "Win, the computer threw rock, and the user threw paper!"
  111.     }
  112.     else if (c == 1 && u == 3)
  113.     {
  114.         return "Loss, the computer threw rock, and the user threw scissors!"
  115.     }
  116.     else if (c == 2 && u == 1)
  117.     {
  118.         return "Loss, the computer threw paper, and the user threw rock!"
  119.     }
  120.     else if (c == 2 && u == 2)
  121.     {
  122.         return "Tie, the computer and the user both threw rock!"
  123.     }
  124.     else if (c == 2 && u == 3)
  125.     {
  126.         return "Win, the computer threw paper and the user threw scissors!"
  127.     }
  128.     else if (c == 3 && u == 1)
  129.     {
  130.         return "Win, the computer threw scissors and the user threw rock!"
  131.     }
  132.     else if (c == 3 && u == 2)
  133.     {
  134.         return "Loss, the computer threw scissors and the user threw paper!"
  135.     }
  136.     else if (c == 3 && u == 3)
  137.     {
  138.         return "Tie, the computer and the user both threw scissors!"
  139.     }
  140.     else
  141.     return "Please enter a value."
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement