Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I make a rock paper scissors program in onlineGDB

+23 votes
asked Dec 11, 2024 by Tyler Adams (350 points)
I need this before winter break. Please, and thank you.

5 Answers

+2 votes
answered Dec 11, 2024 by Ethan Tran (180 points)
import os, time
def Write(string):
    for l in string:
        print(l, end="", flush=True)
        time.sleep(0.05)
while True:
    os.system("clear")
    player1 = input("Player 1 Enter Name > ")
    player2 = input("Player 2 Enter Name > ")
    while True:
        os.system("clear")
        Write(f"{player1}'s Turn: (don't show player 2)")
        player1_turn = input("\n(s=scissors, r=rock, p=paper) > ").lower()
        if player1_turn == "s" or player1_turn == "r" or player1_turn == "p": break
    while True:
        os.system("clear")
        Write(f"{player2}'s Turn: (don't show player 1)")
        player2_turn = input("\n(s=scissors, r=rock, p=paper) > ").lower()
        if player2_turn == "s" or player2_turn == "r" or player2_turn == "p": break
    if player1_turn == player2_turn: print("Tie!")
    elif (player1_turn == "r" and player2_turn == "s") or (player1_turn == "s" and player2_turn == "p") or (player1_turn == "p" and player2_turn == "r"): print(f"{player1} Won!")
    else: print(f"{player2} Won!")
    input("Continue > ")
asked Dec 23, 2024 by EME2R673323100217 (210 points) Lap 4-1เงื่อนไข
+1 vote
answered Dec 12, 2024 by Peter Minarik (101,340 points)
0 votes
answered Dec 13, 2024 by Prince Tiwari (140 points)
0 votes
answered Dec 17, 2024 by James Degenhardt (150 points)
This is the program in c++ :

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

char getComputerMove()
{
    int move;
    
    srand(time(NULL));
    move = rand() % 3;

  
    if (move == 0) {
        return 'p';
    }
    else if (move == 1) {
        return 's';
    }
    return 'r';
}

// Function to return the result of the game
int getResults(char playerMove, char computerMove)
{
    // condition for draw
    if (playerMove == computerMove) {
        return 0;
    }

    // condition for win and loss according to game rule
    if (playerMove == 's' && computerMove == 'p') {
        return 1;
    }
    if (playerMove == 's' && computerMove == 'r') {
        return -1;
    }
    if (playerMove == 'p' && computerMove == 'r') {
        return 1;
    }
    if (playerMove == 'p' && computerMove == 's') {
        return -1;
    }
    if (playerMove == 'r' && computerMove == 'p') {
        return -1;
    }
    if (playerMove == 'r' && computerMove == 's') {
        return 1;
    }
    
    return 0;
}

// driver code
int main()
{

    char playerMove;

    cout << "\n\n\n\t\t\tWelcome to Stone Paper Scissor "
            "Game\n";

    cout << "\n\t\tEnter r for ROCK, p for PAPER, and s "
            "for SCISSOR\n\t\t\t\t\t";

    // input from the user
    while(1) {
        cin >> playerMove;
        if (playerMove == 'p' || playerMove == 'r' || playerMove == 's') {
            break;
        }
        else {
            cout << "\t\t\tInvalid Player Move!!! Please Try Again." << endl;
        }
    }
    
    

    // computer move
    char computerMove = getComputerMove();

    int result = getResults(playerMove, computerMove);

    // priting result based on who won the game
    if (result == 0) {
        cout << "\n\t\t\tGame Draw!\n";
    }
    else if (result == 1) {
        cout << "\n\t\t\tCongratulations! Player won the "
                "game!\n";
    }
    else {
        cout << "\n\t\t\tOh! Computer won the game!\n";
    }

    // showing both  moves
    cout << "\t\t\tYour Move: " << playerMove << endl;
    cout << "\t\t\tComputer's Move: " << computerMove << endl;

    return 0;
}
+1 vote
answered Dec 28, 2024 by PRIYANSHU DAS (160 points)

I Think So This Might Work!!!!

import java.util.*;
class RockPaperScissor
{
    public static void main (String args[])
    {
        Scanner sc = new Scanner (System.in);
        int n,m=0,c=0,r;
        char cc='R',mc;
        System.out.print("Enter The Limit Of Points:- ");
        n= sc.nextInt();
        System.out.println("Game Start:- ");
        System.out.println("********************************************************************************************************");
        System.out.println("Select 'R' for Rock");
        System.out.println("Select 'P' for Paper");
        System.out.println("Select 'S' for Scissor");
        while(n>0)
        {
            System.out.println("********************************************************************************************************);
            r=(int)(Math.random()*10);
            if(r>=0 && r<=3)
                cc='R';
            else if(r>=4 && r<=6)
                cc='P';
            else if(r>=7 && r<=9)
                cc='S';
            System.out.print("Your Choice:- ");
            mc=sc.next().charAt(0);
            System.out.println("Computer's Choice:- "+cc);
            if(mc==cc)
                continue;
            else if(mc!=cc)    
            {
                if(mc=='R' && cc=='P')
                    c++;
                else if(mc=='S' && cc=='R')
                    c++;
                else if(mc=='P' && cc=='S')
                    c++;
                else if(mc=='P' && cc=='R')
                    m++;
                else if(mc=='R' && cc=='S')
                    m++;
                else if(mc=='S' && cc=='P')
                    m++;
                n--;
            }
        }
        if(m>c)
        {
            System.out.println("Congratulations! You Are The Winner:-)");
            System.out.println("Your Score:- "+m);
            System.out.println("Computer's Score:- "+c);
        }
        else if(m<c)
        {
            System.out.println("Too Bad For You! You Lost:-(");
            System.out.println("Your Score:- "+m);
            System.out.println("Computer's Score:- "+c);
        }
        else if(m==c)
        {
            System.out.println("It's a Draw!:-|");
            System.out.println("Your Score:- "+m);
            System.out.println("Computer's Score:- "+c);
        }
        System.out.println("********************************************************************************************************************");
    }
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...