DEV Community

Cover image for Day 11: Rock, Paper, Scissors
Matt Ryan
Matt Ryan

Posted on

1 1

Day 11: Rock, Paper, Scissors

Simple Rock Paper Scissors console game in Java.

import java.util.Scanner;
import java.util.Random;
public class DayEleven {

    public static void main(String[] args) {

      int userChoice, computerChoice, rock, paper, scissors;
      Scanner input = new Scanner (System.in);
      Random rnd = new Random();  

      rock = 0;
      paper = 1;
      scissors = 2;

      System.out.println("ROCK - PAPER - SCISSORRS GAME");
      System.out.println("Enter your choice (0=rock, 1=paper, 2=scissors)");
      userChoice = input.nextInt();
              while (userChoice > 2) {
            System.out.println("give number between 0 and 2");
            userChoice = input.nextInt();
        }
       if (userChoice == rock)
       {
         System.out.println("User chose ROCK");
       }
      else
      {
        if(userChoice== paper)
        {
          System.out.println("User chose PAPER");
        }
        else
        {
          System.out.println("User chose SCISSORS");
        }
            }

      computerChoice = rnd.nextInt(3);

      if(computerChoice == rock)
      {
        System.out.println("Computer chose ROCK");
      }
      else
      {
        if(computerChoice == paper)
        {
          System.out.println("Computer chose PAPER");
        }
        else
        {
          System.out.println("Computer chose SCISSORS");
        }
      }

      while (userChoice == computerChoice) {
            System.out.println("draw try again");

            userChoice = input.nextInt();
                          while (userChoice > 2) {
                            System.out.println("give number between 0 and 2");
                            userChoice = input.nextInt();
                            }
            computerChoice = rnd.nextInt(3);
                        if (userChoice == rock)
                            {
                                System.out.println("User chose ROCK");
                            }
                        else
                        {
                            if(userChoice== paper)
                            {
                                System.out.println("User chose PAPER");
                            }
                            else
                            {
                                System.out.println("User chose SCISSORS");
                            }
            }      
                        if(computerChoice == rock)
                            {
                                System.out.println("Computer chose ROCK");
                            }
                        else
                            {
                                if(computerChoice == paper)
                                {
                                    System.out.println("Computer chose PAPER");
                                }
                                else
                                {
                                    System.out.println("Computer chose SCISSORS");
                                }
                            }
                        }

      if (computerChoice==rock)
        {
            if (userChoice==paper)
            {  
                System.out.println("User wins!");
            }
            else 
            {
                System.out.println("Computer Wins");
            }
        }
      else if (computerChoice==paper)
      {
        if (userChoice==rock)
            {
                System.out.println("Computer wins");
            }
        else
            {
                System.out.println("User Wins!");
            }
      }
      else if (userChoice==rock)
      {
        System.out.println("User Wins");
      }
      else
      {
          System.out.println("Computer Wins");
      }
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay