Jump to content
  • Advertisement
Sign in to follow this  
Avion

My first game!

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

If you intended to correct an error in the post then please contact us.

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 <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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

[edit]

Blasted! Beat by Ezbez haha.

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 %

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!