In: Computer Science
How do I write this method correctly? What Ihave is a Java project assignment where I'm supposed to create a Roshambo game. One of the classes that the project is supposed to have is a class called "RoshamboApp that let the user play 1 of 2 AI opponents, a "Bart" class, and a "Lisa" class. The instructions say "Create a class names RoshamboApp that allows player1 to play Bart or Lisa as shown in the console output. Rock should beat scissors, paper should beat rock, and scissors should beat paper." The code for a switch statement that I wrote is below, but the IDE says the way I wrote it is wrong. How can I write it correctly?
The assignment:
Project 10-3: Roshambo
Create an application that uses an enumeration and an abstract class.
Console
Welcome to the game of Roshambo
Enter your name: Joel
Would you like to play Bart or Lisa? (B/L): b
Rock, paper, or scissors? (R/P/S): r
Joel: rock
Bart: rock
Draw!
Play again? (y/n): y
Rock, paper, or scissors? (R/P/S): p
Joel: paper
Bart: rock
Joel wins!
Play again? (y/n): y
Rock, paper, or scissors? (R/P/S): s
Joel: scissors
Bart: rock
Bart wins!
Play again? (y/n): n
Specifications
My code so far for the class in question:
// R = rock
// P = paper
// S = scissors
import java.util.Scanner;
public class RoshamboApp {
String R;
String P;
String S;
String UserChoice;
String AIChoice;
switch(UserChoice)
{
case R:
if (AIChoice == P)
System.out.println("you lose.");
if (AIChoice == S)
System.out.println("you win!");
case P:
if (AIChoice == R)
System.out.println("you win!");
if (AIChoice == S)
System.out.println("you lose.");
case S:
if (AIChoice == R)
System.out.println("you lose.");
if (AIChoice == P)
System.out.println("you
win!");
}
if (UserChoice == AIChoice) System.out.println("it's a draw.");
}
/***************************Roshambo.java*******************/
public enum Roshambo
{
ROCK, PAPER, SCISSORS;
@Override
public String toString() {
if (this.ordinal() == 0)
return
"Rock";
if (this.ordinal() == 1)
return
"Paper";
if (this.ordinal() == 2)
return
"Scissors";
return "";
}
}
/*****************************Bart.java**********************/
public class Bart extends Player {
public Bart() {
name = "Bart";
}
// implements the generateRoshambo method from the
Player class
@Override
public Roshambo generateRoshambo() {
return Roshambo.ROCK;
}
}
/***********************Lisa.java*****************/
import java.util.Random;
public class Lisa extends Player {
public Lisa() {
name = "Lisa";
}
/**
*
* @return
*/
@Override
public Roshambo generateRoshambo() {
// randomly selects rock,paper, or
scissors
// (a one in three chance)
Random r = new Random();
int ch = r.nextInt(3);
if (ch == 0)
return
Roshambo.ROCK;
else if (ch == 1)
return
Roshambo.PAPER;
else
return
Roshambo.SCISSORS;
}
}
/****************************Player1.java*****************/
public class Player1 extends Player {
public Player1() {
super();
}
@Override
public Roshambo generateRoshambo() {
if (response.compareTo("R") == 0 ||
response.compareTo("r") == 0)
return
Roshambo.ROCK;
if (response.compareTo("P") == 0 ||
response.compareTo("p") == 0)
return
Roshambo.PAPER;
return Roshambo.SCISSORS;
}
}
/********************************Player.java*****************/
public abstract class Player {
public String name;
public String response;
public Player() {
}
// set method for name
public void setName(String name) {
this.name = name;
}
// get method for name
public String getName() {
return name;
}
// set method for response
public void setResponse(String response) {
this.response = response;
}
// get method for response
public String getResponse() {
return response;
}
// abstract method that allows an inheriting
class
// to generate and return a Roshambo value
public abstract Roshambo generateRoshambo();
}
/*******************************RoshamboApp.java*******************************/
import java.util.Scanner;
/**
*
* @author Christina
*/
public class RoshamboApp
{
public static void main(String args[])
{
Scanner s = new
Scanner(System.in);
char choice = ' ';
char player;
Player p = null;
String name;
// create 2-objects of Bart and
lisa
Bart b = new Bart();
Lisa l = new Lisa();
System.out.println("Welcome to the
game of Roshambo\n");
// input name
System.out.print("Enter your name :
");
name = s.nextLine();
System.out.print("Would you like to
play Bart or Lisa?(B/L): ");
String playerChoice =
s.nextLine();
// playing neighbour choice
player =
playerChoice.charAt(0);
// either bart
if (player == 'b' || player ==
'B')
p = b;
// or lisa
else if (player == 'l' || player ==
'L')
p = l;
// set exact name Bart for b|B,
Lisa for l|L
if (playerChoice.charAt(0) == 'b'
|| playerChoice.charAt(0) == 'B')
playerChoice =
"Bart";
else if (playerChoice.charAt(0) ==
'l' || playerChoice.charAt(0) == 'L')
playerChoice =
"Lisa";
p.setName(playerChoice);
// repeating loop with character variable choice
do {
System.out.print("Rock, paper or scissors?(R/P/S) : ");
String rps =
s.nextLine();
Player1 p1 = new
Player1();
p1.setName(name);
p1.setResponse(rps);
// set Rock
for r|R ,Paper for p|P, Scissors for s|S
if
(rps.charAt(0) == 'r' || rps.charAt(0) == 'R')
rps = "Rock";
else if
(rps.charAt(0) == 'p' || rps.charAt(0) == 'P')
rps = "Paper";
else if
(rps.charAt(0) == 's' || rps.charAt(0) == 'S')
rps = "Scissors";
System.out.println(name + " : " + rps);
// main logic
generate random number only ones.Each time it will give
different
// number
String roshambo
= p.generateRoshambo().toString(); // generate opponent's
roshambo
System.out.println(p.getName() + " : " + roshambo);
// match
generated random number to rashambo enterd by user for all
cases
if
(Character.toUpperCase(rps.charAt(0)) == roshambo.charAt(0))
System.out.println("Draw!");
else if
(rps.equals("Paper") && roshambo.equals("Rock"))
System.out.println(p1.getName() + " Wins");
else if
(roshambo.equals("Paper") && rps.equals("Rock"))
System.out.println(p.getName() + " Wins");
else if
(rps.equals("Paper") && roshambo.equals("Scissors"))
System.out.println(p.getName() + " Wins!");
else if
(roshambo.equals("Paper") && rps.equals("Scissors"))
System.out.println(p1.getName() + " Wins!");
else if
(rps.equals("Scissors") && roshambo.equals("Rock"))
System.out.println(p.getName() + " Wins");
else if
(roshambo.equals("Scissors") && rps.equals("Rock"))
System.out.println(p1.getName() + " Wins");
// see if the user wants to continue
System.out.print("Play again?(y/n): ");
choice =
s.nextLine().toLowerCase().charAt(0);
System.out.println();
} while (choice != 'n');
}
}
/******************output*******************/
Welcome to the game of Roshambo
Enter your name : Joel
Would you like to play Bart or Lisa?(B/L): b
Rock, paper or scissors?(R/P/S) : r
Joel : Rock
Bart : Rock
Draw!
Play again?(y/n): y
Rock, paper or scissors?(R/P/S) : P
Joel : Paper
Bart : Rock
Joel Wins
Play again?(y/n): n
Please let me know if you have any doubt or modify the answer, Thanks :)