Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java game

// 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

PREVIOUS NEXT
Code Example
Java :: how to make an int into a string java 
Java :: array to arraylist java 
Java :: Java JPanel set background color 
Java :: factorial of a number using recursion in java 
Java :: convert list of integer to array in java 
Java :: how to generate random number in java 
Java :: how to reverse a string in java 
Java :: how to add to a file in java 
Java :: How to perform breadth first search through a binary tree, in Java? 
Java :: java collection to list 
Java :: how to access variable from another class in java 
Java :: java array remove space 
Java :: set intersection java 
Java :: sorting the characters in a String in java 
Java :: How to invoke sendKeys() method for Integer values using Selenium and Java 
Java :: enum type spring boot entity 
Java :: pass list to intent in android java 
Java :: how to create a stack data structure in java? 
Java :: get type java 
Java :: java parse unix timestamp 
Java :: string array to string java 
Java :: java detect new line in string 
Java :: java get number of threads 
Java :: How to iterate hashmap entries in java 
Java :: how to add 2 numbers in java 
Java :: James Gosling 
Java :: find min in array java 
Java :: junit vintage engine maven 
Java :: implementing iterator for linked list java 
Java :: load file as string java 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =