Need help writing a rock, paper, scissors program with these requirements. Decla
ID: 640545 • Letter: N
Question
Need help writing a rock, paper, scissors program with these requirements.
Declare String constants for "rock", "paper" and "scissors" and use these constants in the code.
The user's choice must be read as a String.
The program should not be case sensitive. That is an entry of uppercase P (for paper) is equivalent to an entry of paper or lowercase p, etc.
The program must validate the user's entry, printing an error message and exiting if invalid.
The basic algorithm for playing the game is:
generate a random play for the computer
prompt & read the user's play
if the user's play is valid
determine and print the winner by comparing the computer's choice with the user's choice stating the reason for the win. (Use a nested-if for this.)
else
print an error message and exit
It has to use this random number generator.
The output should look like:
Let's play Rock, Paper, Scissors! The computer has made a choice, but it is a secret for now! Enter your choice of R for Rock, P for Paper or S for Scissors: R Okay, I got that. You chose Rock! Now I can reveal that the Computer chose Scissors and determine a winner... Game results: You won! Rock beats Scissors... (win, lose, and tie are options as well).
I have been working on this for hours and have not come up with a viable task.
Explanation / Answer
scissors> java -jar scissors.jar */ abstract class GameMove {} class Scissors extends GameMove {} class Paper extends GameMove {} class Rock extends GameMove {} String plays(GameMove m1, GameMove m2){ if (m1 == m2) return m1 + " draws " + m2; else return m2.plays(m1); } plays(Scissors m1, Paper m2) = "Scissors cut Paper"; plays(Paper m1, Rock m2) = "Paper wraps Rock"; plays(Scissors m1, Rock m2) = "Rock breaks Scissors"; toString(Scissors m)="Scissors"; toString(Paper m)="Paper"; toString(Rock m)="Rock"; void main(String[] args){ let scissors = new Scissors(); let rock = new Rock(); let paper = new Paper(); println( rock.plays(scissors) ); println( rock.plays(paper) ); println( scissors.plays(paper) ); println( scissors.plays(scissors) ); } /* Notes - language abstract class GameMove {} Declare an abstract class and 3 subclasses: Scissors, Paper, Rock. String plays(GameMove m1, GameMove m2){ Declare a multimethod with an implementation - if the moves are the same it's a draw, if the moves are different then there should be a more specific implementation so reverse the parameters and try again (double dispatch). plays(Scissors m1, Paper m2) = "Scissors cut Paper"; This implementation will be selected when the first parameter value is an instance of Scissors and the second parameter value is an instance of Paper (or a subclass). plays(Paper m1, Rock m2) = "Paper wraps Rock"; This implementation will be selected when the first parameter value is an instance of Paper and the second parameter value is an instance of Rock (or a subclass). plays(Scissors m1, Rock m2) = "Rock breaks Scissors"; This implementation will be selected when the first parameter value is an instance of Scissors and the second parameter value is an instance of Rock (or a subclass). We could implement the game using value dispatch. Can you rewrite Scissors, Paper, Rock; using an enum, or int constants or string literals? */ /* Notes - Compile & Run The Nice compiler compiles PACKAGES (directories) not files, so we must tell the compiler which directory contains the package directory (scissors) not which directory contains the file (main.nice). projects> nicec scissors OK projectsscissors> nicec --sourcepath=.. scissors OK projectsscissors> nicec scissors FAILS! the compiler will try to find projectsscissorsscissors Let's make the compiler output to a jar file (scissors.jar): projectsscissors> nicec --sourcepath=.. -a scissors.jar scissors nice.lang: parsing scissors: parsing scissors: typechecking scissors: generating code scissors: linking scissors: writing in archive nice.lang: writing in archive Let's run the program: projectsscissors> java -jar scissors.jar Rock breaks Scissors Paper wraps Rock Scissors cut Paper Scissors draws Scissors */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.