Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name:
- // Date:
- // 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
- 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 {
- lblScore.text = "Wins: " + "" + " Losses: " + "" + " Ties: " + ""
- }
- // 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((highNumber - lowNumber + 1) * Math.random() + lowNumber);
- }
- // 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 {
- if (u == 1 && c == 1) {
- ties++;
- return "Both you and the computer threw a Rock. You tie.";
- }
- if (u == 1 && c == 2) {
- losses++;
- return "The computer threw a Paper that covers your Rock. You lose.";
- }
- if (u == 1 && c == 3) {
- wins++;
- return "The computer threw a Scissors and your Rock smashes it. You win";
- }
- if (u == 2 && c == 1) {
- wins++;
- return "The computer threw a Rock and your Paper covers it";
- }
- if (u == 2 && c == 2) {
- ties++;
- return "Both you and the computer threw a Paper. You tie."
- }
- if (u == 2 && c == 3) {
- losses++;
- return "The computer threw a Scissors that cuts your Paper. You lose.";
- }
- if (u == 3 && c == 1) {
- losses++;
- return "The computer threw a Rock that smashes your Scissors. You lose.";
- }
- if (u == 3 && c == 2) {
- wins++;
- return "The computer threw a Paper and your Scissors cuts it. You win.";
- }
- if (u == 3 && c == 3) {
- ties++;
- return "Both you and the computer threw a Scissors. You tie.";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement