Public Group

# My first game!

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I just made this Rock, Paper, Scissors game and I was looking for some advice on style and choice of program flow such as using ifs over and over agian and if there is a better way to do random integers.
#include &lt;iostream&gt;
#include &lt;ctime&gt;
using namespace std;

int main()
{
srand((unsigned)time(0));

unsigned short int playerInput;
unsigned short int computerInput;

cout &lt;&lt; "Welcome to the rock, paper, scissors game thing.\n";
cout &lt;&lt; "(1) Rock\n";
cout &lt;&lt; "(2) Paper\n";
cout &lt;&lt; "(3) Scissors\n";
cin &gt;&gt; playerInput;
computerInput = (rand() & 2) + 1;
if (computerInput == 1)
{
cout &lt;&lt; "Computer used Rock!\n";
}
if (computerInput == 2)
{
cout &lt;&lt; "Computer used Paper!\n";
}
if (computerInput == 3)
{
cout &lt;&lt; "Computer used Scissors!\n";
}

if (playerInput == 1 && computerInput == 1)
{
cout &lt;&lt; "I think those rocks could be put to a better use, tie\n";
}
if (playerInput == 1 && computerInput == 2)
{
cout &lt;&lt; "Your army of rocks were halted by a wave of paper shock troops you suffer major losses.\n";
}
if (playerInput == 1 && computerInput == 3)
{
cout &lt;&lt; "The Scissorians can't stop the rock, you win!\n";
}
if (playerInput == 2 && computerInput == 1)
{
cout &lt;&lt; "Your Paper engulfed the rock, victory.\n";
}
if (playerInput == 2 && computerInput == 2)
{
cout &lt;&lt; "You watch the hot paper on paper action unfold(harr harr) but it ends in a stalemate\n";
}
if (playerInput == 2 && computerInput == 3)
{
cout &lt;&lt; "Paper carnage, you lose\n";
}
if (playerInput == 3 && computerInput == 1)
{
cout &lt;&lt; "Your scissors have seen it's last snip, you lose\n";
}
if (playerInput == 3 && computerInput == 2)
{
cout &lt;&lt; "You turn the paper into swiss cheese, I guess that works?\n";
}
if (playerInput == 3 && computerInput == 3)
{
cout &lt;&lt; "Scissoring?";
}

return 0;
}


EDIT: How do I get my code into one of those boxes? [Edited by - Avion on June 16, 2008 8:11:36 PM]

##### Share on other sites
Use "source" tags for the box. Eg: [ source]mah beautiful code[ /source] (but remove the spaces after the open brackets).

Not too much code to go on there, but I can say a couple things. First, there's no major problems with it. Using "using namespace std" is sometimes frowned upon. I don't think it's a big issue, so long as you are aware of the drawbacks (that it takes the *entire* std namespace into scope, possibly making naming collisions) and know how to not use it (append "std::" to things in it). A good habit to get into is to declare variables at the last possible second. If a variable is declared at the top of the function, but isn't used until the very end, simply move the declaration down to the bottom of the function. This has various benefits, from making sure that the variable isn't used before its properly initialized to making it easier to read since the type of the variable is near where its used.

Other than that, congrats on your first game! :) I give you two challenges:
1) Making the game run repeatedly, so that you play more than once and that it keeps track of the score.
2) Notice how you have to repeat a lot of "if( playerInput == X and computerInput == Y )" code? Try refactoring that out so that you don't repeat code like that. Perhaps you could create a two-dimensional array that has the results stored in them. Then, you'd index that array with the player and computer input to obtain the result.

Note that #2 is more of a challenge than something that would actually make your code better. It's just to get you thinking! If you don't know it already, learn loops for number 1 and arrays (or lists) for number 2.

##### Share on other sites
You use the [source][/source] tags. You may want to look into using some switch statements (just for practice). You can also use nested if statements.

