Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

RockPaperScissorsLizardSpock

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am struggling heavily with my play method. I am required to generate ints in the range of [1 to 5], one per player. 1 refers to Rock, 2 refers to Paper, 3 refers to Scissors, 4 refers to Lizard, and 5 refers to Spock. Afterwards I must determine who wins according to the rules:
Rule 1: Scissors cut paper
Rule 2: Paper covers rock
Rule 3: Rock crushes lizard
Rule 4: Lizard poisons Spock
Rule 5: Spock smashes (or melts) scissors
Rule 6: Scissors decapitate lizard
Rule 7: Lizard eats paper
Rule 8: Paper disproves Spock
Rule 9: Spock vaporizes rock
Rule 10: Rock breaks scissors



Is anyone able to help me with the errors, I am really stuck.
 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Katherine, welcome to the Ranch!

Yes, there are lots of people who can help you with the errors. But... you have to tell us what they are. As you can imagine, it's hard to discuss errors when you don't know anything about them. Copying the error messages from wherever you see them and pasting them into your post will be a lot easier for all concerned than trying to retype them, too.
 
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have the items 1-5 and if you consider a rule like
where value = (player1item * 10) + player2item
then you can create all the rules in a similar fashion and use a switch() statement for all the rules to determine if player 1 won. If there is no winner, then switch p1 and p2, i.e. value = player1item + (player2iitem * 10), and go through a second switch of rules. If a match is found then player 2 wins. If neither 1 or 2 wins then it is a tie.

 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you have the items 1-5 and if you consider a rule like
where value = (player1item * 10) + player2item
then you can create all the rules in a similar fashion and use a switch() statement for all the rules to determine if player 1 won. If there is no winner, then switch p1 and p2, i.e. value = player1item + (player2iitem * 10), and go through a second switch of rules. If a match is found then player 2 wins. If neither 1 or 2 wins then it is a tie.



Thank you for your response! I have added global variables and think this is a good approach towards this problem although I still require further explanation, now I am wondering how code the individual case statements. I have updated my code:
 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.Random;
import java.util.Scanner;

public class RockPaperScissorsLizardSpock {

   private int consecutiveWins;
   private int lastWiner;

   public static final int LIZARD = 4;
   public static final int PAPER = 2;
   public static final int PLAYER1 = 1;
   public static final int PLAYER2 = 2;
   public static final int ROCK = 1;
   public static final int SCISSORS = 3;
   public static final int SPOCK = 5;
   
   public static final int SCISSORS_CUT_PAPER = 32;
   public static final int PAPER_COVERS_ROCK = 31;
   public static final int ROCK_CRUSHES_LIZARD = 14;
   public static final int LIZARD_POISONS_SPOCK = 45;
   public static final int SPOCK_MELTS_SCISSORS = 53;
   public static final int SCISSORS_DECAPITATE_LIZARD = 34;
   public static final int LIZARD_EATS_PAPER = 42;
   public static final int PAPER_DISPROVES_SPOCK = 25;
   public static final int SPOCK_VAPORIZES_ROCK = 51;
   public static final int ROCK_BREAKS_SCISSORS = 13;

   public int getConsecutiveWins() {
       return consecutiveWins;
   }

   public int getLastWiner() {
       return lastWiner;
   }

   public int random() {
       return (int)(Math.round(Math.random() * 4) + 1);
   }

   public static String convert(int i) {
    switch (i) {
       case 1:
           return "rock";
       case 2:
           return "paper";
   case 3:
       return "scissors";
   case 4:
       return "lizard";
       case 5:
       return "spock";
       default:
       return "Invalid choice";
   }
   }

   public void play(int p1, int p2) {
       // not sure how
int consecutive = 0;
    if (p1 < 1 || p1 > 5 || p2 < 1 || p2 > 5) {
           System.out.printf("invalid input:%d %d\n", p1, p2);
           return;
       }
    switch( p1 * 10 + p2 )
       {
       case SCISSORS_CUT_PAPER:
           break
       case PAPER_COVERS_ROCK:
        break;
       case ROCK_CRUSHES_LIZARD:
        break;
       case ROCK_CRUSHES_LIZARD:
        break;
       case LIZARD_POISONS_SPOCK:
           winner = 1;
           break;
    case SPOCK_MELTS_SCISSORS:
    break;
    case SCISSORS_DECAPITATE_LIZARD:
       break;
    case LIZARD_EATS_PAPER:
    break;
    case PAPER_DISPROVES_SPOCK:
    break;
    case SPOCK_VAPORIZES_ROCK:
    break;
    case ROCK_BREAKS_SCISSORS:
    break;
   }
   }
   
   public static void main(String[] args) {
       RockPaperScissorsLizardSpock agame = new RockPaperScissorsLizardSpock();
       Scanner input = new Scanner(System.in);
       System.out.println("How many consecutive wins needed?");
       int cW = input.nextInt();
       while (agame.getConsecutiveWins() < cW) {
           agame.play(agame.random(), agame.random());
       }
       System.out.printf("Game Over! LastWiner: Player %d\n", agame.getLastWiner());
       input.close();
   }
}
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The case statements are followed by (optionally) some task to do followed by "break;". IF you leave out (or forget) the break then the execution "falls through". Usually this is not a desirable thing to do but you can use "fall through" to your advantage in this case which means you don't have to write a set of instructions to be executed for each and every case.

For example:

For your game all the rules will fall through

 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have fixed all these breaks; I am wondering what I am to code within the cases to determine whether p1 or p2 wins.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were close. You just had to put a statement that says IF ANY of these RULES match THEN player 1 wins.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Should be 21.
 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You were close. You just had to put a statement that says IF ANY of these RULES match THEN player 1 wins.



Would this be required within each case statement like this:

 
Katherine Johnson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should code statements to subtract 10 from the global variable?
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Katherine Johnson wrote:Would this be required within each case statement like this:


No. As I've stated the advantage of using fall through is that you only have to put your logic in once, after all the cases. You wouldn't need the if(true) because the case statements already compare the values for you. If you want to print "p1 wins" you could just do it after winner=1. Perhaps you don't need the "winner" variable if you assume that if player 1 didn't win then player 2 did.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Katherine Johnson wrote:I should code statements to subtract 10 from the global variable?

I'm not sure what you're thinking here. I don't see any place where subtracting 10 would serve a purpose.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Katherine Johnson wrote:Would this be required within each case statement like this:


Just to be clear on fall throughs: the code will get executed in the order that you've coded them. In this case if SCISSORS_CUT_PAPER matches then you will print "p1 wins" and then it will keep going (fall through) to see if there are any other matches and statements to execute. In your code if PAPER_COVERS_ROCK is a match it would have alread skipped over your print statement.
 
Marshal
Posts: 74780
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . you can use "fall through" to your advantage in this case  . . .

It is rather unusual for fallthrough to be useful, so all switch statements using fallthrough shou‍ld have a comment saying where the fallthrough is wanted; something like “// fallthrough intended” is usually sufficient. Otherwise somebody reading your code will assume the fallthrough is a mistake and “correct” the code by adding break;

And welcome to the Ranch
 
Master Rancher
Posts: 4606
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here: http://www.dreamincode.net/forums/topic/406227-rockpaperscissorslizardspock/
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enough time has passed so I don't think posting the answer should be an issue.

 
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Enough time has passed so I don't think posting the answer should be an issue.



Here's my variation using enums, all the magic of numbers, etc.. are encapsulated within the enum and only a single method is public:
 
salvin francis
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
usage :
Now caller code is free to implement it in any way using javafx/swing/console. Optionally, the enum can have a "name" field which can be initiated to "Rock", "Paper", "Scissors", etc.
I omitted a null check in the getWinner method for player1Action, player2Action.
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a relatively well-known formula for calculating who the winner of RPS and other variants like RPSLS without having to resort to a comprehensive list of possible outcomes, etc. The formula works by assigning values to each possible gesture and using +/- and % operations.  

For RPS, Rock=0, Paper=1, Scissors=2 and the algorithm would be

On line 2, the winner will equal either a or b.

For Rock-Paper-Scissors-Lizard-Spock, the numbers are assigned differently:
Rock=0, Spock=1, Paper=2, Lizard=3, Scissors=4

The algorithm is very similar to the one for RPS:

You can work out a fairly elegant solution using enum values for Gesture. I may have even posted a solution based on this in the past. You can even use source ordering and the built-in Enum.ordinal() function.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of ways you can design a fluent API with the enums as well.  One option has already been shown.  I like to lay out the high-level code I want to write first, like this maybe:

This is my starting point for building out the enum class Gesture.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find these approaches academically interesting but they would be more complicated if part of the requirements were to present the winning rule in textual format.
 
salvin francis
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:... but they would be more complicated if part of the requirements were to present the winning rule in textual format.


On the contrary, enums are more helpful in this manner. The task of making a meaningful text does not interfere with the game winning/losing logic (unless one puts everything including the winning losing messages in the enum). Enums make comparisons easier.
 
Carey Brown
Saloon Keeper
Posts: 8949
77
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show me where you can get the text for "rock breaks scissors".

salvin francis wrote:Here's my variation using enums, all the magic of numbers, etc.. are encapsulated within the enum and only a single method is public:

 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I find these approaches academically interesting but they would be more complicated if part of the requirements were to present the winning rule in textual format.


I don't think it's really that much more complicated. It still involves a Map except the key would be something like a GesturePair that implements the methods needed to make it a useful map key.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic