import java.util.Random;
import java.util.Scanner;
public class Game
{
private static int userWinningCount, computerWinningCount;
private int userInput, computerInput;
public void setUserInput(int userInput)
{
this.userInput = userInput;
}
public void setComputerInput()
{
Random r = new Random();
computerInput = r.nextInt(3-1+1)+1;// corresponding to r.nextInt(max-min+1)+min;
}
public String computerChoice()
{
if(computerInput == 1)
return "Stone";
else if(computerInput == 2)
return "Paper";
else
return "Scissor";
}
public String getResult()
{
if(userInput == computerInput)
return "Tie ";
else if(userInput == 1 && computerInput == 2)// stone = 1, paper = 2, scissor = 3;
return "Computer won";
else if(userInput == 1 && computerInput == 3)
return "You won";
else if(userInput == 2 && computerInput == 1)
return "You won";
else if(userInput == 2 && computerInput == 3)
return "Computer won";
else if(userInput == 3 && computerInput == 1)
return "Computer won";
return "You won";// i.e. user has scissor and computer has paper.
}
public static void main(String []args)
{
String s = "yes";
Game obj = new Game();
Scanner input = new Scanner(System.in);
while(s.equalsIgnoreCase("yes"))
{
System.out.println("Ener your choice (Stone, Paper, Scissor)");
String choice = input.next();
choice = choice.toLowerCase();
if(choice.equals("stone"))
obj.setUserInput(1);
else if(choice.equals("paper"))
obj.setUserInput(2);
else if(choice.equals("scissor"))
obj.setUserInput(3);
else
System.out.println("Enter valid option");
obj.setComputerInput();
System.out.println("The computer chose : "+obj.computerChoice());
if(obj.getResult().equals("You won"))
userWinningCount++;
else if(obj.getResult().equals("Computer won"))
computerWinningCount++;
System.out.println(obj.getResult());
System.out.print("Your winning count = "+userWinningCount);
System.out.print(" and Computer winning count = "+computerWinningCount);
System.out.println("\n Wanna continue playing game ? (enter yes) ");
s = input.next();
}
input.close();
}
}
Sometimes the only way to move forward is to revisit the things in your past that were holding you back. You have to deal with them head on, no matter how scary they may be. Because once we do, you'll see that you can go further than you ever imagined. सो जाएँगे कल लिपटकर तिरंगे के साथ अलमारी में... देशभक्ति है साहब.... तारीखों पर जागती है। The only way out is through.
Saturday, 19 March 2016
Stone Paper Scissor game in Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment