Advertisement
andychen2325

Untitled

Jul 21st, 2018
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Name:
  2. // Date:
  3. // Purpose: To play the game Rock-Paper-Scissors
  4.  
  5. // This line makes the button, btnPlay wait for a mouse click
  6. // When the button is clicked, the determineEligibility function is called
  7. btnPlay.addEventListener(MouseEvent.CLICK, playGame);
  8.  
  9. // This line makes the button, btnNewGame wait for a mouse click
  10. // When the button is clicked, the newGame function is called
  11. btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
  12.  
  13. // Declare the global variables
  14. var ties: int = 0;
  15. var wins: int = 0;
  16. var losses: int = 0;
  17.  
  18. // This is the playGame function
  19. // e:MouseEvent is the click event experienced by the button
  20. // void indicates that the function does not return a value
  21. function playGame(e: MouseEvent): void {
  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.     lblScore.text = "Wins: " + wins.toString() + "  Losses: " + losses.toString() + "  Ties: " + ties.toString();
  35. }
  36.  
  37. // This is the newGame function
  38. // e:MouseEvent is the click event experienced by the button
  39. // void indicates that the function does not return a value
  40. function newGame(e: MouseEvent): void {
  41.     lblScore.text = "Wins: " + "" + "  Losses: " + "" + "  Ties: " + ""
  42. }
  43.  
  44. // This is function randomWholeNumber
  45. // highNumber – the maximum value desired
  46. // lowNumber – the minimum value desired
  47. // returns – a random whole number from highNumber to lowNumber inclusive
  48. function randomWholeNumber(highNumber: int, lowNumber: int): int {
  49.     return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
  50. }
  51.  
  52. // This is function determineOutcome.
  53. // It determines the outcome of the game.
  54. // u - the user's throw value
  55. // c - the computer's throw value
  56. // returns - a string with a message of the outcome
  57. function determineOutcome(u: int, c: int): String {
  58.     if (u == 1 && c == 1) {
  59.         ties++;
  60.         return "Both you and the computer threw a Rock. You tie.";
  61.     }
  62.     if (u == 1 && c == 2) {
  63.         losses++;
  64.         return "The computer threw a Paper that covers your Rock. You lose.";
  65.     }
  66.     if (u == 1 && c == 3) {
  67.         wins++;
  68.         return "The computer threw a Scissors and your Rock smashes it. You win";
  69.     }
  70.     if (u == 2 && c == 1) {
  71.         wins++;
  72.         return "The computer threw a Rock and your Paper covers it";
  73.     }
  74.     if (u == 2 && c == 2) {
  75.         ties++;
  76.         return "Both you and the computer threw a Paper. You tie."
  77.     }
  78.     if (u == 2 && c == 3) {
  79.         losses++;
  80.         return "The computer threw a Scissors that cuts your Paper. You lose.";
  81.     }
  82.     if (u == 3 && c == 1) {
  83.         losses++;
  84.         return "The computer threw a Rock that smashes your Scissors. You lose.";
  85.     }
  86.     if (u == 3 && c == 2) {
  87.         wins++;
  88.         return "The computer threw a Paper and your Scissors cuts it. You win.";
  89.     }
  90.     if (u == 3 && c == 3) {
  91.         ties++;
  92.         return "Both you and the computer threw a Scissors. You tie.";
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement