HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Making my Rock, Paper, Scissors game shorter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scissorspapergamerockmakingshorter

Problem

I am looking for advice on making the following code shorter. Any help is appreciated.

public String determineWinner(String winner) {
    winner = "";
    if (playChoice.equals(compChoice))
        winner = "The result is a tie.";
    else if (compChoice.equals("R"))
    if (playChoice.equals("S") || playChoice.equals("s"))
        winner = "Computer wins because Rock beats Scissors.";
    else
        winner = "Player wins because Paper beats Rock";
    else if (compChoice.equals("S"))
    if (playChoice.equals("P") || playChoice.equals("p"))
        winner = "Computer wins because Scissors beats Paper.";
    else
        winner = "Player wins because Rock beats Scissors";
    else 
    if (playChoice.equals("R") || playChoice.equals("r"))
        winner = "Computer wins because Paper beats Rock.";
    else
        winner = "Player wins because Scissors beats Paper.";
    return winner;
}

Solution

You code looks unclear from point of view of parameter usage and ignoring case difference is case of "The result is a tie.", so ...

Try this :).

public class testik {
    // No throws clause here
    public static void main(String[] args) {
        // Paper < Rock < Scissors < Paper
        System.out.println("Comp:S vs User:R = " + determineWinnerOtherWay("S","R")); //user win
        System.out.println("Comp:S vs User:P = " + determineWinnerOtherWay("S","P")); // com win

        System.out.println("Comp:P vs User:R = " + determineWinnerOtherWay("P","R")); //com win
        System.out.println("Comp:P vs User:S = " + determineWinnerOtherWay("P","S")); // user win

        System.out.println("Comp:R vs User:S = " + determineWinnerOtherWay("R","S")); //com win
        System.out.println("Comp:R vs User:P = " + determineWinnerOtherWay("R","P")); // user win
    }

    public static String code2Title(String code){
        if ("R".equals(code)) {
            return "Rock";
        } else if ("P".equals(code)) {
            return "Paper";
        } else {
            return "Scissors";
        }
    }

    public static String determineWinnerOtherWay(String compChoice, String playChoice) {
        String winner = "";
        String playerChoiceN = playChoice.toUpperCase();
        if (playChoice.equals(compChoice))
            winner = "The result is a tie.";
        int diff = compChoice.charAt(0) - playerChoiceN.charAt(0);
        if ( diff == 1 || diff == 2 || diff ==-3) {
            winner = "User win bacause:"+code2Title(playerChoiceN)+" wins " + code2Title(compChoice);
        } else {
            winner = "Computer wins because:"+code2Title(compChoice)+" wins " + code2Title(playerChoiceN);
        }
        return winner;
    }

}

Code Snippets

public class testik {
    // No throws clause here
    public static void main(String[] args) {
        // Paper < Rock < Scissors < Paper
        System.out.println("Comp:S vs User:R = " + determineWinnerOtherWay("S","R")); //user win
        System.out.println("Comp:S vs User:P = " + determineWinnerOtherWay("S","P")); // com win

        System.out.println("Comp:P vs User:R = " + determineWinnerOtherWay("P","R")); //com win
        System.out.println("Comp:P vs User:S = " + determineWinnerOtherWay("P","S")); // user win

        System.out.println("Comp:R vs User:S = " + determineWinnerOtherWay("R","S")); //com win
        System.out.println("Comp:R vs User:P = " + determineWinnerOtherWay("R","P")); // user win
    }

    public static String code2Title(String code){
        if ("R".equals(code)) {
            return "Rock";
        } else if ("P".equals(code)) {
            return "Paper";
        } else {
            return "Scissors";
        }
    }

    public static String determineWinnerOtherWay(String compChoice, String playChoice) {
        String winner = "";
        String playerChoiceN = playChoice.toUpperCase();
        if (playChoice.equals(compChoice))
            winner = "The result is a tie.";
        int diff = compChoice.charAt(0) - playerChoiceN.charAt(0);
        if ( diff == 1 || diff == 2 || diff ==-3) {
            winner = "User win bacause:"+code2Title(playerChoiceN)+" wins " + code2Title(compChoice);
        } else {
            winner = "Computer wins because:"+code2Title(compChoice)+" wins " + code2Title(playerChoiceN);
        }
        return winner;
    }

}

Context

StackExchange Code Review Q#19513, answer score: 2

Revisions (0)

No revisions yet.