Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date: July 21, 2017
- // Purpose: To play the game Rock-Paper-Scissors
- // This line makes the button, btnPlay wait for a mouse click
- // When the button is clicked, the determineEligibility function is called
- btnPlay.addEventListener(MouseEvent.CLICK, playGame);
- // This line makes the button, btnNewGame wait for a mouse click
- // When the button is clicked, the newGame function is called
- btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
- // Declare the global variables
- var ties:int = 0;
- var wins:int = 0;
- var losses:int = 0;
- // This is the playGame function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function playGame(e:MouseEvent):void
- {
- // declare the variables
- var userThrow:int; // the value of the user's throw. 1-Rock 2-Paper 3-Scissors
- var computerThrow:int; // the value of the computer's throw. 1-Rock 2-Paper 3-Scissors
- var outcome:String; // the outcome of the game
- userThrow = int(radRock.group.selectedData); // get the user's throw
- computerThrow = randomWholeNumber(3,1); // get the computer's throw
- // Detemine the outcome
- outcome = determineOutcome(userThrow, computerThrow);
- lblOutcome.text = outcome;
- // Update the score
- // write code here to update the wins, losses and ties
- if (computerThrow == 1 && userThrow == 1)
- {
- ties += 1
- }
- else if (computerThrow == 1 && userThrow == 2)
- {
- wins += 1
- }
- else if (computerThrow == 1 && userThrow == 3)
- {
- losses += 1
- }
- else if (computerThrow == 2 && userThrow == 1)
- {
- losses += 1
- }
- else if (computerThrow == 2 && userThrow == 2)
- {
- ties += 1
- }
- else if (computerThrow == 2 && userThrow == 3)
- {
- wins += 1
- }
- else if (computerThrow == 3 && userThrow == 1)
- {
- wins += 1
- }
- else if (computerThrow == 3 && userThrow == 2)
- {
- losses += 1
- }
- else if (computerThrow == 3 && userThrow == 3)
- {
- ties += 1
- }
- lblScore.text = "Wins: " + wins.toString() + " Losses: " + losses.toString() + " Ties: " + ties.toString();
- }
- // This is the newGame function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function newGame(e:MouseEvent):void
- {
- // write code here to reset the values to start a series of games
- ties = 0
- wins = 0
- losses = 0
- lblOutcome.text = ""
- lblScore.text = "Wins: " + wins.toString() + " Losses: " + losses.toString() + " Ties: " + ties.toString();
- }
- // This is function randomWholeNumber
- // highNumber – the maximum value desired
- // lowNumber – the minimum value desired
- // returns – a random whole number from highNumber to lowNumber inclusive
- function randomWholeNumber(highNumber:int,lowNumber:int):int
- {
- return Math.floor((3 - 1 + 1) * Math.random() + 1);
- }
- // This is function determineOutcome.
- // It determines the outcome of the game.
- // u - the user;s throw value
- // c - the computer;s throw value
- // returns - a string with a message of the outcome
- function determineOutcome(u:int,c:int):String
- {
- // write code here to return an appropriate message for the outcome of the game
- if (c == 1 && u == 1)
- {
- return "Tie, the computer and the user both threw rock!"
- }
- else if (c == 1 && u == 2)
- {
- return "Win, the computer threw rock, and the user threw paper!"
- }
- else if (c == 1 && u == 3)
- {
- return "Loss, the computer threw rock, and the user threw scissors!"
- }
- else if (c == 2 && u == 1)
- {
- return "Loss, the computer threw paper, and the user threw rock!"
- }
- else if (c == 2 && u == 2)
- {
- return "Tie, the computer and the user both threw rock!"
- }
- else if (c == 2 && u == 3)
- {
- return "Win, the computer threw paper and the user threw scissors!"
- }
- else if (c == 3 && u == 1)
- {
- return "Win, the computer threw scissors and the user threw rock!"
- }
- else if (c == 3 && u == 2)
- {
- return "Loss, the computer threw scissors and the user threw paper!"
- }
- else if (c == 3 && u == 3)
- {
- return "Tie, the computer and the user both threw scissors!"
- }
- else
- return "Please enter a value."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement