Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Write a game that plays many rounds of rock-paper-scissors. The user and comp

ID: 3621872 • Letter: 5

Question

5. Write a game that plays many rounds of rock-paper-scissors. The user and computer will each
choose between three items: rock (defeats scissors, but loses to paper), paper (defeats rock, but
loses to scissors) and scissors (defeats paper, but loses to rock). If the player and computer
choose the same item, the game is a tie.



An extension of this program would be to write different algorithmic strategies for choosing
the best item. Should the computer pick randomly? Should it always pick a particular item or
repeating pattern of items? Should it count the number of times the opponent chooses various
items and base its strategy on this history?

Explanation / Answer

please rate - thanks

import java.util.*;
    public class rockpaperscissor
   {static String moves[]={"rock","paper","scissors"};
    static Random randomGenerator = new Random();
      public static void main(String[] args)
      {Scanner in = new Scanner(System.in);
    boolean again=true;

   int usermove;
int computermove;
while(again)
   {usermove= user(in);
     computermove=computer();
     score(usermove,computermove);
     again=conclusion(in);
     }

}

public static int user(Scanner in)
{char a;
int move=0;
    System.out.print("Choose your weapon (r,p,s): ");
    a=in.nextLine().charAt(0);
    a=Character.toLowerCase(a);
    while(a!='r'&&a!='p'&&a!='s')
     {System.out.print("Choose your weapon (r,p,s): ");
     a=in.nextLine().charAt(0);
     a=Character.toLowerCase(a);
         }
     switch(a)
       {case 'r': move=0;
                  break;
        case 'p': move=1;
                  break;
        case 's': move=2;
                  break;
        }   
return move;
}

public static int computer()
{int move=randomGenerator.nextInt(3);

System.out.println("I chose "+moves[move]);
return move;
}

public static void score(int user,int computer )
{
if(user==computer)
     System.out.println("Tie!");
else if(user==0&&computer==2)
     System.out.println("You break my sissors. you win!");
else if(user==0&&computer==1)
     System.out.println("I cover your rock. you lose!");
else if(user==1&&computer==2)
     System.out.println("I cut your paper. you lose!");
else if(user==1&&computer==0)
      System.out.println("You cover my rock. you win!");
else if(user==2&&computer==0)
      System.out.println("I break your sissors, you lose!");
else
       System.out.println("You cut my paper. you win!");
return;
}

public static boolean conclusion(Scanner in)
{char a;
System.out.println(" Good game. Play again!(Y/N) ");
a=in.nextLine().charAt(0);
a=Character.toLowerCase(a);
while(a!='y'&&a!='n')
     {System.out.print("invalid entry Play again(Y/N)? ");
     a=in.nextLine().charAt(0);
a=Character.toLowerCase(a);
     }
if(a=='y')
     return true;
else
     return false;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote