Sunday 28 June 2015

' ROCK-PAPER-SCISSOR-LIZARD-SPOCK ' : GAME IN JAVA

/* ROCK-PAPER-SCISSOR-LIZARD-SPOCK */

My Computation

import java.util.*;//Taking Input through Scanner class
import java.util.Random;//Importing class in order to get a random number [required later]
class rpsls
{
    Scanner in = new Scanner(System.in);
    public  void input()//To take the input
    {
     
        System.out.println("Hey, remember how we used to play 'Stone-Paper-Scissor' in our childhood??");
        System.out.println("So , here is a modified version of the game : Stone-Paper-Scissor-Lizard-Spock \n");
        System.out.println("For rules , check the wiki page,i.e: https://en.wikipedia.org/wiki/Rock-paper-scissors#Additional_weapons");
        System.out.println("\n Enter the numbers adjacent to the names : 0-Rock , 1-Spock , 2-Paper , 3-Lizard and 4-Scissor");
     
        int cond = 7 ;
        for(int i = 1 ;cond!=9 ;i++)//To ensure that the game ends when user inputs '9'
        {
            System.out.println("Enter your choice");
            int ch = in.nextInt();
            if(ch>4||ch<0)//to check the validity of the number
            {
                System.out.println("Enter a valid number");
            break;
        }
            player_chooses(ch);
            compute(ch);
            cond = in.nextInt();//whether the user wants to end the program or not
         
        }
        System.out.println("Thank you for playing!");
    }
    private void compute(int ch)//This is the main function that computes the result
    {
     Random generator = new Random();
     int comp_choice = generator.nextInt(5) + 0;//To generate a number between 0-4
     comp_chooses(comp_choice);
     int diff = (ch - comp_choice)%5;//This was my method of decreasing the length of the program ; Otherwise, it would have reqiured us to write 5*5 lines of code
     if(diff==0)
     System.out.println("Match tie!");
     else if(diff==1||diff==2)
     System.out.println("Player wins!");
     else
     System.out.println("Computer wins!");
     System.out.println("\n If you want to exit press 9 , else press any digit");
    }
    private void player_chooses(int ch)//To print user's choice
    {
        switch (ch)
        {
            case 0:
            System.out.println("Player chooses Rock");
            break;
            case 1:
            System.out.println("Player chooses Spock");
            break;
            case 2:
            System.out.println("Player chooses Paper");
            break;
            case 3:
            System.out.println("Player chooses Lizard");
            break;
            case 4:
            System.out.println("Player chooses Scissor");
            break;
        }
}
private void comp_chooses(int ch)//To print computer's choice
    {
        switch (ch)
        {
            case 0:
            System.out.println("Computer chooses Rock");
            break;
            case 1:
            System.out.println("Computer chooses Spock");
            break;
            case 2:
            System.out.println("Computer chooses Paper");
            break;
            case 3:
            System.out.println("Computer chooses Lizard");
            break;
            case 4:
            System.out.println("Computer chooses Scissor");
            break;
        }
}
}
         
        

2 comments:

  1. Great program !
    Plus, it's really fun to play the game :)

    ReplyDelete
  2. Thanks , stay tuned for more interesting games.........

    ReplyDelete