Question: About papper, rock and sissors?

This is a discussion on Question: About papper, rock and sissors? within the C Programming forums, part of the General Programming Boards category; Hello this is what i have so far and i am trying to rewite the program so that messages like ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question: About papper, rock and sissors?

    Hello this is what i have so far and i am trying to rewite the program so that messages like the following arte printed:

    You chose paper and I chose rock. You win.
    So how would i go about doing this, it may be simple but i have not seen it yey so can anyone help me, please?

    #include "p_r_s.h"

    void report(outcome result, int *win_cnt_ptr,
    int *lose_cnt_ptr, int *tie_cnt_ptr)
    {
    switch (result) {
    case win:
    ++*win_cnt_ptr;
    printf("%27sYou win.\n", "");
    break;
    case loose:
    ++*loose_cnt_ptr;
    printf("%27sYou loose.\n", "");
    break;
    case tie:
    ++*tie_cnt_ptr;
    printf("%27sYou tie.\n", "");
    break;
    default:
    printf("PROGRAMMER ERROR: Unexpected result!\n\n");
    exit(1);
    }
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Could you please use code tags,

    [X]
    /* put code here */
    [/X]

    Just replace the 'X' with the word "CODE", it's that simple.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Could you please use code tags,

    I am curoius, why?
    whats the big deal?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you use code tags, then your code will be neat.

    Eg.
    Code:
    #include <stdio.h>
    #include <codeTags.h>
    
    void clarify()
    {
            printf("You can read this code easily, right?");
    }
    
    int main()
    {
           clarify();  
           return 0;
    }

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanx for the tip!

    No Problemo!
    I will do it from now on BIG DOG in the HOUSE!
    RUFF! RUFF!!
    Peace out!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's a solution:

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    
    void function(int* rnd, char* choice)
    {
    	*rnd = rand()%3+1;
    	switch(*rnd)
    	{
    		case 1: strcpy(choice, "Paper");break;
    		case 2: strcpy(choice, "Rock");break;
    		case 3: strcpy(choice, "Scissors");break;
    	}
    }
    
    int calculate(int p1,int p2)
    {
    	if(p2==p1)
    		return 0;
    	if(p2 > p1)
    	{
    		if( (p2 - p1) == 1 )
    			return -1;
    		return 1;
    	}
    	if( (p1 - p2) == 1 )
    		return 1;
    	return -1;
    }
    
    int main()
    {
    	int  player1, player2;
    	char p1Choice[9], p2Choice[9];
    	srand((unsigned)time(NULL));
    	function(&player1,p1Choice);
    	function(&player2,p2Choice);
    	
    	printf("Player one chose %s", p1Choice);
    	printf("\nPlayer two chose %s", p2Choice);
    
    	if(calculate(player1,player2) == 0)
    		printf("\nIt's a tie!!", player1);
    	else if(calculate(player1,player2) == 1)
    		printf("\nPlayer 2 wins!!", player1);
    	else 
    		printf("\nPlayer 1 wins!!", player1);
    	
    	return 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So how would i go about doing this
    Perhaps if you maintain three strings, one for each option, and only use those in the output. For example:
    Code:
    char *option[] = {
      "Paper",
      "Rock",
      "Scissors",
    };
    
    <snip>
    
    POne = option[i];
    PTwo = option[j];
    
    <snip>
    
    printf ( "You chose %s and I chose %s. ", POne, PTwo );
    switch ( result ) {
    case WIN:  printf ( "You lose.\n" ); break;
    case LOSE: printf ( "You win.\n" ); break;
    case TIE:  printf ( "You tie.\n" ); break;
    default:
      perror ( "Say what?" );
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Just for fun, try this


    Code:
    #define PAPER 0
    #define ROCK 1
    #define SCISSORS 2
    
    #define NO_WINNER 3
    #define COMPUTER 4
    #define HUMAN 5
    
    
    
    
    
    int Random()
     {
      srand((unsigned)time(NULL));
      return rand()%3;
     }
    
    
    
    
    
    int Find_Winner( int your_choice, int computers_choice )
     {
      int i;
    
      int winner = NO_WINNER;
    
      int win[]  = { PAPER,  ROCK,     SCISSORS };
      int lose[] = { ROCK,   SCISSORS, PAPER    };
    
       for( i = 0; i < 3; i++)
        {
         if( ( win[ i ] == your_choice ) && ( lose[ i ] == computers_choice) ) winner = HUMAN;
         else
         if( ( win[ i ] == computers_choice ) && ( lose[ i ] == your_choice) ) winner = COMPUTER;
        }
    
      return winner;
     }
    
    
    
    
    
    int main(int argc, char *argv[])
    {
     char input[6];  //...a buffer for input...
    
     char *option[] = {
      "Paper",
      "Rock",
      "Scissors"
    };
    
    
    
    int wins = 0,
        losses = 0,
        ties = 0;
    
    int your_choice,
        computers_choice,
        result;
    
    
    
    do{
    
    printf("\n\n"
    "Choose a weapon!\n\n"
    " (1) %s \n"
    " (2) %s \n"
    " (3) %s \n"
    " (4) Quit\n\n",
    option[0], option[1], option[2]);
    
    
    your_choice = atoi( fgets( input, 5, stdin ) );
    
    
    --your_choice;  //...adjust to match options, which start at '0'...
    
    
    if( your_choice > 2 || your_choice < 0 ) break; //...assume player is trying to quit...
    
    
    clrscr();
    
    
    computers_choice = Random();
    
    
    
    printf("\n\nYou Chose %s. \n", option[ your_choice ]);
    printf("\nComputer Chooses %s. \n\n", option[ computers_choice ]);
    
    
    
    result =  Find_Winner( your_choice, computers_choice );
    
    
    switch( result )
     {
      case COMPUTER: ++losses;
      printf("You Lose! %s Beats %s. \n", option[ computers_choice ], option[ your_choice ] );
      break;
    
      case HUMAN:   ++wins;
      printf("You Win! Your %s Beat His %s. \n", option[ your_choice ] , option[ computers_choice ]);
      break;
    
      default:       ++ties;
      printf("It's A Tie! You Both Chose %s. \n", option[ your_choice ] );
     }
    
    }while(1);
    
    
    
    
    clrscr();
    
    
    
    
    char *master = "None";
    
    if(wins > losses) master = "You";
    else
    if(losses > wins) master = "Computer";
    
    
    
    printf("\n\n\n\n"
    "  Winner: %s!\n\n"
    "  Thanks For Playing!\n\n"
    "  - Your Score - \n"
    "  Wins:   %3i\n"
    "  Losses: %3i\n"
    "  Ties:   %3i\n",
    master, wins, losses, ties);
    
      getch();
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21