Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java rock paper scissors

// Fun rock paper scissors game in Java

import java.util.*;

public class Game 
{
    
    public static final String ROCK = "ROCK";
    public static final String PAPER = "PAPER";
    public static final String SCISSORS = "SCISSORS";

    public static void main(String args[]) 
    {
      System.out.println("Enter any one of the following inputs:  ");
      System.out.println("ROCK");
      System.out.println("PAPER");
      System.out.println("SCISSORS");
      System.out.println();
          
      String playerMove = getPlayerMove();
      String computerMove = getComputerMove(); 
 
      //Rules of the Game Applied Below:
      /*if both playerMove and computerMove
        produces the same formation, then 
        Game is a tie*/
      if (playerMove.equals(computerMove))
            System.out.println("Game is Tie !!");
      // if playerMove is ROCK         
      else if (playerMove.equals(Game.ROCK))
        System.out.println(computerMove.equals(Game.PAPER) ? "Computer Wins": "Player wins");   
      // if playerMove is PAPER
      else if (playerMove.equals(Game.PAPER))
        System.out.println(computerMove.equals(Game.SCISSORS) ? "Computer Wins": "Player wins");   
      // if playerMove is SCISSORS    
      else
        System.out.println(computerMove.equals(Game.ROCK) ? "Computer Wins": "Player wins");   
    }
    
    /* Get Computer's move using Random 
       class nextInt() method */   
    public static String getComputerMove()
    {
        String computermove;
        Random random = new Random();
        int input = random.nextInt(3)+1;
        if (input == 1)
            computermove = Game.ROCK;
        else if(input == 2)
            computermove = Game.PAPER;
        else
            computermove = Game.SCISSORS;
            
        System.out.println("Computer move is: " + computermove);
        System.out.println();
        return computermove;    
    }
    
    /* Get Player's move using Scanner
       class */
    public static String getPlayerMove()
    {
        Scanner in = new Scanner(System.in);
        String input = in.next();
        String playermove = input.toUpperCase();
        System.out.println("Player move is: "+ playermove);
        return playermove;
    }    
}
Comment

rock paper scissors java

import java.util.Random;

public class RandomComputerPlayer implements RPSPlayer {
    private final Random random;

    public RandomComputerPlayer(Random random) {
        this.random = random;
    }

    public String play() {
        return CHOICES[this.random.nextInt(CHOICES.length)];
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java stream sum integers 
Java :: byte to bufferedimage java 
Java :: java string split from input string 
Java :: java get classname 
Java :: get type of variable java 
Java :: autowire and qualifier 
Java :: android push notification icon 
Java :: iterating over a hashmap 
Java :: Loop Structure in Java 
Java :: java gettime 
Java :: java string padding 
Java :: java import text file into arraylist 
Java :: Horizontal RecyclerView android example 
Java :: android translate string java 
Java :: android toast message 
Java :: java arraylist to string 
Java :: spring h2 database example 
Java :: java foreach arraylist 
Java :: fill an array with random numbers between 1 and 100 java 
Java :: how to use scanners in java 
Java :: java array to string 
Java :: string to hexstring In java 
Java :: java read keyboard input 
Java :: android studio breakpoint not working 
Java :: remove all new line and other in string using java 
Java :: Who made java ? 
Java :: transformer un string en double java 
Java :: generate random password in spring boot 
Java :: java protected 
Java :: array to list java 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =