GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Oct-2009, 15:55
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Functions, loops, counters, help please.


CPP / C++ / C Code:
/*
1=rock
2=paper
3-scissors
*/

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





//Shows the welcome msg and instructions on how to play the game;
 int   winner (int userChoice , int compChoice);
 void  showWelcome();
 int   showMenu();
 
 
int main()
{
    srand((unsigned)time(0));

	int compChoice = (rand()%3)+1;
	int userChoice;
	int num;
	
	
	
	showWelcome();
	userChoice = showMenu();
	
	system ("pause");
	return 0;
}	
	// Formatting comands;
   //cout << fixed << showpoint << setprecision(2);
	
	
		
	
  //This is the void that shows the welcome msg and instructions on game play.

  void showWelcome()
{
       cout << "This is the classic game of Rock, Paper, Scissors\n";
       cout << "Select your choice from the menu and see who wins!\n";
}



//Validation check and cout choice statements;

  

 
 
int showMenu()
{ 
  int num; 
 cout << "\n1. Rock.\n";       
 cout << "2. Paper.\n" ;     
 cout << "3. Scissors.\n" ;  
 cout << "4. Exit.\n" ;      
 
 cin >> num;
 
return num;
}


if (num < 1 || num > 4)

{
      cout << "Please enter 1, 2, 3, or 4:";
      cin >> num;
}


	
	if(userChoice == 1)
	{
                  if (compChoice == 1)
                  cout << "It's a tie! \n\n.";
                  else if (compChoice == 2)
                  cout << "Paper smothers Rock! Computer wins\n\n!";
                  else if (compChoice == 3)
                  cout << "Rock Crushes Scissors! You Win!";
                  
     }             
     
     if (userChoice == 2)
     {
                    if (compChoice == 1)
                    cout <<"Paper Smothers Rock! You Win!\n\n" ;
                    else if (compChoice == 2)
                    cout <<"It's a Tie!\n\n";
                    else if (compChoice == 3)
                    cout <<"Scissors slices paper! Computer Wins!\n\n";
     }
     
     if (userChoice == 3)
     {
                    if (compChoice == 1)
                    cout <<"Rock Crushes Scissors! Computer Wins! \n\n";
                    else if (compChoice == 2)
                    cout <<"Scissors Slices Paper! You Win!\n\n";
                    else if (compChoice == 3)
                    cout <<"It's a Tie!\n\n";
     }
     
     system ("pause");
     return (showMenu);



Okay, thanks for your time to begin with. I am currently trying to make a Rock paper scissors program, that will loop through until told to exit (4), will keep a win counter (which I realize that I do not have included yet). I also have to validate the user input. I thought a do while might work but I am not positive. I know I am currently missing something simple but can not come up with the solution.

Again thanks for any and all help.
  #2  
Old 22-Oct-2009, 17:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Functions, loops, counters, help please.


Quote:
Originally Posted by Score_Reaper
...I am currently trying to make a Rock paper scissors program,...

So: What happened when you tried to compile? Didn't work did it? Your executable statements must all reside inside function blocks. The blocks can not be nested. That is, each function block must be complete and can not contain other function blocks. I hate to repeat myself, but all of your executable code must reside in a function block.

You always have a main() function, and you have a good start for a couple of other functions that can be useful.

Here's my suggestion:

First, ya gotta have a plan! Really. I strongly recommend that before you even think of writing code or figuring out what kind of loop you need, make a plan and write it down!

Scribble out a few lines that show how the program will play some games by getting user input and computer input values.

One plan might go something like the following.

You have a main program that consists of some kind of loop that gets the user play and the machine's play. It calculates the winner and increments the counter for the winner. The loop terminates when the user says so.

So, here's a plan:

1. Make a function that gets the user's choice (return an integer value 1, 2, 3, 4, where the values represent Rock, Paper, Scissors, and Exit).

2. Make a function that gets the machine's choice (return an integer value 1, 2, or 3 where the values represent Rock, Paper, and Scissors).

3. Make a function that takes user choice and machine choice as inputs and returns a value of 1 if the user wins and a value of 2 if the machine wins. (Or whatever values you would like: maybe a 0 for machine win and 1 for user win. Anything you want. See Footnote)

You already have a couple of good functions for welcoming the user and showing the user what choices are possible.

Here's an example of how you might make a main() function that plays the game along the lines of the above plan:

Code:
Example 1 ShowWelcome() Loop Forever ShowMenu() UserChoice = GetUserChoice() If UserChoice == Exit Then Break out of the loop Else MachineChoice = GetMachineChoice() Compute and announce the winner, based on UserChoice and MachineChoice Increment the winning player's counter. End If End Loop Show the total winning games for each player and bid a fond adieu.

The "Loop Forever" thing can be simply
CPP / C++ / C Code:
    while(1) {
.
.
.
   /* 
      Somewhere in here there is a "break" statement that
      will terminate the loop when the user enters the code
      for "Exit."
   */
.
.
.

    }

Some programmers don't like "break" statements, so they might prefer something like the following. In particular, experienced C and C++ programmers like to show off the feature of the language that lets statements do more than one thing: The while condition gets the user input and uses that value to decide whether to go into the loop:
Code:
Example 2 ShowWelcome() ShowMenu() While ((UserChoice = GetUserChoice()) != Exit) Loop MachineChoice = GetMachineChoice() Compute and announce the winner, based on UserChoice and MachineChoice ShowMenu End Loop Show the total winning games for each player, and bid a fond adieu.

Now, your plan doesn't have to be like my suggestions. For example my so-called "ShowMenu()" function could be called by "GetUserInput()" rather than by a separate call from main() since they always go together. I would never name the function "ShowMenu" if, in fact its purpose was to show the menu and to get the user input. I might very well make my function "GetUserInput" show the menu (probably by coding the menu inside the GetUserInput function rather than calling a void function that merely prints out the choices. Unless, that is, I wanted to internationalize the game by having a selection of "ShowMenu" functions for various languages that I could compile into the program. Anyhow, I think that the important thing is to have a plan in your head (and on paper) before the code comes pouring out.


Regards,

Dave

Footnote: When it comes to making and naming functions, try to create something that makes sense to you and something that is easy to remember for future programmers who might want to debug and/or enhance the code.

For example, I might create the grading function like this:
CPP / C++ / C Code:
int UserWins(int UserChoice, int MachineChoice)
{
   /* 
    * The good stuff goes here.
    * return a value of 1 if the user wins and 0 if the machine wins
    */
}

Then in the main() function, after getting the players' choices, you would have something that is self-documenting:

CPP / C++ / C Code:
    if (UserWins(UserChoice, MachineChoice)) {
        ++UserWinCounter;
    }
    else {
        ++MachineWinCounter;
    }

Now, anyone can tell at a glance what you have in mind with those statements. (Or so it seems to me.)
Last edited by davekw7x : 22-Oct-2009 at 18:12.
  #3  
Old 22-Oct-2009, 18:09
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Re: Functions, loops, counters, help please.


I would once again like to say many thanks, I currently do not have time to go over all aspects of ur advice. I will however do so tomorrow after work, thank you so much for your time and help!
  #4  
Old 22-Oct-2009, 20:19
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Re: Functions, loops, counters, help please.


CPP / C++ / C Code:
/*
1=rock
2=paper
3-scissors
*/

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;





//Shows the welcome msg and instructions on how to play the game;
 int   userWins (int userChoice , int compChoice);
 void  showWelcome();
 int   showMenu();
 
 
int main()
{
    srand((unsigned)time(0));

	int compChoice = (rand()%3)+1;
	int userChoice;
	int userWins;
	int compWins;
	int userWinCounter;
	int compWinCounter;
	
	
	
	showWelcome();
	userChoice = showMenu();
	
	cout << fixed << showpoint << setprecision(2);
            
    if(userChoice == 1)
	{
                  if (compChoice == 1)
                  cout << "It's a tie! \n\n.";
                  else if (compChoice == 2)
                  cout << "Paper smothers Rock! Computer wins\n\n!";
                  else if (compChoice == 3)
                  cout << "Rock Crushes Scissors! You Win!";
                  
     }             
     
     if (userChoice == 2)
     {
                    if (compChoice == 1)
                    cout <<"Paper Smothers Rock! You Win!\n\n" ;
                    else if (compChoice == 2)
                    cout <<"It's a Tie!\n\n";
                    else if (compChoice == 3)
                    cout <<"Scissors slices paper! Computer Wins!\n\n";
     }
     
     if (userChoice == 3)
     {
                    if (compChoice == 1)
                    cout <<"Rock Crushes Scissors! Computer Wins! \n\n";
                    else if (compChoice == 2)
                    cout <<"Scissors Slices Paper! You Win!\n\n";
                    else if (compChoice == 3)
                    cout <<"It's a Tie!\n\n";
     }
     if (userChoice ==4)
     {
         cout << "This program is now exiting, thank you for playing\n";
     }
     
     
    /* if userWins 
    {
        ++userWinCounter;
    }
    
      else 
    {
        ++compWinCounter;
        
    }
    */
     system ("pause");
     return (0);

}	

	
  //This is the void that shows the welcome msg and instructions on game play.



  void showWelcome()
{
       cout << "This is the classic game of Rock, Paper, Scissors\n";
       cout << "Select your choice from the menu and see who wins!\n";
}

 
int showMenu()
 {
   int userChoice;  
     cout << " \n\t\tRock-Paper-Scissors Menu\n";
     cout << "1. Rock\n";
     cout << "2. Paper\n";
     cout << "3. Scissors\n";
     cout << "4. Exit\n";
     cin >> userChoice;
     
     if (userChoice < 1 || userChoice > 4)

{
      cout << "Please enter 1, 2, 3, or 4:\n";
      cin >> userChoice;
}


         
 return userChoice;
}   

	


Thanks again Dave for your help, this was all the further I was able to take the program with what you gave me. I am still struggling on figuring out how to get that win counter implanted in there, I realize that it still is not declared for useage anywhere, but that is because I am not sure where to declare it. Also I seem to be having a problem with the continual looping, it executes once then stops.
  #5  
Old 23-Oct-2009, 09:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Functions, loops, counters, help please.


Quote:
Originally Posted by Score_Reaper
...problem with the continual looping...

I'll take this one first.


Your program doesn't have the big loop that continues playing until the user says to exit.

Using one of my previous examples without changing much in your program, it could go something like this:

CPP / C++ / C Code:
#include <iostream>
#include <ctime>

using namespace std;

void showWelcome();
int showMenu();

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

    int compChoice;
    int userChoice;

    int userWinCounter = 0;
    int compWinCounter = 0;

    showWelcome(); // Do this once at the beginning

    while (1) { // The big loop terminates only when user says so
        //
        // Get computer choice and user choice for each play
        //
        compChoice = (rand() % 3) + 1;
        userChoice = showMenu();
        cout << endl;
.
.
.
        // All of your stuff to compute and announce the winner
.
.
.
        // I would print the score after each play
    }
    return (0);
}


Quote:
Originally Posted by Score_Reaper
...Ifiguring out how to get that win counter...

Set counters to zero before you begin the play loop.

At each play, after you calculate the winner, increment the appropriate counter. Since you have the calculations spread out over the entire loop, it could go something like this:
CPP / C++ / C Code:
#include <iostream>
#include <ctime>

using namespace std;

void showWelcome();
int showMenu();

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

    int compChoice;
    int userChoice;

    int userWinCounter = 0;
    int compWinCounter = 0;

    showWelcome(); // Do this once at the beginning

    while (1) { // The big loop terminates only when user says so
        //
        // Get computer choice and user choice for each play
        //
        compChoice = (rand() % 3) + 1;
        userChoice = showMenu();
        cout << endl;

        if (userChoice == 1) {
            if (compChoice == 1) {
                cout << "It's a tie!" << endl;
            }
            else if (compChoice == 2) {
                cout << "Paper smothers Rock! Computer wins!" << endl;
                ++compWinCounter;
            }
            else if (compChoice == 3) {
                cout << "Rock Crushes Scissors! You Win!" << endl;
                ++userWinCounter;
            }
        }

        if (userChoice == 2) {
.
.  // Same thing: Increment counter of the winner
.
        }

        if (userChoice == 3) {
.
.  // Same thing: Increment counter of the winner
.
        }

        if (userChoice == 4) {
            cout  << endl
                  << "This program is now exiting, thank you for playing\n";
            break;
        }

        // Print out the score after each play
        cout << "Score:" << endl
             << "   You     : " << userWinCounter
             << "   Computer: " << compWinCounter
             << endl << endl;
    } // This is the end of the "while(1)" loop

    return (0);
}

Regards,

Dave
  #6  
Old 23-Oct-2009, 11:38
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Re: Functions, loops, counters, help please.


Once again dave thank you. Combined with what I've learned today and the assistance you've provided I believe I have all I need to complete this program. I will post the completed program when finished. I learned the proper way to document the counter today which I see that you appear to have done it the same way.

Thanks ,

Score_Reaper
  #7  
Old 28-Oct-2009, 16:02
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Re: Functions, loops, counters, help please.


Well I couldn't get it to work in that form dave even after I spent all that time on it. So I decided to try the switch method and see if I would fair any better but to no avail, This is what I am currently stuck with. I either get and infinite loop, or just the display of the letter I insert.

The goal of this program is to allow the user to play as many times as they want while keeping a counter. When the user Presses "q or Q" to quit, then I am wanting it to display the number of, wins for player, computer, and ties. As well as the overall winner. But I am stumped on something easy I am sure.


*Side note* functions must be used and nothing can be placed in main except the callers (prototypes I think they are called).



CPP / C++ / C Code:
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

void winner(int userChoice, int botChoice);
void playerChoice();
void compChoice();
void showWelcome();
char  showGame();
char showPlay();


//int playerWin = 0;
//int compWin = 0;
//int gameTie= 0;



int main()
{
    
 showWelcome();
 showPlay();
 showGame();
 
 
}



void showWelcome()  
{ 
cout<<"Welcome to Rock Paper Scissors!!\n";
}

char showPlay()

{
          
cout <<"\n";
cout <<"You will be playing against the computer!\n\nYou gain one point for every win!\n\n";
cout <<"Enter:\nR for Rock\nP for Paper\nS for Scissors!\nQ for Quit\n\n";
cout <<"Let's begin!\n";
cout <<"\n";

}



char showGame()
{
char playerChoice;
cout<<"Enter your choice: ";
cin>>playerChoice;


while (playerChoice != 'Q' && playerChoice !='q')
{
      
//if (playerChoice != 'r' || 'R' || 'p' || 'P' || 's' || 'S'|| 'q' || 'Q')
if(playerChoice >= 'P' && playerChoice <='S')
{
cout << "invalid re-enter,R for rock, P for Paper, S for Scissors or Q to quit";
}

     
switch(playerChoice)
{                   
case 'r': case 'R':
cout<<"Player: Rock"<<endl;
playerChoice = 0;
break;

case 'p': case 'P':
cout<<"Player: Paper"<<endl;
playerChoice = 1;
break;

case 's': case 'S':
cout<<"Player: Scissors"<<endl;
playerChoice = 2;
break;

case 'q': case 'Q':
cout << "Thanks for playing. Good Bye"<<endl;
break;
}
}


srand((unsigned)time(0));

int compChoice = rand()%3+1;

switch(compChoice)
{
case 0:
cout<<"Computer: Rock"<<endl;
break;

case 1:
cout<<"Computer: Paper"<<endl;
break;

case 2:
cout<<"Computer: Scissors"<<endl;
break;


}

winner(playerChoice, compChoice);

system ("pause");
return 0;
}

void winner(int userChoice, int botChoice)
{
     
int playerWin = 0;
int compWin = 0;
int gameTie= 0;

if(userChoice == 0 && botChoice == 0 || userChoice == 1 && botChoice == 1
|| userChoice == 2 && botChoice == 2)
{
  cout<<"It's a tie!!"<<endl;
 gameTie++;
}

    else if(userChoice == 0 && botChoice == 1)
{
  cout<<"Paper covers rock!\n"
  <<"You lose!\n";
 compWin++;
}

    else if(userChoice == 1 && botChoice ==0)
{
  cout<<"Paper cover rock!\n"
  <<"You win!\n";
 playerWin++;
}

    else if(userChoice == 0 && botChoice == 2)
{
  cout<<"Rock crushes scissors!\n"
  <<"You win!\n";
 playerWin++;
}

    else if(userChoice == 2 && botChoice == 0)
{
  cout<<"Rock crushes scissors!\n"
  <<"You lose!\n";
 compWin++;
  }

    else if(userChoice = 1 && botChoice == 2)
  {
  cout<<"Scissors cut paper!\n"
  <<"You lose!\n";
compWin++;
 }
    else if(userChoice == 2 && botChoice == 1)
 {
  cout<<"Scissors cut paper!\n"
  <<"You win!\n";
playerWin++;



cout << " You won " <<playerWin << " Games. " << " Computer won " << compWin 
     << " Games." <<"You tied " << gameTie << "times.";
          
//cout << "The over all winner is" << winner;
}

} 


/*Still need to get validation working, counter working, 
calculate the winner and successful looping*/
  #8  
Old 29-Oct-2009, 14:33
Score_Reaper Score_Reaper is offline
New Member
 
Join Date: Oct 2009
Posts: 6
Score_Reaper is on a distinguished road

Re: Functions, loops, counters, help please.


CPP / C++ / C Code:



// Programmer: Score_Reaper;

// October 2009;

// This is a rock, paper, scissors game. Hope you enjoy it as much as I did
// creating it;



#include<iostream>
#include<ctime>
#include<cstdlib>


