CSIS 1410 A08 Rock Paper Scissors I have this assignment completed but, I have a
ID: 3776325 • Letter: C
Question
CSIS 1410 A08 Rock Paper Scissors
I have this assignment completed but, I have a glitch where scissors is beating rock, I know this is an issue with the ordinals in EnumRPS I just can't seem to get it working though. Can someone help me with my code?
//EnumRPS
package RPS;
import javax.swing.*;
public enum EnumRPS
{
ROCK(new ImageIcon(EnumRPS.class.getResource("Rock.jpg"))),
PAPER(new ImageIcon(EnumRPS.class.getResource("Paper.jpg"))),
SCISSORS(new ImageIcon(EnumRPS.class.getResource("Scissors.jpg")));
private ImageIcon image;
private EnumRPS(ImageIcon newImage)
{
image = newImage;
}
public ImageIcon getImage()
{
return image;
}
public static int evaluate(EnumRPS choice1, EnumRPS choice2)
{
if(choice1.ordinal() > choice2.ordinal())
return 1;
else if(choice1.ordinal() < choice2.ordinal())
return 2;
else
return 0;
}
public static EnumRPS randomChoice()
{
return values()[(int) (Math.random() * values().length)];
}
}
//RockPaperScissorsGUI
package RPS;
import javax.swing.*;
public enum EnumRPS
{
ROCK(new ImageIcon(EnumRPS.class.getResource("Rock.jpg"))),
PAPER(new ImageIcon(EnumRPS.class.getResource("Paper.jpg"))),
SCISSORS(new ImageIcon(EnumRPS.class.getResource("Scissors.jpg")));
private ImageIcon image;
private EnumRPS(ImageIcon newImage)
{
image = newImage;
}
public ImageIcon getImage()
{
return image;
}
public static int evaluate(EnumRPS choice1, EnumRPS choice2)
{
if(choice1.ordinal() > choice2.ordinal())
return 1;
else if(choice1.ordinal() < choice2.ordinal())
return 2;
else
return 0;
}
public static EnumRPS randomChoice()
{
return values()[(int) (Math.random() * values().length)];
}
}
//TestRockPaperScissorsGUI
package RPS;
import java.awt.*;
public class TestRockPaperScissorsGUI
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
RockPaperScissorsGUI frame = new RockPaperScissorsGUI();
frame.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class HelloWorld {
public static void main(String[] args) throws java.lang.Exception {
Scanner Ilya = new Scanner(System. in );
Random rnd = new Random();
int input;
int B = 1;
System.out.println("ILYA IS THE GREATEST");
System.out.println("Pick 1,2, or 3 for:");
System.out.println("ROCK (1), PAPER(2), or SCISSOR (3)");
while (B != 0) {
// 1 = rock
// 2 = paper
// 3 = scissors
// N= Ilya.nextInt();
int Rock = 1, Paper = 2, Scissor = 3;
input = Ilya.nextInt();
int randomNumber = rnd.nextInt(3 - 1 + 1) + 1;
if (randomNumber == Rock) {
if (input == Rock) {
System.out.println("Rock Vs. Rock: TIE");
} else if (input == Paper) {
System.out.println("Paper Vs. Rock: YOU WIN");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Rock: YOU LOSE");
}
} else if (randomNumber == Paper) {
if (input == Rock) {
System.out.println("Rock Vs. Paper: YOU LOSE");
} else if (input == Paper) {
System.out.println("Paper Vs. Paper: TIE");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Paper: YOU WIN");
}
} else if (randomNumber == Scissor) {
if (input == Rock) {
System.out.println("Rock Vs. Scissor: YOU WIN");
} else if (input == Paper) {
System.out.println("Paper Vs. Scissor: YOU LOSE");
} else if (input == Scissor) {
System.out.println("Scissor Vs. Scissor: TIE");
}
}
int Y=8, N=9;
System.out.println("Do you want to play again? Y(8)/N(9)");
input = Ilya.nextInt();
if(input==Y){
B=1;
System.out.println("Rock, Paper,SCISSOR");
}
else if(input==N)
{System.exit(0);
System.out.println("GOODBYE");
}
}
}
}