Java program to play Rock, Paper and Scissors with computer.
import java.lang.*;
import java.util.Scanner;
import java.util.Random;
public class RPS{
public int user_score =0 , comp_score = 0;
public String user_choice, comp_choice;
Random ran;
Scanner scan;
public RPS(){
ran = new Random();
scan = new Scanner(System.in);
}
public void userTurn(){
System.out.println("\n[1] Rock , [2] Paper , [3] Scissors");
System.out.print("Choice > ");
user_choice = whatIsIt(scan.nextInt() - 1);
}
public void machineTurn(){
comp_choice = whatIsIt(ran.nextInt(3) + 1);
}
public String whatIsIt(int a){
if(a==0){
return "Rock";
}
if(a==1){
return "Paper";
}
return "Scissors";
}
public String whoKilled(){
if(user_choice!= comp_choice){
if(user_choice== "Rock"){
if(comp_choice == "Paper"){
comp_score++;
return "comp";
}
else{
user_score++;
return "user";
}
}
if(user_choice== "Paper"){
if(comp_choice == "Rock"){
user_score++;
return "user";
}
else{
comp_score++;
return "comp";
}
}
if(user_choice== "Scissors"){
if(comp_choice == "Rock"){
comp_score++;
return "comp";
}
else{
user_score++;
return "user";
}
}
}
return "Draw";
}
public String whoWon(){
scan.close();
if(user_score>comp_score){
return "user";
}
else{
return "comp";
}
}
}
class Controller{
public static void main(String[] args) {
RPS rpc = new RPS();
int times=5;
String who;
System.out.println("##### Rock Paper Scissors #####");
while(times>0){
rpc.userTurn();
rpc.machineTurn();
who = rpc.whoKilled();
System.out.println("\nMachine Choose : "+rpc.comp_choice);
switch(who){
case "user" -> {
times--;
System.out.println("You killed Computer !!");
}
case "comp" -> {
times--;
// System.out.println("Machine Choose : "+rpc.comp_choice);
System.out.println("computer killed you !!");
}
default -> System.out.println("\nIts a Draw.");
}
}
System.out.println("\nScore: [ You -> "+rpc.user_score+" ] [ Computer -> "+rpc.comp_score+" ]");
if(rpc.whoWon() == "user"){
System.out.println("\nCongratulation!! You have won!!\n");
}
else{
System.out.println("\nComputer defeated you!!\n");
}
}
}
import java.util.Scanner;
import java.util.Random;
public class RPS{
public int user_score =0 , comp_score = 0;
public String user_choice, comp_choice;
Random ran;
Scanner scan;
public RPS(){
ran = new Random();
scan = new Scanner(System.in);
}
public void userTurn(){
System.out.println("\n[1] Rock , [2] Paper , [3] Scissors");
System.out.print("Choice > ");
user_choice = whatIsIt(scan.nextInt() - 1);
}
public void machineTurn(){
comp_choice = whatIsIt(ran.nextInt(3) + 1);
}
public String whatIsIt(int a){
if(a==0){
return "Rock";
}
if(a==1){
return "Paper";
}
return "Scissors";
}
public String whoKilled(){
if(user_choice!= comp_choice){
if(user_choice== "Rock"){
if(comp_choice == "Paper"){
comp_score++;
return "comp";
}
else{
user_score++;
return "user";
}
}
if(user_choice== "Paper"){
if(comp_choice == "Rock"){
user_score++;
return "user";
}
else{
comp_score++;
return "comp";
}
}
if(user_choice== "Scissors"){
if(comp_choice == "Rock"){
comp_score++;
return "comp";
}
else{
user_score++;
return "user";
}
}
}
return "Draw";
}
public String whoWon(){
scan.close();
if(user_score>comp_score){
return "user";
}
else{
return "comp";
}
}
}
class Controller{
public static void main(String[] args) {
RPS rpc = new RPS();
int times=5;
String who;
System.out.println("##### Rock Paper Scissors #####");
while(times>0){
rpc.userTurn();
rpc.machineTurn();
who = rpc.whoKilled();
System.out.println("\nMachine Choose : "+rpc.comp_choice);
switch(who){
case "user" -> {
times--;
System.out.println("You killed Computer !!");
}
case "comp" -> {
times--;
// System.out.println("Machine Choose : "+rpc.comp_choice);
System.out.println("computer killed you !!");
}
default -> System.out.println("\nIts a Draw.");
}
}
System.out.println("\nScore: [ You -> "+rpc.user_score+" ] [ Computer -> "+rpc.comp_score+" ]");
if(rpc.whoWon() == "user"){
System.out.println("\nCongratulation!! You have won!!\n");
}
else{
System.out.println("\nComputer defeated you!!\n");
}
}
}
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)