//RockPaperScissors.java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
private static boolean playAgain(Scanner scanner) {
System.out.println("Play again? Y(8), N(9)?");
switch (scanner.nextInt()) {
case 8:
System.out.println("Rock, Paper, Scissors!");
return true;
default:
System.out.println("Thanks for playing!");
return false;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
RPSPlayer computer = new RandomComputerPlayer(new Random());
RPSPlayer human = new HumanPlayer(scanner);
// Count the number of wins
int winCount = 0;
System.out.println("Rock Paper Scissors");
do {
for (int i = 1; i <= 3; i++) {
String comp = computer.play();
String you = human.play();
System.out.printf("%s vs. %s\n", comp, you);
if (you.equals(comp)) {
System.out.println(", IT'S A TIE!");
} else if (("Rock".equals(you) && "Scissors".equals(comp))
|| ("Scissors".equals(you) && "Paper".equals(comp))
|| ("Paper".equals(you) && "Rock".equals(comp))) {
winCount++;
}
}
if (winCount >= 2)
System.out.println("! You win!");
else
System.out.println("! You lose!");
} while (playAgain(scanner));
}
}
//RPSPlayer.java
public interface RPSPlayer {
String[] CHOICES = new String[] { "Rock", "Paper", "Scissors" };
/**
* Returns one of "Rock", "Paper", or "Scissors".
*/
String play();
}
//HumanPlayer.java
import java.util.Scanner;
public class HumanPlayer implements RPSPlayer {
private final Scanner scanner;
public HumanPlayer(Scanner scanner) {
this.scanner = scanner;
}
public String play() {
System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");
int choice = this.scanner.nextInt();
// Keeping things simple, not doing any validation here
return CHOICES[choice - 1];
}
}
//RandomComputer.java
import java.util.Random;
public class RandomComputerPlayer implements RPSPlayer {
private final Random random;
public RandomComputerPlayer(Random random) {
this.random = random;
}
public String play() {
return CHOICES[this.random.nextInt(CHOICES.length)];
}
}
Output:
Objective: Write a program that simulates a game of rock, paper, scissors between a human and...
For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....
IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....
(Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...
In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....
Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive: human vs. computer! Making the game interactive presents a new issue for us -- how do we get the computer's choice? To solve this, we will use a "random number generator", which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually "thinking" and making its own decisions. This is the largest, most involved program...
C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...
i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...
java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...
Java CSC252 Programming II Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...
It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...