How to “Rock Paper Scissors” in Java


The challenge

Let’s play Rock Paper Scissors! You have to return which player won! In case of a draw return Draw!.

Examples:

rps('scissors','paper') // Player 1 won!
rps('scissors','rock') // Player 2 won!
rps('paper','paper') // Draw!

The rules of the game Rock Paper Scissors

The rules of the game Rock Paper Scissors is quite simple.

Rock beats scissors, scissors beats paper, and paper beats rock.

Below is an illustration showing the cyclical routes.

Test cases

import static org.junit.Assert.*;
import org.junit.Test;

public class RPSTest {
    
    @Test
    public void test1() {
        System.out.println("Fixed Tests Player 1 won!");
        assertEquals("Player 1 won!", RPS.rps("rock", "scissors"));
        assertEquals("Player 1 won!", RPS.rps("scissors", "paper"));
        assertEquals("Player 1 won!", RPS.rps("paper", "rock"));
    }
    @Test
    public void test2() {
        System.out.println("Fixed Tests Player 2 won!");
        assertEquals("Player 2 won!", RPS.rps("scissors", "rock"));
        assertEquals("Player 2 won!", RPS.rps("paper", "scissors"));
        assertEquals("Player 2 won!", RPS.rps("rock", "paper"));
    }
    @Test
    public void test3() {
        System.out.println("Fixed Tests Draw!");
        assertEquals("Draw!", RPS.rps("scissors", "scissors"));
        assertEquals("Draw!", RPS.rps("paper", "paper"));
        assertEquals("Draw!", RPS.rps("rock", "rock"));
    }
    
}

The solution in Java

Option 1 (the long way):

public class RPS {
  public static String rps(String p1, String p2) {
    // create some Strings to return
    String p1w = "Player 1 won!";
    String p2w = "Player 2 won!";
    String d = "Draw!";
    
    // return the draws
    if (p1.equals("rock") && p2.equals("rock")) return d;
    if (p1.equals("paper") && p2.equals("paper")) return d;
    if (p1.equals("scissors") && p2.equals("scissors")) return d;
    
    // return the rock vs
    if (p1.equals("rock") && p2.equals("paper")) return p2w;
    if (p1.equals("rock") && p2.equals("scissors")) return p1w;
    
    // return the paper vs
    if (p1.equals("paper") && p2.equals("rock")) return p1w;
    if (p1.equals("paper") && p2.equals("scissors")) return p2w;
    
    // return the scissors vs
    if (p1.equals("scissors") && p2.equals("rock")) return p2w;
    if (p1.equals("scissors") && p2.equals("paper")) return p1w;

    // finally return null if all else failed
    return null;
  }
}

Option 2 (quicker):

public class RPS {
  public static String rps(String p1, String p2) {
    if(p1 == p2) return "Draw!";
    int p = (p1 + p2).equals("scissorspaper") || (p1 + p2).equals("rockscissors") || (p1 + p2).equals("paperrock") ? 1 : 2;
    return "Player " + p + " won!";
  }
}

Option 3 (in a ternary):

public class RPS {
  public static String rps(String p1, String p2) {
    return p1.equals(p2) ? "Draw!" : "Player " + ("scissorspaper paperrock rockscissors".contains(p1+p2)?1:2) + " won!";
  }
}

Option 4 (with a HashMap):

import java.util.HashMap;
import java.util.Map;

public class RPS {
    public static String rps(String p1, String p2) {
        if (p1.equals(p2)) return "Draw!";
        
        Map<String, String> game = new HashMap<>();
        game.put("scissors", "paper");
        game.put("paper", "rock");
        game.put("rock", "scissors");

        String result = game.get(p1);
        if (result.equals(p2)) return "Player 1 won!";
        else                   return "Player 2 won!";
    }
}

Option 5 (checking in full string contains):

public class RPS {
  public static String rps(String p1, String p2) {
    if (p1 == p2) {
      return "Draw!";
    } else {
      String rules = "rockpaperscissorsrock";
      if (rules.contains(p1+p2)) {
        return "Player 2 won!";
      } else {
        return "Player 1 won!";
      }
    }
  }
}