Stone Paper and Scissors Game Coding Problem

Problem Statement:


Stone Paper and Scissors Game Coding Problem: Tom and Jerry are engaged in a Stone-Paper, and Scissors game, vying for possession of a cake stored in the refrigerator. Your task is to determine the winner based on their chosen options. If Tom wins, return “Tom”; if Jerry wins, return “Jerry”; and if they choose the same option, return “Share it!”.

In this game, both players choose between Stone, Paper, and Scissors. The outcome is determined by the following rules: Stone beats Scissors, Scissors beat Paper, and Paper beats Stone.

Your objective is to create a program that takes the choices of both players as input and returns the winner according to the game rules.

Sample Input:
Player 1: "Stone"
Player 2: "Paper"
Sample Output:
Jerry
Explanation:
Since Paper beats Stone, Jerry emerges as the winner.

Stone Paper and Scissors Game | Use a 6-Step Strategy to Solve Problems:

  1. Understand the Problem:
    Comprehend the task requirements and the constraints given in the problem description.
  2. Design Test Data/Test Cases:
    Prepare a set of test cases with input values and the expected corresponding output.
  3. Derive the Solution:
    Develop a plan or pseudocode that outlines the steps to achieve the desired outcome.
  4. Test the Solution:
    Dry run the pseudo-code using the test data to verify its correctness.
  5. Write the Program/Code:
    Implement the solution using a programming language (e.g., Java).
  6. Test the Code:
    Thoroughly examine the code for syntax errors, runtime errors, and logical errors. Execute the code with the prepared test cases to ensure its accuracy.

The 6-step strategy to solve the given problem statement and provide code examples in C, C++, Java, JavaScript, and Python.

Step 1: Understand the Problem:


We have to determine the winner of a Stone-Paper-Scissor game based on the choices of two players. The rules of the game are provided, and we need to return the name of the winner or “Share it!” if it’s a tie.

Step 2: Design Test Data/Test Cases:

  • Input: “Stone”, “Paper”
    Output: Jerry
  • Input: “Scissor”, “Scissor”
    Output: Share it!
  • Input: “Paper”, “Stone”
    Output: Tom

Step 3: Derive the Solution:

We’ll compare the choices of both players based on the game rules to determine the winner or tie.

Step 4: Test the Solution:


We’ll apply the solution to the test cases to ensure it works as expected.

Step 5: Write the Program/Code:

Stone Paper and Scissors Game

C
#include <stdio.h>
#include <string.h>

const char* determineWinner(const char* player1, const char* player2) {
    if (strcmp(player1, player2) == 0)
        return "Share it!";
    else if ((strcmp(player1, "Stone") == 0 && strcmp(player2, "Scissor") == 0) ||
             (strcmp(player1, "Scissor") == 0 && strcmp(player2, "Paper") == 0) ||
             (strcmp(player1, "Paper") == 0 && strcmp(player2, "Stone") == 0))
        return "Tom";
    else
        return "Jerry";
}

int main() {
    const char* player1 = "Stone";
    const char* player2 = "Paper";
    printf("%s\n", determineWinner(player1, player2));
    return 0;
}
C

C++
#include <iostream>
#include <string>

std::string determineWinner(const std::string& player1, const std::string& player2) {
    if (player1 == player2)
        return "Share it!";
    else if ((player1 == "Stone" && player2 == "Scissor") ||
             (player1 == "Scissor" && player2 == "Paper") ||
             (player1 == "Paper" && player2 == "Stone"))
        return "Tom";
    else
        return "Jerry";
}

int main() {
    std::string player1 = "Stone";
    std::string player2 = "Paper";
    std::cout << determineWinner(player1, player2) << std::endl;
    return 0;
}
C++

Java
public class StonePaperScissorGame {
    public static String determineWinner(String player1, String player2) {
        if (player1.equals(player2))
            return "Share it!";
        else if ((player1.equals("Stone") && player2.equals("Scissor")) ||
                 (player1.equals("Scissor") && player2.equals("Paper")) ||
                 (player1.equals("Paper") && player2.equals("Stone")))
            return "Tom";
        else
            return "Jerry";
    }

    public static void main(String[] args) {
        String player1 = "Stone";
        String player2 = "Paper";
        System.out.println(determineWinner(player1, player2));
    }
}
Java

JavaScript
function determineWinner(player1, player2) {
    if (player1 === player2)
        return "Share it!";
    else if ((player1 === "Stone" && player2 === "Scissor") ||
             (player1 === "Scissor" && player2 === "Paper") ||
             (player1 === "Paper" && player2 === "Stone"))
        return "Tom";
    else
        return "Jerry";
}

const player1 = "Stone";
const player2 = "Paper";
console.log(determineWinner(player1, player2));
JavaScript

Python
def determine_winner(player1, player2):
    if player1 == player2:
        return "Share it!"
    elif (player1 == "Stone" and player2 == "Scissor") or \
         (player1 == "Scissor" and player2 == "Paper") or \
         (player1 == "Paper" and player2 == "Stone"):
        return "Tom"
    else:
        return "Jerry"

player1 = "Stone"
player2 = "Paper"
print(determine_winner(player1, player2))
Python

Step 6: Test the Code:


Execute the code using the provided test cases to ensure it produces the expected outputs.

Conclusion

By following this systematic strategy, we were able to successfully address the problem of determining the winner of the Stone-Paper-Scissor game based on the given player choices and game rules. The provided code examples in multiple programming languages should help you implement the solution in your preferred programming environment.



Leave a Comment