For the record I am completely green in C++ and any other programming language for that matter, so if it's possible I'd be happy if you can answer in a way that I can understand :)
I have been working on a simple rock, paper, scissors game recently where I have 3 basic functions, one for the user, one for the bot, and one that choose which one wins the game.
I know using system("cls") isn't the best way to go, but I'm not planning on using this outside Windows.
The final function results() need to use the variables x and brand from the two previous functions, however I can't seem to find a way to do this. And where people explain it anywhere else, they explain it too advanced for me. Remeber that I don't need to change any of them, I simply have to compare them to determine the winner.
I'll show you my code here so you can see what you are dealing with. Please give me comments on other things I can improve here.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int bgame(), ugame(), results();
int main()
{
srand(time(NULL));
cout<<"Welcome to RPS!\n" <<"You know what to do.\n" <<"\n[ENTER]";
cin.ignore();
system("cls");
ugame();
return 0;
}
int ugame()
{
int x;
cout<<"Type a number from 1-3: ";
cin>> x;
cin.ignore();
if ( x == 1 )
{
cout<<"\nYou chose rock!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 2)
{
cout<<"\nYou chose paper!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 3 )
{
cout<<"\nYou chose scissors!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else
{
cout<<"\nTry again.\n" <<"\n[ENTER]";
cin.ignore();
ugame();
system("cls");
}
return 0;
}
int bgame()
{
int brand = rand()>>4;
system("cls");
cout<<"The bot will now choose an item.\n" <<"\n" <<"[ENTER]\n";
cin.ignore();
brand = rand() % 3 + 1;
if ( brand == 1)
{
cout<<"\nBot chose rock!";
cin.ignore();
results();
}
else if ( brand == 2 )
{
cout<<"\nBot chose paper!";
cin.ignore();
results();
}
else if ( brand == 3 )
{
cout<<"\nBot chose scissors!";
cin.ignore();
results();
}
else
{
cout<<"\nError.";
cin.ignore();
bgame();
system("cls");
}
return 0;
}
int results()
{
}
you can send "parameters" to a function. That's create a copy of your variable and you can use it in another function.
You need to specifies it with your function name (that's named prototype) like this :
int results(int x, int brand)
You put a type name and a variable name. For example this function will take 2 int as parameters.
int results(int x, int brand)
{
// do something with your variables
}
And when you call the function you type:
results(x, brand);
If you want, this link explains it with images and examples and more details: http://www.cplusplus.com/doc/tutorial/functions/
First, learn how to use functions with parameters.
After that, you should be able to do what you want.
As suggested, you only have to do something like this :
declaration :
int bgame(int x){
... what bgame do ...
}
and in ugame :
int ugame(){
int x;
...
cin >> x;
...
bgame(x);
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With