using namespace std;
void winner(int , int );
void showWelcome();
void showTotal();
int  playerChoice = -1; 
int  compChoice = -1; 
int  showGame();
int  showPlay();
int  playerWin = 0;
int  compWin = 0;
int  gameTie = 0;




//Main
int main()
{
 int usrChoice;
 int compChoice;   
 showWelcome();
 usrChoice = showPlay();
 compChoice = showGame();
  
 while (usrChoice !=3)
 
 { 
 winner(usrChoice, compChoice);
 
 compChoice = showGame();
 usrChoice = showPlay();
 
 
}
 showTotal();
 system("pause");
 return (0); 


}

//First function, welcome message;

void showWelcome()  
{ 
cout<<"Welcome to Rock Paper Scissors!!\n";
cout <<"\nYou will be playing against the computer!";
cout <<"\n\nYou gain one point for every win!\n\n";
cout <<"Let's begin!\n ";
}


//Second function, shows the selection menu also does user input validation;

int showPlay()

{
int playerChoice;        
cout <<"\n";

cout <<"Enter:\n0 for Rock ";
cout <<"\n1 for Paper ";
cout <<"\n2 for Scissors! ";
cout <<"\n3 to Quit\n\n";

cout <<"\n"; 
cout<<"Enter your choice: ";
cin>>playerChoice; 

      
while (playerChoice <0 || playerChoice >3)

{
                 
cout << "invalid re-enter:\n0 for rock,\n1 for Paper,\n2 for Scissors,\n3 to quit\n";
cin >> playerChoice;
}



return playerChoice;
}



//Random # gen;

int showGame()

{

srand((unsigned)time(0));

int compChoice = (rand()%2);


return compChoice;
}



//Counter and compair;
void winner(int userChoice, int botChoice)
{
     
 
//Switch statements user choice;     
switch(playerChoice)
{                   
case 0:
cout<<"Player: Rock"<<endl;
break;

case 1:
cout<<"Player: Paper"<<endl;
break;

case 2:
cout<<"Player: Scissors"<<endl;
break;

case 3:
cout << "Thanks for playing. Good Bye"<<endl;
break;
}     
     
     
//Switch statements comp choice;
switch(compChoice)
{
case 0:
cout<<"Computer: Rock"<<endl;
break;

case 1:
cout<<"Computer: Paper"<<endl;
break;

case 2:
cout<<"Computer: Scissors"<<endl;
break;

}     


if(userChoice == 0 && botChoice == 0 || userChoice == 1 && botChoice == 1
|| userChoice == 2 && botChoice == 2)
{
  cout<<"It's a tie!!"<<endl;
 gameTie++;
}

    else if(userChoice == 0 && botChoice == 1)
{
  cout<<"Paper covers rock!\n"
      <<"You lose!\n";
 compWin++;
}

    else if(userChoice == 1 && botChoice ==0)
{
  cout<<"Paper cover rock!\n"
      <<"You win!\n";
 playerWin++;
}

    else if(userChoice == 0 && botChoice == 2)
{
  cout<<"Rock crushes scissors!\n"
      <<"You win!\n";
 playerWin++;
}

    else if(userChoice == 2 && botChoice == 0)
{
  cout<<"Rock crushes scissors!\n"
      <<"You lose!\n";
 compWin++;
  }

    else if(userChoice = 1 && botChoice == 2)
  {
  cout<<"Scissors cut paper!\n"
      <<"You lose!\n";
compWin++;
 }
    else if(userChoice == 2 && botChoice == 1)
 {
  cout<<"Scissors cut paper!\n"
      <<"You win!\n";
playerWin++;
 }

}


void  showTotal()
{
 cout << " You won " <<playerWin << " Games. " << " Computer won " << compWin 
      << "\n Games." <<"You tied " << gameTie << "times. \n";
      
      if (compWin > playerWin)
      {
        cout <<"Computer Is the over all winner";
        }
       else if (playerWin > compWin) 
       {
            cout << "You are the over all winner";
        }
        else if (playerWin != 0 && compWin != 0)
         {
         cout << "You tied with the computer BLNT!";
          }
                      
}


Finally finished it and got it working, this was writting and compiled in Dev-C++ so anyone who has questions feel free to ask.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Display the graphs of functions reedz C++ Forum 1 25-Feb-2008 21:30
Help with some functions baka_yaro C++ Forum 3 14-Mar-2007 06:56
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Functions and Classes - Where did I go wrong? redmage C++ Forum 5 10-Apr-2005 19:31
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:46.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.