if (playerInput == 1){    if (computerInput == 1)    {        cout << "I think those rocks could be put to a better use, tie\n";    }    else if (computerInput == 2)    {        cout << "Your army of rocks were halted by a wave of paper shock troops you suffer major losses.\n";    }    else    {    cout << "The Scissorians can't stop the rock, you win!\n";    }}

You don't use any else statements, but your code could actually use them if you wanted. Ezbez's suggestion of making the game run repeatedly is a great idea. You could also use some binary/bitwise operators which could virtually eliminate your if statements (at least a lot). That's a pretty tough challenge (and indeed would add more complexity to the game that it really needs), but if you're feeling like it you could try it. Your code is clean though, which is a very important thing! Using some functions might be a good idea also.

Blasted! Beat by Ezbez haha.

[Edited by - MikeTacular on June 16, 2008 7:00:57 PM]

##### Share on other sites
I haven't been using C++ very long, so I don't know if the use of switch is frowned upon or is slower, but I know it makes code look nicer.

computerInput = (rand() & 2) + 1;switch (computerInput){case 1:        cout << "Computer used Rock!\n";        break;case 2:        cout << "Computer used Paper!\n";        break;case 3:        cout << "Computer used Scissors!\n";        break;default:        cout << "We don't know what the computer chose.\n";        break;}

##### Share on other sites
The computer currently can never choose paper. Can you see why?

##### Share on other sites

1) Don't use short unless you need to, and you don't need to.

2) That's not how you correctly use rand, linky.

3) Use a switch to determine the choice the computer made.

4) Define some constants instead of just 1, 2 and 3 - i.e. give them names

ex:
enum { rock = 1, paper = 2, scissors = 3 };

5) When you're faced with lots of conditions and only one of them will ever evaluate to true, so no need to evaluate them all, then you should use 'else if' clauses

ex:
if (first_condition)
{
}
else if (second_condition)
{
}
else
{
}

6) You can replace all those conditionals (from point 5, above) which choose the phrase with a two-dimensional array of phrases and simply use the two inputs to index into it:

const char * choosePhrase(unsigned int playerChoice, unsigned int computerChoice){    const char * phrases[3][3] = { {                                     "I think those rocks could be put to a better use, tie.",                                     "Your army of rocks were halted by a wave of paper shock troops you suffer major losses.",                                     "The Scissorians can't stop the rock, you win!"                                   }, {                                     "Your Paper engulfed the rock, victory.",                                     "You watch the hot paper on paper action unfold(harr harr) but it ends in a stalemate.",                                     "Paper carnage, you lose!"                                   }, {                                     "Your scissors have seen its last snip, you lose!",                                     "You turn the paper into swiss cheese, I guess that works?",                                     "Scissoring?"                                   }                                 };    return phrases[playerChoice - 1][computerChoice - 1];}

And then actually in-place of all those conditionals within main:

std::cout << choosePhrase(playerInput, computerInput) << std::endl;

7) Did you really have to apply using namespace std? Is it too much effort to prefix cout and cin with std::? [smile]

8) There's no need to return 0 at the end of main. If you leave it out then the compiler automagically adds it for you at compile time. You only need a return statement in main if you're going to return a non-zero value. Leaving it in does no harm, but it serves no purpose.

##### Share on other sites
Quote:
 Original post by ZahlmanThe computer currently can never choose paper. Can you see why?

I'm sure it has to do with the line
computerInput = (rand() & 2) + 1;
but I don't know how to make a random or at least pseudorandom number 1-3.

EDIT: whoops I was using & and not %

##### Share on other sites
Nor can he (computer) ever use scissors. It has to deal with one character in this following line:

rand() % 2 + 1

Only one character is wrong, but that wrong character never lets the result reach three. Think about it, think, think about it (Fligh of the Conchords reference).

##### Share on other sites
The modulos operator returns any value between 0 and n-1, where n is equal to the number to the right of the operator.

so rand() % 2 returns 0 or 1.

##### Share on other sites
Quote:
 The modulos operator returns any value between 0 and n-1, where n is equal to the number to the right of the operator.
You might want to reword that statement.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007627
• ### Who's Online (See full list)

There are no registered users currently online

×