Westonci.ca is the best place to get answers to your questions, provided by a community of experienced and knowledgeable experts. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform. Get precise and detailed answers to your questions from a knowledgeable community of experts on our Q&A platform.
Sagot :
Answer:
import java.util.*;
class Main {
private enum RPS { Rock, Paper, Scissors };
private static RPS[] options = { RPS.Rock, RPS.Paper, RPS.Scissors };
private static String[] names = {"Rock", "Paper", "Scissors"};
// Return 1 if player 1 beats player 2, 0 otherwise
static public int rps( RPS p1, RPS p2 ) {
if (p1 == RPS.Rock && p2 == RPS.Scissors) return 1;
if (p1 == RPS.Scissors && p2 == RPS.Paper) return 1;
if (p1 == RPS.Paper && p2 == RPS.Rock) return 1;
return 0;
}
static public int playRps() {
Random rnd = new Random();
int totalPoints = 0;
for(int round = 0; round < 24; round++) {
int randomNumber = rnd.nextInt(2);
RPS player1 = options[randomNumber];
RPS player2 = options[round%3];
String name1 = names[randomNumber];
String name2 = names[round%3];
int score = rps(player1, player2);
if (player1 == player2) {
System.out.printf("%d. Both players %s. Draw.\n", round+1, name1);
} else if (score == 0) {
System.out.printf("%d. %s does not beat %s, player 1 loses.\n", round+1, name1, name2);
} else {
System.out.printf("%d. %s beats %s, player 1 wins.\n", round+1, name1, name2);
}
totalPoints += score;
}
return totalPoints;
}
public static void main(String[] args) {
System.out.println("Playing RPS.");
int total = playRps();
System.out.printf("Player 1 earned %d points.\n", total);
}
}
We appreciate your time. Please revisit us for more reliable answers to any questions you may have. Thank you for your visit. We're committed to providing you with the best information available. Return anytime for more. Thank you for visiting Westonci.ca. Stay informed by coming back for more detailed answers.