Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 1 August 2012

JAVA Code for the Rock Paper Scissors Program

Posted on 08:38 by Unknown
Hello, people. How are you doing? I want to show off the code for the game of Rock Paper Scissors where it's just one player against the computer; this was a programming challenge from Chapter 5 of Tony Gaddis' Starting Out with Java:Fourth Edition. The program was done during my first programming course at my college called JAVA Programming Level 1.

This is also for any YouTube users who have watched how the JAVA game works. If you people ask any questions, post a comment on this blog, on the original YouTube video (http://www.youtube.com/watch?v=q_BjPjVsMbg)

import javax.swing.JOptionPane;
import java.util.Random;

public class Chapter5RockPaperScissors
{
    public static void main(String[] args)
    {
        String computerSide,
               userSide;

        JOptionPane.showMessageDialog(null, "How about we go \"Rock, Paper, Scissors?\"");
       
        computerSide = ComputerChoice();
        userSide = UserChoice();
       
        if (userSide != null)
        {
            JOptionPane.showMessageDialog(null, "The computer's choice is " + computerSide + ".");
           
            Winner(computerSide, userSide);
        }
       
        else
            JOptionPane.showMessageDialog(null, "Error: Improper User Entry. Please enter either" +
                                        " 'rock', 'paper', or 'scissors'.");
    }
   
    public static String ComputerChoice()
    {
        byte computerChoice;
       
        String computerChoiceString = "";
       
        Random choiceGenerator = new Random();
       
        computerChoice = (byte)(choiceGenerator.nextInt(3) + 1);
       
        switch (computerChoice)
        {
       
            case 1:
            {
                computerChoiceString = "rock";
                break;
            }
       
            case 2:
            {
                computerChoiceString = "paper";
                break;
            }
       
            case 3:
            {
                computerChoiceString = "scissors";
                break;
            }
       
        }
       
        return computerChoiceString;
    }
   
    public static String UserChoice()
    {
        String userChoice,
               userChoiceLowerCase;
       
        userChoice =
                JOptionPane.showInputDialog("What object are you going for?");
       
        if (userChoice.equalsIgnoreCase("rock") || userChoice.equalsIgnoreCase("paper")
                || userChoice.equalsIgnoreCase("scissors"))
        {
            userChoiceLowerCase = userChoice.toLowerCase();
        }
       
        else
            userChoiceLowerCase = null;
       
        return userChoiceLowerCase;
    }
   
    public static void Winner(String computerSide, String userSide)
    {
       
        if (computerSide.equals(userSide))
            JOptionPane.showMessageDialog(null, "The game has to be played again, because we have a tie.");
       
        else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("paper"))
            JOptionPane.showMessageDialog(null, "You win. Paper covers rock.");
       
        else if (computerSide.equalsIgnoreCase("rock") && userSide.equalsIgnoreCase("scissors"))
            JOptionPane.showMessageDialog(null, "You lose. Rock crushes scissors.");
       
        else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("rock"))
            JOptionPane.showMessageDialog(null, "You lose. Paper covers rock.");
       
        else if (computerSide.equalsIgnoreCase("paper") && userSide.equalsIgnoreCase("scissors"))
            JOptionPane.showMessageDialog(null, "You win. Scissors cuts paper.");
       
        else if (computerSide.equalsIgnoreCase("scissors") && userSide.equalsIgnoreCase("rock"))
            JOptionPane.showMessageDialog(null, "You win. Rock crushes scissors.");
       
        else
            JOptionPane.showMessageDialog(null, "You lose. Scissors cuts paper.");
    }
}


Have a good day, people!

Be sure to follow me on Facebook, Twitter, and Google+!

Also, check out my YouTube channels!
Bammer2001 : http://www.youtube.com/user/Bammer2001
Alternative Channel (for monetization and publicity purposes) : http://www.youtube.com/channel/UCVGkYqhVtOsumY7R14hP3hw

Facebook: https://www.facebook.com/#!/pages/Gregory-Desrosiers/171954446270382
Twitter: https://twitter.com/GregoryDes
Google+: I can't provide a link, but you can find me in the search engine under my name.
Blogger: http://gregorypdesrosiersmontreal.blogspot.ca/


Please make a donation to me for an advertising campaign for my e-book, The Asperger Computer! All donations will be kept track and shall be used for a Facebook advertising campaign. Optimistically, I should have a donated budget of $100 before making way for a much larger Facebook ad campaign for my e-book. Donations exceeding the budget will either add up to the budget for the mass ad campaign, or to boost a post, primarily for the status on Facebook sharing all the links for people to buy my e-book!

If you donate more than $4.00 CAD, you will get my e-book for free. You aren't necessarily paying for my e-book by doing so because all of that $4.00 and over donated will be spent for advertising, to fulfill the purpose of "fundraising" or "not-for-profit" campaign.

Come support my efforts!



Thanks for reading!
 Click here to follow me on Facebook: https://www.facebook.com/TheAspergerComputerGregDes?ref=hl
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • Java Programming : For Loop Example
    Here's an example of using a for loop in JAVA. This program uses a loop inside another to create a list of integers where the number on ...
  • Java Program : Fat Gram Calculator
    Here's another program I want to share with you guys, which is simply a program that multiplies the number of grams of fat in food by ni...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • JAVA Program: Lottery Class
    Here's a nother JAVA Program I want to share with you guys , which is a demonst ratio n of the Lottery class I w as as ked to design fro...
  • List of Played N64 Games
    Hello, guys. Let me introduce to you a list of N64 games I have played when I was young; the N64 was one of the two consoles I had, besides ...
  • Inequality in Canadian Women through Politics and Employment [Gender and Knowledge Essay]
    This is a research essay I did in my Gender and Knowledge class with Rachel Morris as my instructor at Champlain. It was finished on April ...

Blog Archive

  • ►  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ►  May (12)
    • ►  April (2)
    • ►  January (1)
  • ▼  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ▼  August (2)
      • Video Game Quotes
      • JAVA Code for the Rock Paper Scissors Program
Powered by Blogger.

About Me

Unknown
View my complete profile