7 Replies - 966 Views - Last Post: 18 October 2008 - 01:07 PM Rate Topic: -----

#1 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

help in starting off with code

Post icon  Posted 16 October 2008 - 09:23 AM

im making a program that will let the user play rock paper scissors with the computer (exciting huh? lol.. :P )

there will be a little intro to the game, and then i will have a menu
where the user can pick 1. for rock, 2.scissors, etc 4. quit.

once the user picks their choice, the computer will randomly select one as well.
then it should display a message that repeats what each person chose.

then if you won it should say you chose rock, rock smashes scissors, you win, (or vice versa)

the program should also keep a tally of amount of times played and who has won x amount of games.


i want to make this program as simple as possible, and want to use functions (this is the easiest one i think-since we just learned this method) and i want to use switch statements for my menu as well.

the prob is i dont know how to start off writing my code or what kind of outline i must have, and how to make a counter that will keep count of the games.

please help me in starting this.

much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: help in starting off with code

#2 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

Re: help in starting off with code

Posted 16 October 2008 - 10:13 AM

ok here is what i have so far:
#include <iostream>
using namespace std;

int main()

//function protoypes
void menu();
void userChoice();
void computerChoice();

int rock, paper, scissors;

do 
{
menu();
cin >> userChoice;

while (userChoice < 1 || userChoice > 4)
{
cout << "please enter 1, 2, 3, or 4";
cin >> userChoice;
}



this is all i have so far....(i know i havent actually written the actual functions yet)
then i do the if statements for the choices.......

how do i set up a statement where the computer can randomly generate their own choice?
Was This Post Helpful? 0
  • +
  • -

#3 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: help in starting off with code

Posted 16 October 2008 - 11:41 AM

This is an 'outline' solution that you may be able to adapt ...

Notice how I took each task that you listed ... in steps ... and then wrote some code to outline that task.

This 'outline' compiles ok ... and runs ok ... if you enter choice '1' ...


 /* help ... in starting ... */
#include <iostream>

using namespace std;

/*
    im making a program  < ? >
    that will let the user play rock paper scissors 
    with the computer ( exciting huh? ) ... 
    < Is it exciting to learn some new skills ? >
*/

string toString( char reply )
{
    if ( reply == '1' ) return "Rock";
    //if ...
    //if ...
}

char getComputerReply() // you need to finish this ...
{
    // now get x, ...  a random num in range 1..3  
    // and return that number converted to the char ie. 1 -> '1'  
    // you could return char( x +'0' );   
}

void displayChoices( char myReply, char compReply )
{
    // you need to make the function ...toString
    cout << "Your choice was: ";// << toString( myReply ) << endl;   
    cout << "Comp choice was: ";// << toString( compReply ) << endl;
}


int main()
{
    /*
        there will be a 'little' intro to the game, and then i will have a menu
        where the user can pick 1. for rock, 2.scissors, etc 4. quit.
    */
    
    int compWins =   0; // initial 'tallys' to zero
    int playerWins = 0; 
    
    for(;;)/> // do forever loop ... until a 'return', 'exit' or 'break'
    {
        cout << "\n\n1. Rock, 2. Scissors, 3. ..... q . quit. ";
        char myReply;
        cin >> myReply;
        /*
            once the user picks their choice, 
            the computer will randomly select one as well.
            then it should display a message 
            that repeats what each person chose.
        */
        
        // computer returns a random number (char) in range '1' ... 'x'
        char compReply = getComputerReply(); // You need to make this function
        
        displayChoices( myReply, compReply ); // could call a 'void' function here
        
        /*
            ... if you win ...
            it should say you chose rock, rock smashes scissors, 
            you win, (or vice versa)
        */
        switch( myReply )
        {
            case '1' :
                if( compReply != '1' )
                {
                    cout << "You choose rock ... and rock smashes"; //<< toString(compReply) << endl;
                    ++playerWins; // update .... and report 'wins'
                   
                }    
                else // both choose rock ... so nobody wins here   print this out
                break;
            case '2' :
                // ....
                break;
            
            // ....    
                
            case 'q' :
                
                // ... display exit message
                // pause for key press ... then ...
                cout << "\nPress 'Enter' key to continue ...";
                cin.sync(); // 'flush' cin in-stream
                cin.get(); // wait for 'Enter' to be pressed ...
                return 0; // exit program now ....      
        }

        /*
            the program should also keep a tally 
            of amount of times played 
            and who has won x amount of games.
        */
        cout << "\nPlayer   wins " << playerWins << endl;
        cout << "Computer wins " << compWins << endl;        
        
    }// end for loop       
}


/*
i want to make this program as simple as possible, 
and want to use functions 
(this is the easiest one i think-since we just learned this method) 
and i want to use switch statements for my menu as well.

the prob is i dont know how to start off writing my code o
r what kind of outline i must have, 
and how to make a counter that will keep count of the games.

please help me in starting this.

*/

This post has been edited by David W: 16 October 2008 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: help in starting off with code

Posted 16 October 2008 - 06:15 PM

View Postucmath786, on 16 Oct, 2008 - 09:23 AM, said:

im making a program that will let the user play rock paper scissors with the computer (exciting huh? lol.. :P )

there will be a little intro to the game, and then i will have a menu
where the user can pick 1. for rock, 2.scissors, etc 4. quit.

once the user picks their choice, the computer will randomly select one as well.
then it should display a message that repeats what each person chose.

then if you won it should say you chose rock, rock smashes scissors, you win, (or vice versa)

the program should also keep a tally of amount of times played and who has won x amount of games.


i want to make this program as simple as possible, and want to use functions (this is the easiest one i think-since we just learned this method) and i want to use switch statements for my menu as well.

the prob is i dont know how to start off writing my code or what kind of outline i must have, and how to make a counter that will keep count of the games.

please help me in starting this.

much appreciated.


I have spent the past 15ish minutes writting this. It does not (properly) calculate a "WIN" or a "LOSE," I've left that up to you...

#include <iostream>
#include <time.h> /* for srand() function */
#include <string> /* for the answer */

using namespace std;

int menu() {

	/* Windows specific -- Console */
	system("CLS");

	cout << "\t\tWelcome to Scissors, Paper, Rock" << endl << endl;

	cout << "\t\t\t -- OPTIONS --" << endl;
	cout << "\t\t - 1. Scissors" << endl;
	cout << "\t\t - 2. Paper" << endl;
	cout << "\t\t - 3. Rock" << endl << endl;
}

int answer(int& userChoice, string& szUserChoice) {

	cout << "PLAYER! Please input your choice: ";

	while (userChoice < 1 || userChoice > 3)
	{
		  cin >> userChoice;
		  if (userChoice < 1 || userChoice > 3) {
						 cout << endl << "PLAYER! Incorrect input... Try again: ";
		  }
	}

	cout << endl;
	switch (userChoice) {
		   case 1 : szUserChoice = "Scissors"; break;
		   case 2 : szUserChoice = "Paper"; break;
		   case 3 : szUserChoice = "Rock"; break;
	}

	return 0;
}

int computerChoice(int& compChoice, string& szCompChoice) {

	srand (time(NULL));			 /* randomized value based on Computer Clock */
	compChoice = rand() % 2 + 1;	/* assign randomized value to "compChoice" */

	switch (compChoice) {
		   case 1 : szCompChoice = "Scissors"; break;
		   case 2 : szCompChoice = "Paper"; break;
		   case 3 : szCompChoice = "Rock"; break;
	}

	return 0;
}

int main() {

	/* Windows specific -- Console */
	system("TITLE Scissors, Paper, Rock!");

	int userChoice = 0;
	int compChoice = 0;

	string szUserChoice;
	string szCompChoice;
	string result;

	menu();									  /* Show the menu */
	answer(userChoice, szUserChoice);			/* Input (and check) users input */
	computerChoice(compChoice, szCompChoice);	/* Input (and check) computers input */

	cout << "You had: " << szUserChoice << endl;
	cout << "Computer had: " << szCompChoice << endl << endl;

	/* Show the user if they had won or lost */
	if (userChoice < compChoice) { result = "LOST!"; }
	else { result = "WON!"; }

	cout << "PLAYER! You have... " << result;

	_flushall();
	cout << endl << endl;
	cout << "Press enter to exit";
	cin.get();

	return 0;
}



Welcome! Hope this helps you figure it out (I don't mean to spoil... I personally love help as shown, it helps you to READ and UNDERSTAND how the logic of it all comes together, like a jig-saw puzzle).
Was This Post Helpful? 0
  • +
  • -

#5 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

Re: help in starting off with code

Posted 16 October 2008 - 07:16 PM

ok...i spent the whole day writing this and for some reason, my compiler piles it with NO errors, but it does not say who wins.

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


//*********************
//Function Prototypes *
//*********************

void ProgramInfo();
int UserChoice();
int ComputerChoice();
int DetermineAndDisplayWinner(int User, int Computer);
void PrintObject(int obj);
void Pause();


const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
const int Quit = 4;
const int CompWon = 5;
const int UserWon = 6;
const int Tie = 7;


int main()
{
	
	int User;
	int Computer;
	int obj;
	int choice;

	char answer = 'y';
	while (answer == 'y' || answer == 'Y')
	{


	
	choice = UserChoice();
	ComputerChoice();
	DetermineAndDisplayWinner(User,Computer);
	PrintObject(choice);

	cout << "Do you want to continue?";
	cin >> answer;
	} 

	return 0;
}


//*************************
//Functions			   *
//*************************





//*******************
//Menu&User Choice  *
//*******************

int UserChoice()
{

	int choice;
	cout << "Select one of the following options" << endl;
	cout << "1. Rock" << endl;
	cout << "2. Paper" << endl;
	cout << "3. Scissors" << endl;
	cout << "4. Quit" << endl;
	cout << "Enter your choice: ";
	cin >> choice;

	while(choice < 1 || choice > 4)
	{
		cout << endl;
		cout << "Please enter 1,2,3, or 4.";
		cin >> choice;
		
	}

	cout << endl;
	
	if (choice == 1) 
		cout << "You picked Rock\n";
	else if (choice == 2)
		cout << "You picked Paper\n";
	else if (choice == 3)
		cout << "You picked Scissors\n";
	else 
		cout << "Goodbye!\n";
	
	
	return choice;
	
	
}

//***************************
//Get Computer Choice	   *
//***************************

int ComputerChoice()
{
	int randomNumber;
	srand( (unsigned) time (NULL) );
	randomNumber = rand() % 3 + 1;

	if (randomNumber == 1) 
		cout << "The computer picked Rock\n";
	else if (randomNumber == 2)
		cout << "The computer picked Paper\n";
	else 
		cout << "The computer picked Scissors\n";

	return randomNumber;

}

//*********************************
//Determine And Display Winner	*
//*********************************

int DetermineAndDisplayWinner(int User, int Computer)
{



	if (User == Rock && Computer == Rock)
	{
		cout << "Its a Tie. Try again";
		return Tie;
	}
	
	if (User == Rock && Computer == Paper)
	{
		cout << "You lose. Paper wraps Rock.";
		return CompWon;
	}

	if (User == Rock && Computer == Scissors)
	{
		cout << "You win. Rock Smashes Scissors.";
		return UserWon;
	}

	if (User == Paper && Computer == Scissors)
	{
		cout << "You lose. Scissors cuts Paper.";
		return CompWon;
	}

	if (User == Paper && Computer == Paper)
	{
		cout << "It's a tie. Try again.";
		return Tie;
	}

	if (User == Paper && Computer == Rock) 
	{
		cout << "You win. Paper Wraps Rock.";
		return CompWon;
	}

	if (User == Scissors && Computer == Scissors)
	{
		cout << "It's a tie. Try again.";
		return Tie;
	}

	if (User == Scissors && Computer == Rock)
	{
		cout << "You lose. Rock smashes Scissors.";
		return CompWon;
	}

	if (User == Scissors && Computer == Paper)
	{
		cout << "You win. Scissors cuts Paper.";
		return UserWon;
	} 

	return -1;

}

//*************************************
//Print Object						*
//*************************************

void PrintObject(int obj)
{
	switch(obj)
	{
	case Rock: cout << "Rock";
		break;
	case Paper: cout << "Paper";
		break;
	case Scissors: cout << "Scissors";
		break;
	case Quit: cout << "Quit";
		break;
	case CompWon: cout << "Computer Wins";
		break;
	case UserWon: cout << "You Win";
		break;
	case Tie: cout << "It's a tie";
		break;
	default: cout << "Invalid. try again";
	}

}

//*****************
//Pause		   *
//*****************

void Pause()
{
cin.ignore(80, '\n');
}





i think some of my functions and calls are jumbled up!
can anyone help me here?? im soo close to it!!
Was This Post Helpful? 0
  • +
  • -

#6 xtreampb   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 345
  • Joined: 20-June 07

Re: help in starting off with code

Posted 16 October 2008 - 07:33 PM

ok you have them in the right order. but the only paramiter that is being updated is choice which is being modifyed in the user choice. the computer var needs to be updated as well. I don't think that this is all that is wrong with your code, but right now that is all that i can see. I am doing a bunch of scrolling and my eyes are getting mixed up. Where do YOU think the logical error is located?

This post has been edited by xtreampb: 16 October 2008 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: help in starting off with code

Posted 17 October 2008 - 03:07 AM

You have come a long way ... Just a few things added ... like keeping and reporting score ... Take a look :D

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


//*********************
//Function Prototypes *
//*********************

void ProgramInfo();
int UserChoice();
int ComputerChoice();
int DetermineAndDisplayWinner(int User, int Computer);
void PrintObject(int obj);

int charToInt( char c )
{
    if ( c<'0' || c>'9' ) return -1; // error flag that char is not in range '0'..'9' 
    // else ...
    return c - '0';
}


// a GOOD use of some Global constants ...
const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
const int Quit = 4;
const int CompWon = 5;
const int UserWon = 6;
const int Tie = 7;

// Global variables ... set initial values to zero
int playerWins = 0;
int computerWins = 0;
int numGames =0;


int main()
{
    srand( (unsigned) time (NULL) ); // call this once ... at the start
    
    int User;
    int Computer;
    int obj;
    int choice;

    char answer = 'y';
    do
    {
        User = UserChoice();
        Computer = ComputerChoice();
        
        //... Always returns a valid 'obj' value ...
        obj = DetermineAndDisplayWinner(User,Computer);
        
        // You could 'comment' this next line out ... 
        // as it adds no new info to game.
        // But it does 'sneak in' your 'switch' structure ... 
        PrintObject(obj);
        
        // display results so far ...
        cout << "\nSo far you have won " << playerWins
             << " and the computer has won " << computerWins
             << " in " << numGames
             << " games.\n\n"
             << "Do you want to continue? ";
             
        answer = cin.get();
        cin.sync(); // 'flush' cin stream ...
    
        // need to enter n or N to exit ...
    
    }while ( ! (answer == 'n' || answer == 'N' ) );

}


//*************************
//Functions               *
//*************************


//*******************
//Menu&User Choice  *
//*******************

int UserChoice()
{

    int choice;
    for(;;)/> // a C++ forever loop ... until break
    {
        cout << "Select one of the following options:\n\n";
        cout << "1. Rock" << endl;
        cout << "2. Paper" << endl;
        cout << "3. Scissors" << endl;
        cout << "4. Quit" << endl;
        cout << "\nYour choice ? ";

        choice = charToInt( cin.get() );
        cin.clear(); // clear any error flags in case a NON integer was entered
        cin.sync(); // 'flush' cin ..
        
        if (choice < 1 || choice > 4)
        {
            cout << "\nPlease enter only 1, 2, 3 or 4.\n\n";
            continue; // ... from the top of the for loop ...
        }
        // if we reach here ... then
        break;
    }
    
    // Ok ... we have a valid number ... so print
    
    if (choice == 1)
        cout << "\nYou picked Rock.\n";
    else if (choice == 2)
        cout << "\nYou picked Paper.\n";
    else if (choice == 3)
        cout << "\nYou picked Scissors.\n";
    else
    {
        // display results so far ...
        cout << "\nYou have won " << playerWins
             << " and the computer has won " << computerWins
             << " in " << numGames
             << " games.\n\n"
            << "Goodbye!  Press 'Enter' key to exit ... ";
        cin.get();
        exit( 0 );
    }
    
    return choice;
}

//***************************
//Get Computer Choice       *
//***************************

int ComputerChoice() // will always return only one from this set: {1, 2, 3}
{
    int randomNumber = rand() % 3 + 1;
    
    if (randomNumber == 1)
        cout << "The computer picked Rock.\n";
    else if (randomNumber == 2)
        cout << "The computer picked Paper.\n";
    else
        cout << "The computer picked Scissors.\n";

    return randomNumber;
}

//*********************************
//Determine And Display Winner    *
//*********************************
/*
const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
const int Quit = 4;
const int CompWon = 5;
const int UserWon = 6;
const int Tie = 7;
*/

int DetermineAndDisplayWinner(int User, int Computer)
{
    ++numGames; // increment game counter ...
    
    if (User == Rock && Computer == Rock)
    {
        cout << "Its a Tie. Try again.";
        return Tie;
    }
    
    if (User == Rock && Computer == Paper)
    {
        cout << "You lose. Paper wraps Rock.";
        ++computerWins;
        return CompWon;
    }

    if (User == Rock && Computer == Scissors)
    {
        cout << "You win. Rock Smashes Scissors.";
        ++playerWins;
        return UserWon;
    }

    if (User == Paper && Computer == Scissors)
    {
        cout << "You lose. Scissors cuts Paper.";
        ++computerWins;
        return CompWon;
    }

    if (User == Paper && Computer == Paper)
    {
        cout << "It's a tie. Try again.";
        return Tie;
    }

    if (User == Paper && Computer == Rock)
    {
        cout << "You win. Paper Wraps Rock.";
        ++playerWins;
        return UserWon;
    }

    if (User == Scissors && Computer == Scissors)
    {
        cout << "It's a tie. Try again.";
        return Tie;
    }

    if (User == Scissors && Computer == Rock)
    {
        cout << "You lose. Rock smashes Scissors.";
        ++computerWins;
        return CompWon;
    }

    if (User == Scissors && Computer == Paper)
    {
        cout << "You win. Scissors cuts Paper.";
        ++playerWins;
        return UserWon;
    }
}

//*************************************
//Print Object                        *
//*************************************

// only valid values for 'obj' are ever passed in here
void PrintObject(int obj)
{
    switch(obj)
    {
    case Rock: cout << "  Rock.";
        break;
    case Paper: cout << "  Paper.";
        break;
    case Scissors: cout << "  Scissors.";
        break;
    case Quit: cout << "  Quit.";
        break;
    case CompWon: cout << "  Computer Wins.";
        break;
    case UserWon: cout << "  You Win.";
        break;
    case Tie: cout << "  It's a tie.";
        break;
    // this is not really necessary here ... as per above
        default: cout << "  Invalid. Try again";
    }
}

This post has been edited by David W: 17 October 2008 - 04:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 ucmath786   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-October 08

Re: help in starting off with code

Posted 18 October 2008 - 01:07 PM

thanks for your help.

and thanks for writing in those comments-they really help me understand :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1