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 :: static class java 
Java :: convert Date to LocalDate via Instant 
Java :: how to concatenate two strings in java 
Java :: java use variables inside strings 
Java :: how to create an entry in java 
Java :: how to create set java 
Java :: how to output in java 
Java :: how to convert string to int in java 
Java :: arraylist vs vector in java 
Java :: java stream sort 
Java :: what is static setter and getter examples in java 
Java :: parcourir un string java 
Java :: java how to know if there is something on arguments 
Java :: FlutterSound.java uses or overrides a deprecated API. 
Java :: java set classpath 
Java :: how to check number of messages in kafka topic 
Java :: java break and continue 
Java :: change text size java 
Java :: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 
Java :: What is the function of an IntentFilter? 
Java :: bukkit console filter 
Java :: get current background color of TextView java 
Java :: how to install clojure on linux 
Java :: java stream code to ignore null 
Java :: get host from request object java 
Java :: difference between maven plugin and dependency 
Java :: getresources in adapter android 
Java :: java add element to map 
Java :: expression régulière téléphone java 
Java :: find the third largest number in an array 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =