My first game!

Started by
9 comments, last by Sanctux 15 years, 10 months ago
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 <iostream>
#include <ctime>
using namespace std;

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

	unsigned short int playerInput;
	unsigned short int computerInput;

	cout << "Welcome to the rock, paper, scissors game thing.\n";
	cout << "Please enter a number coresponding with your choice.\n";
	cout << "(1) Rock\n";
	cout << "(2) Paper\n";
	cout << "(3) Scissors\n";
	cin >> playerInput;
	computerInput = (rand() & 2) + 1;
	if (computerInput == 1)
	{
		cout << "Computer used Rock!\n";
	}
	if (computerInput == 2)
	{
		cout << "Computer used Paper!\n";
	}
	if (computerInput == 3)
	{
		cout << "Computer used Scissors!\n";
	}
	
	
	
	if (playerInput == 1 && computerInput == 1)
	{
		cout << "I think those rocks could be put to a better use, tie\n";
	}
	if (playerInput == 1 && computerInput == 2)
	{
		cout << "Your army of rocks were halted by a wave of paper shock troops you suffer major losses.\n";
	}
	if (playerInput == 1 && computerInput == 3)
	{
		cout << "The Scissorians can't stop the rock, you win!\n";
	}
	if (playerInput == 2 && computerInput == 1)
	{
		cout << "Your Paper engulfed the rock, victory.\n";
	}
	if (playerInput == 2 && computerInput == 2)
	{
		cout << "You watch the hot paper on paper action unfold(harr harr) but it ends in a stalemate\n";
	}
	if (playerInput == 2 && computerInput == 3)
	{
		cout << "Paper carnage, you lose\n";
	}
	if (playerInput == 3 && computerInput == 1)
	{
		cout << "Your scissors have seen it's last snip, you lose\n";
	}
	if (playerInput == 3 && computerInput == 2)
	{
		cout << "You turn the paper into swiss cheese, I guess that works?\n";
	}
	if (playerInput == 3 && computerInput == 3)
	{
		cout << "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]
Advertisement
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.
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.

[edit]

Blasted! Beat by Ezbez haha.

[Edited by - MikeTacular on June 16, 2008 7:00:57 PM]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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;}
MSN Contact: zachmeyer@movie-boards.com
The computer currently can never choose paper. Can you see why?
Congratulations on your game, some comments...

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.
Quote:Original post by Zahlman
The 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 %
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).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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.
-A.K.A SteveGravity is unreliable-DTD
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.

This topic is closed to new replies.

Advertisement