Categories > Coding > C++ >

rock paper scissor

Posts: 59

Threads: 12

Joined: Feb, 2020

Reputation: 2

Posted

rock paper scissor

rock paper scissor

rock paper scissor

 

#include <iostream>
#include <ctime>


using namespace std;

char get_user_input();
char get_computer_choice();
void choice(char l);
void winner(char plr, char comp);

int main() {
	char plr;
	char comp;
	bool x = true;
	
	do {
		plr = get_user_input();
		printf("Your Choice Is: ", '\n');
		choice(plr);

		comp = get_computer_choice();
		printf("Computer Choice Is: ", '\n');
		choice(comp);

		winner(plr, comp);
	} while (x == true);
}

char get_user_input() {
	char player;
	cout << "Rock - Paper - Scissor\n";
	do {
		cout << "'r' for rock, 's' for scissor, 'p' for paper: ";
		cin >> player;
	} while (player != 'r' && player != 's' && player != 'p');
	return player;
}

char get_computer_choice() {
	int computer = (rand() % 3) + 1;

	switch (computer)
	{
	case 1:
		return 'r';

	case 2:
		return 's';

	case 3:
		return 'p';

	}
}

void choice(char l) {
	
	switch (l)
	{
	case 'r':
		cout << "Rock\n";
		break;
	case 's':
		cout << "Scissor\n";
		break;
	case 'p':
		cout << "Paper\n";
		break;
	}

}

void winner(char plr, char comp) {
	//i know i could have used switch but i used if statments so ok
	if (plr == 's' && comp == 'r') {
		cout << "Computer Won\n";
	}
	else if (plr == 'r' && comp == 's')
	{
		cout << "You Won\n";
	}
	else if (plr == 'p' && comp == 's')
	{
		cout << "Computer Won\n";
	}
	else if (plr == 's' && comp == 'p')
	{
		cout << "You Won\n";
	}
	else if (plr == 'p' && comp == 'r')
	{
		cout << "You Won\n";
	}
	else if (plr == 'r' && comp == 'p')
	{
		cout << "You Won\n";
	}
	else if (plr == 'p' && comp == 'p')
	{
		cout << "Tie\n";
	}
	else if (plr == 'r' && comp == 'r')
	{
		cout << "Tie\n";
	}
	else if (plr == 's' && comp == 's')
	{
		cout << "Tie\n";
	}
}
  • 0

want something? add me on disccord !Spl||HasH!#7562

Posts: 360

Threads: 36

Joined: May, 2022

Reputation: -2

Replied

kind of sloppy code but its ok

  • 0

Exploits i own:

Script-Ware (Down rn) Electron (UWP) Shadow (UWP)

i dont use verm that much so dont contact me there

Posts: 70

Threads: 6

Joined: Aug, 2022

Reputation: 4

Replied

You could just do: else if (plr == comp); for tie detection but ok

  • 0

Posts: 1

Threads: 0

Joined: Mar, 2023

Reputation: 0

Replied

Jesus what the hell is that code my man?

  • 0

Posts: 1996

Threads: 198

Joined: Apr, 2021

Reputation: 13

Replied

0/10 because 

using namespace std;
  • 0

Random quote here...

Posts: 522

Threads: 41

Joined: May, 2020

Reputation: 4

Replied

Here you go instead of whatever that is 

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    srand(time(NULL)); // Initialize random seed

    // Display game instructions
    cout << "Welcome to Rock Paper Scissors!" << endl;
    cout << "Choose your move: (r)ock, (p)aper, or (s)cissors." << endl;

    // Get player's move
    char playerMove;
    cout << "Your move: ";
    cin >> playerMove;

    // Generate computer's move
    char computerMove;
    int randomNum = rand() % 3;
    if (randomNum == 0) {
        computerMove = 'r';
    } else if (randomNum == 1) {
        computerMove = 'p';
    } else {
        computerMove = 's';
    }

    // Determine winner
    if (playerMove == computerMove) {
        cout << "It's a tie!" << endl;
    } else if ((playerMove == 'r' && computerMove == 's') ||
               (playerMove == 'p' && computerMove == 'r') ||
               (playerMove == 's' && computerMove == 'p')) {
        cout << "You win!" << endl;
    } else {
        cout << "Computer wins!" << endl;
    }

    return 0;
}
  • 0

nltr | Xaml & C# Developer Former Fluxus Administrator
------------------------------------------------------------------------------------
💜 Developer of Kronos ðŸ’œ

Senior Dev of Orbit

Discord

Posts: 1996

Threads: 198

Joined: Apr, 2021

Reputation: 13

Replied

@ItsNitro, here I fixed the code for you

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main()
{
    srand(time(NULL)); // Initialize random seed

    // Display game instructions
    std::cout << "Welcome to Rock Paper Scissors!" << std::endl;
    std::cout << "Choose your move: (r)ock, (p)aper, or (s)cissors." << std::endl;

    // Get player's move
    char playerMove;
    std::cout << "Your move: ";
    std::cin >> playerMove;

    // Generate computer's move
    char computerMove;
    int randomNum = rand() % 3;
    if (randomNum == 0) {
        computerMove = 'r';
    } else if (randomNum == 1) {
        computerMove = 'p';
    } else {
        computerMove = 's';
    }

    // Determine winner
    if (playerMove == computerMove) {
        std::cout << "It's a tie!" << std::endl;
    } else if ((playerMove == 'r' && computerMove == 's') ||
               (playerMove == 'p' && computerMove == 'r') ||
               (playerMove == 's' && computerMove == 'p')) {
        std::cout << "You win!" << std::endl;
    } else {
        std::cout << "Computer wins!" << std::endl;
    }

    return 0;
}
  • 0

Random quote here...

Posts: 522

Threads: 41

Joined: May, 2020

Reputation: 4

Replied

@VoidableMethod, hmmmm

using namespace std;
  • 0

nltr | Xaml & C# Developer Former Fluxus Administrator
------------------------------------------------------------------------------------
💜 Developer of Kronos ðŸ’œ

Senior Dev of Orbit

Discord

Posts: 1996

Threads: 198

Joined: Apr, 2021

Reputation: 13

Replied

@ItsNitro, no dont use that

  • 0

Random quote here...

Posts: 13

Threads: 8

Joined: Mar, 2023

Reputation: 0

Replied

bro literally God level codes 

  • 0

Thank you

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )