12 Replies - 463 Views - Last Post: 02 February 2012 - 02:48 AM Rate Topic: -----

Topic Sponsor:

#1 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Rock Paper scissors game

Posted 30 January 2012 - 11:59 PM

So I need to make a rock paper scissors program. I am fairly new to programming and I can't seem to start the thought process. Any suggestions?

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

int main() {

    // constant declarations
    const char ROCK = 'R,r';
	const char PAPER = 'P,p';
	const char SCISSORS = 'S,s';
	char playerInput;
    const string P1_WINS = "Player one wins!";
	const string P2_WINS = "Player two wins!";
    const string ROCK_BEATS_SCISSORS = "Rock beats scissors.";
	const string SCISSORS_BEATS_PAPER = "Scissors beats paper.";
	const string PAPER_BEATS_ROCK = "Paper beats rock";


	// print heading

	cout << "Welcome one and all to a round of Rock, Paper Scissors! (Enter P, R or S)." << endl;
	cout << "Player 1:";
		cin >> playerInput; 
	cout << "Player 2:";
		cin >> playerInput;

This post has been edited by no2pencil: 31 January 2012 - 12:21 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Rock Paper scissors game

#2 no2pencil  Icon User is online

  • 2 girls, 1 club
  • member icon

Reputation: 3061
  • View blog
  • Posts: 22,961
  • Joined: 10-May 07

Re: Rock Paper scissors game

Posted 31 January 2012 - 12:22 AM

I'm not sure why you are setting your constants that way. Just use ctype to check for upper/lower.

BTW, check over this tutorial that I wrote. Maybe it'll help you.
Was This Post Helpful? 0
  • +
  • -

#3 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: Rock Paper scissors game

Posted 31 January 2012 - 12:45 AM

It's kind of hard for me to follow. So I defined my declarations but I'm having trouble with the else if statements. I seem to run the program but it always prompts the same result regardless of the inputs. What am I doing wrong?

cout << "Welcome one and all to a round of Rock, Paper Scissors! (Enter P, R or S)." << endl;
	cout << "Player one:";
		cin >> player1input;
	cout << "Player two:";
		cin >> player2input;

	if ((player1input = ROCK) && (player2input = SCISSORS))
		cout << ROCK_BEATS_SCISSORS << P1_WINS;
	else ((player1input = PAPER) && (player2input = SCISSORS));
		cout << SCISSORS_BEATS_PAPER << P2_WINS;

Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 187
  • View blog
  • Posts: 505
  • Joined: 12-July 09

Re: Rock Paper scissors game

Posted 31 January 2012 - 07:03 AM

const char ROCK = 'R,r';

A char only stores one character. There can't be more than one character between the 's (except if it's an escape sequence like \n).
if ((player1input = ROCK) && (player2input = SCISSORS))

You use = to set variables and == to compare values.
Was This Post Helpful? 0
  • +
  • -

#5 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 122
  • Joined: 24-January 12

Re: Rock Paper scissors game

Posted 31 January 2012 - 02:00 PM

Quote

	if ((player1input = ROCK) && (player2input = SCISSORS))
		cout << ROCK_BEATS_SCISSORS << P1_WINS;
	else ((player1input = PAPER) && (player2input = SCISSORS));
		cout << SCISSORS_BEATS_PAPER << P2_WINS;



Try using the == operator isntead of =, = makes the left the same as the right while == does a comparison.
Was This Post Helpful? 0
  • +
  • -

#6 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: Rock Paper scissors game

Posted 01 February 2012 - 10:41 PM

everything works except when I type an uppercase it doesn't respond. How can i make it so that it takes lower case and uppercase? I know there's like a cin >> number-32;?

int main() {

    // constant declarations
    const char ROCK = 'r';
	const char PAPER = 'p';
	const char SCISSORS = 's';
    const string P1_WINS = "Player one wins!";
	const string P2_WINS = "Player two wins!";
	const string TIE = "It's a tie!";
    const string ROCK_BEATS_SCISSORS = "Rock beats scissors.";
	const string SCISSORS_BEATS_PAPER = "Scissors beats paper.";
	const string PAPER_BEATS_ROCK = "Paper beats rock.";

	//variable declarations
	char player1input;
	char player2input;

	//print heading
	cout << "Welcome one and all to a round of Rock, Paper Scissors! (Enter P, R or S)." << endl;
	cout << "Player one:";
		cin >> player1input;
	cout << "Player two:";
		cin >> player2input;

	//player1

	while ((player1input != ROCK) && (player1input != PAPER) && (player1input != SCISSORS));
	if (player1input == ROCK){
		player1input = ROCK;
	}
	else if (player1input == PAPER){
		player1input = PAPER;
	}
	else if (player1input == SCISSORS){
		player1input = SCISSORS;
	}

	//player2
	while  ((player2input != ROCK) && (player2input != PAPER) && (player2input != SCISSORS));
	if (player2input == ROCK){
		player2input = ROCK;
	}
	else if (player2input == PAPER){
		player2input = PAPER;
	}
	else if (player2input == SCISSORS){
		player2input = SCISSORS;
	}

	//outcome
int outcome (char player1input , char player2input);

	if ((player1input == ROCK) && (player2input == ROCK)){
		cout << TIE;
	}
	else if ((player1input == ROCK) && (player2input == PAPER)){
		cout << PAPER_BEATS_ROCK << P2_WINS;
	}
	else if ((player1input == ROCK) && (player2input == SCISSORS)){
		cout << ROCK_BEATS_SCISSORS << P1_WINS;
	}
	else if ((player1input == PAPER) && (player2input == ROCK)){
		cout << PAPER_BEATS_ROCK << P1_WINS;
	}
	else if ((player1input == PAPER) && (player2input == PAPER)){
		cout << TIE;
	}
	else if ((player1input == PAPER) && (player2input == SCISSORS)){
		cout << SCISSORS_BEATS_PAPER << P2_WINS;
	}
	else if (( player1input == SCISSORS) && (player2input == ROCK)){
		cout << ROCK_BEATS_SCISSORS << P2_WINS;
	}
	else if ((player1input == SCISSORS) && (player2input == PAPER)){
		cout << SCISSORS_BEATS_PAPER << P1_WINS;
	}
	else if ((player1input == SCISSORS) && (player2input == SCISSORS)){
		cout << TIE;
	}
	cout << endl;


    // Exit program.
    return 0;
}


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 02 February 2012 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#7 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 187
  • View blog
  • Posts: 505
  • Joined: 12-July 09

Re: Rock Paper scissors game

Posted 02 February 2012 - 12:39 AM

After the inputs you can do:
player1Input = tolower(player1Input);
player2Input = tolower(player2Input);

tolower() converts a character to lowercase if it is uppercase. It is defined in the header <cctype>.

Edit:
while ((player1input != ROCK) && (player1input != PAPER) && (player1input != SCISSORS));

Here you go into an infinite loop (which does nothing) if player 1's input is invalid. You do the same with player 2.

if (player1input == ROCK){
player1input = ROCK;
}
else if (player1input == PAPER){
player1input = PAPER;
}
else if (player1input == SCISSORS){
player1input = SCISSORS;
}

This does nothing. You are basically saying, "If a variable has some value, set it to that same value." You do the same with player 2.

int outcome (char player1input , char player2input);

You shouldn't declare functions in other functions. That function can be removed, because you never implement or call it.

This post has been edited by Hezekiah: 02 February 2012 - 12:51 AM

Was This Post Helpful? 1
  • +
  • -

#8 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:20 AM

so my program shown below can accept lower case and it will run properly. But when you put in the same letter in uppercase it won't run. What is the proper way to use the tolower function in this occasion?

int main() {

    // constant declarations
	const char ROCK = 'r';
	const char PAPER = 'p';
	const char SCISSORS = 's';
    const string P1_WINS = "Player one wins!";
	const string P2_WINS = "Player two wins!";
	const string TIE = "It's a tie!";
    const string ROCK_BEATS_SCISSORS = "Rock beats scissors.";
	const string SCISSORS_BEATS_PAPER = "Scissors beats paper.";
	const string PAPER_BEATS_ROCK = "Paper beats rock.";

	//variable declarations
	char player1input;
	char player2input;

	//print heading
	cout << "Welcome one and all to a round of Rock, Paper Scissors! (Enter p, r or s)." << endl;
	cout << "Player one:";
		cin >> player1input;
	cout << "Player two:";
		cin >> player2input;

	//player1

	while ((player1input != ROCK) && (player1input != PAPER) && (player1input != SCISSORS));
	if (player1input == ROCK){
		player1input = ROCK;
	}
	else if (player1input == PAPER){
		player1input = PAPER;
	}
	else if (player1input == SCISSORS){
		player1input = SCISSORS;
	}

	//player2
	while  ((player2input != ROCK) && (player2input != PAPER) && (player2input != SCISSORS));
	if (player2input == ROCK){
		player2input = ROCK;
	}
	else if (player2input == PAPER){
		player2input = PAPER;
	}
	else if (player2input == SCISSORS){
		player2input = SCISSORS;
	}

	//outcome
int outcome (char player1input , char player2input);

	if ((player1input == ROCK) && (player2input == ROCK)){
		cout << TIE;
	}
	else if ((player1input == ROCK) && (player2input == PAPER)){
		cout << PAPER_BEATS_ROCK << P2_WINS;
	}
	else if ((player1input == ROCK) && (player2input == SCISSORS)){
		cout << ROCK_BEATS_SCISSORS << P1_WINS;
	}
	else if ((player1input == PAPER) && (player2input == ROCK)){
		cout << PAPER_BEATS_ROCK << P1_WINS;
	}
	else if ((player1input == PAPER) && (player2input == PAPER)){
		cout << TIE;
	}
	else if ((player1input == PAPER) && (player2input == SCISSORS)){
		cout << SCISSORS_BEATS_PAPER << P2_WINS;
	}
	else if (( player1input == SCISSORS) && (player2input == ROCK)){
		cout << ROCK_BEATS_SCISSORS << P2_WINS;
	}
	else if ((player1input == SCISSORS) && (player2input == PAPER)){
		cout << SCISSORS_BEATS_PAPER << P1_WINS;
	}
	else if ((player1input == SCISSORS) && (player2input == SCISSORS)){
		cout << TIE;
	}
	cout << endl;


    // Exit program.
    return 0;
}

This post has been edited by Atli: 02 February 2012 - 02:38 AM
Reason for edit:: Added [code] tags.

Was This Post Helpful? 0
  • +
  • -

#9 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:27 AM

#include<cctype>

tolower(int c); // use char array of a string
toupper(int c); // same thing


an alternative would be to create a menu inside a loop and have it list a bunch of options that can be accessed through a single number or letter.

This post has been edited by Toadill: 02 February 2012 - 02:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:31 AM

how do I use char array of a string?
Was This Post Helpful? 0
  • +
  • -

#11 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:39 AM

Okay I see now that you have r, s, p as the menu input and it is off char data type already. All you have to do is include the header i gave you and use the function by putting the user input into it the return should be in the proper format.

example

char convertedResponse;
cout << "enter r, s, or p"
cin >> response;
convertedResponse = tolower(response);



or you could just

cout << "enter r, s, or p"
cin >> response;
response = tolower(response);



This post has been edited by Toadill: 02 February 2012 - 02:42 AM

Was This Post Helpful? 0
  • +
  • -

#12 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:47 AM

Ok, I did this and my program is still not working. Is it stuck on some infinite loop or what;s the deal? I can't seem to figure it out.

Never mind I got it.... Thanks a million
Was This Post Helpful? 0
  • +
  • -

#13 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: Rock Paper scissors game

Posted 02 February 2012 - 02:48 AM

No problem :bigsmile:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1