0% found this document useful (0 votes)
12 views5 pages

Rock Paper Scissors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Rock Paper Scissors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cctype>

using namespace std;

// returns either 'r', 'p', or 's' for rock, paper or scissors

char get_computers_choice() {

// generate random number based on time

srand(time(NULL));

int random_number = (rand() % 3) + 1;

char computers_choice;

switch(random_number) {

case 1:

computers_choice = 'r';

break;

case 2:

computers_choice = 'p';

break;
case 3:

computers_choice = 's';

break;

return computers_choice;

// take user input and return it

// function to get user input

char get_user_input() {

char user_input;

cout << "Enter Your Choice (r for rock, p for paper, s for scissors): ";

cin >> user_input;

user_input = tolower(user_input);

return user_input;

// return either 'w', 'l' or 'd' for win, loss and draw

char get_result(char user_pick, char computer_pick) {


// condition for user to draw

if (computer_pick == user_pick) {

return 'd';

// condition for user to win

else if (user_pick == 'p' && computer_pick == 'r') {

return 'w';

else if (user_pick == 'r' && computer_pick == 's') {

return 'w';

else if (user_pick == 's' && computer_pick == 'p') {

return 'w';

// all other conditions result in user losing

else {

return 'l';

int main() {

// get computer choice


char computer_pick = get_computers_choice();

char user_pick = get_user_input();

char result = get_result(user_pick, computer_pick);

// print computer_pick and user_pick

cout << "Computer's pick: " << computer_pick << endl;

cout << "Your pick: " << user_pick << endl;

// print the result

switch(result) {

case 'w':

cout << "You won";

break;

case 'l':

cout << "You lost";

break;

case 'd':

cout << "Draw";

break;

}
return 0;

You might also like