D : Rock-paper-scissors-lizard-Spock

04Dec13

I recently participated in the Advent Programming Contest 2013 organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt), wherein – as I’d recently received from Udacity a certificate for Introduction to Programming : Problem Solving with Java (CS046) – I challenged myself to program my solutions to the problems posed by the contest using only Java (I’d used C and C++ when I’d participated in the contest in 2012); I solved eighteen of the problems posed by the contest, achieving a rank of 10th (out of 155 participants).

As submissions of solutions to the contest are now closed, I’m posting here the solutions I submitted to the contest.

Problem (D; easy difficulty):

Rock-paper-scissors-lizard-Spock

The game rock-paper-scissors-lizard-Spock is an expansion of the classic selection game rock-paper-scissors. The principle is the same, but it includes two additional choices: the lizard (formed by the hand as a sock-puppet-like mouth) and Spock (formed by the Star Trek Vulcan salute). The game was invented by Sam Kass and Karen Bryla. The rules of rock-paper-scissors-lizard-Spock are:

  • Scissors cut paper
  • Paper covers rock
  • Rock crushes lizard
  • Lizard poisons Spock
  • Spock smashes scissors
  • Scissors decapitate lizard
  • Lizard eats paper
  • Paper disproves Spock
  • Spock vaporizes rock
  • Rock crushes scissors

Problem

Write a program that reads the choices of Player 1 and Player 2, seperated by a comma (no spaces, all words except Spock are lower case) from the standard input. Depending on the pairing, your program should write back the respective rule (see above) or, in case of a tie, “Tie”. Each winning choice earns the respective player a point. Once one player reaches 3 points, announce the winner (“Player 1 wins!” or “Player 2 wins!”) and terminate the program.

Sample Input 1

lizard,rock
Spock,rock
paper,paper
rock,lizard
scissors,paper

Sample Output 1

Rock crushes lizard
Spock vaporizes rock
Tie
Rock crushes lizard
Scissors cut paper
Player 1 wins!

Solution (D.java):

import java.util.Scanner;

class Fight
{
  public String m_actionSequence;
  public byte m_winner;

  public Fight( String s[], byte b )
  {
    m_actionSequence = capitalise( s[ 0 ] ) + " " + s[ 1 ] + " " + s[ 2 ];
    m_winner = b;
  }

  private String capitalise( String p )
  {
    return p.substring( 0, 1 ).toUpperCase() + p.substring( 1, p.length());
  }
}

public class D
{
  public static void main( String p[] )
  {
    Scanner s = new Scanner( System.in );
    byte player_1_points = 0, player_2_points = 0;
    do
    {
      final String l = s.nextLine();
      final int i = l.indexOf( ',' );
      final String player_1 = l.substring( 0, i );
      final String player_2 = l.substring( i + 1, l.length());
      if( player_1.equals( player_2 ))
        System.out.println( "Tie" );
      else
      {
        Fight f = StarTrekFightMusic( new String[]{ player_1, player_2 } );
        System.out.println( f.m_actionSequence );
        if( f.m_winner == 1 )
          player_1_points ++;
        else
          player_2_points ++;
      }
    }
    while( player_1_points != 3 && player_2_points != 3 );
    System.out.println( "Player " + (( player_1_points == 3 )?1:2 ) + " wins!" );
  }

  private static Fight StarTrekFightMusic( String players[] )
  {
    Fight f = StarTrekFightMusic( players, new String[]{ "rock", "scissors" }, "crushes" );
    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "rock", "lizard" }, "crushes" );

    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "paper", "rock" }, "covers" );
    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "paper", "Spock" }, "disproves" );

    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "scissors", "paper" }, "cut" );
    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "scissors", "lizard" }, "decapitate" );

    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "lizard", "Spock" }, "poisons" );
    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "lizard", "paper" }, "eats" );

    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "Spock", "scissors" }, "smashes" );
    if( f == null ) f = StarTrekFightMusic( players, new String[]{ "Spock", "rock" }, "vaporizes" );

    return f;
  }

  private static Fight StarTrekFightMusic( String players[], String entities[], String action )
  {
    if( entities[ 0 ].equals( players[ 0 ] ) && entities[ 1 ].equals( players[ 1 ] ))
      return new Fight( new String[]{ players[ 0 ], action, players[ 1 ] }, ( byte )1 );
    if( entities[ 0 ].equals( players[ 1 ] ) && entities[ 1 ].equals( players[ 0 ] ))
      return new Fight( new String[]{ players[ 1 ], action, players[ 0 ] }, ( byte )2 );
    return null;
  }
}

Testing:

lizard,rock
Rock crushes lizard
Spock,rock
Spock vaporizes rock
paper,paper
Tie
rock,lizard
Rock crushes lizard
scissors,paper
Scissors cut paper
Player 1 wins!
Advertisements


No Responses Yet to “D : Rock-paper-scissors-lizard-Spock”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: