Another rock paper scissor question

This is a discussion on Another rock paper scissor question within the C Programming forums, part of the General Programming Boards category; Hi, New to these forums. I have a rock paper scissor program I need to change and for some reason ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    15

    Unhappy Another rock paper scissor question

    Hi,

    New to these forums.

    I have a rock paper scissor program I need to change and for some reason can't quite see where I need to do the change. I'm sure it's easy but I can't see it.

    I want to change the output from just saying "You win"
    to You chose (paper rock or scissors) and I chose (paper rock or scissors. You win, You lose or You Tie.

    So for instance if I won with paper and the computer lost with rock it would say:

    "You chose paper and I chose rock. You win."

    Thanks in advance for any help.

    mattflick

    Here's the code:

    Code:
    /*The game of paper, rock, and scissors. */
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    enum p_r_s {paper, rock, scissors,
    game, help, instructions, quit};
    
    enum outcome {win, lose, tie, erro};
    
    typedef		enum p_r_s		p_r_s;
    typedef		enum outcome	outcome;
    
    outcome		compare(p_r_s player_choice, p_r_s machine_choice);
    void		prn_final_status(int win_cnt, int lose_cnt);
    void		prn_game_status(int win_cnt,
    							int lose_cnt, int tie_cnt);
    
    void		prn_help(void);
    void		prn_instructions(void);
    void		report(outcome result, int *win_cnt_ptr,
    				   int *lose_cnt_ptr, int *tie_cnt_ptr);
    p_r_s		selection_by_machine(void);
    p_r_s		selection_by_player(void);

    Code:
    #include "p_r_s.h"
    
    int main(void)
    {
    	int			win_cnt = 0, lose_cnt = 0, tie_cnt = 0;
    	outcome		result;
    	p_r_s		player_choice, machine_choice;
    
    	srand(time(NULL));
    	prn_instructions();
    
    	while	((player_choice = selection_by_player()) != quit)
    		switch (player_choice) {
    			case paper:
    			case rock:
    			case scissors:
    				machine_choice = selection_by_machine();
    				result = compare(player_choice, machine_choice);
    				report(result, &win_cnt, &lose_cnt, &tie_cnt);
    				break;
    			case game:
    				prn_game_status(win_cnt, lose_cnt, tie_cnt);
    				break;
    			case instructions:
    				prn_instructions();
    				break;
    			case help:
    				prn_help();
    				break;
    			default:
    				printf("PROGRAMMER ERROR: Cannot get to here!\n\n");
    				exit(1);
    		}
    	prn_game_status(win_cnt, lose_cnt, tie_cnt);
    	prn_final_status(win_cnt, lose_cnt);
    	return 0;
    }
    Code:
    #include "p_r_s.h"
    
    void prn_final_status(int win_cnt, int lose_cnt)
    {
    	if (win_cnt > lose_cnt)
    		printf("CONGRATULATIONS - You win!\n\n");
    	else if (win_cnt == lose_cnt)
    		printf("A DRAW - You tied!\n\n");
    	else
    		printf("SORRY - You lost!\n\n");
    }
    
    
    void prn_game_status(int win_cnt, int lose_cnt, int tie_cnt)
    {
    	printf("\n%s\n%s%4d\n%s%4d\n%s%4d\n%s%4d\n\n",
    		"GAME STATUS:",
    		"	Win:	", win_cnt,
    		"	Lose:	", lose_cnt,
    		"	Tie:	", tie_cnt,
    		"	Total:	", win_cnt + lose_cnt + tie_cnt);
    }
    
    void prn_help(void)
    {
    	printf("\n%s\n",
    		"The following characters can be used for input:\n"
    		"	p	for paper\n"
    		"	r	for rock\n"
    		"	s	for scissors\n"
    		"	g	print the game status\n"
    		"	h	help, print this list\n"
    		"	i	reprint the instructions\n"
    		"	q	quit this game\n");
    }
    
    void prn_instructions(void)
    {
    	printf("\n%s\n",
    		"PAPER, ROCK, SCISSORS:\n"
    		"\n"
    		"In this game\n"
    		"\n"
    		"	p is for \"paper\"\n"
    		"	r is for \"rock\"\n"
    		"	s is for \"scissors\"\n"
    		"\n"
    		"Both player and the machine will choose one\n"
    		"of p, r, or s. If the two chcices are the same,\n"
    		"then the game is a tie. Otherwise:\n"
    		"\n"
    		"	\"paper covers the rock\"		(a win for paper)\n"
    		"	\"rock breaks the scissors\"	(a win for rock)\n"
    		"	\"scissors cut the paper\"		(a win for scissors)\n"
    		"\n"
    		"There are other allowable inputs:\n"
    		"	g for game status	(print number of wins)\n"
    		"	h for help			(print short instructions)\n"
    		"	i for instructions	(print these instructions)\n"
    		"	q for quit			(quit the game)\n"
    		"\n"
    		"This game is played repeatedly until q is entered.\n"
    		"\n"
    		"Good luck!\n");
    }

    Code:
    #include "p_r_s.h"
    
    p_r_s selection_by_machine(void)
    {
    	return ((p_r_s) (rand() % 3));
    }
    
    p_r_s selection_by_player(void)
    {
    	char	c;
    	p_r_s	player_choice;
    
    	printf("Input p, r, or s:	");
    	scanf("%c", &c);
    	switch (c) {
    	case 'p':
    		player_choice = paper;
    		break;
    	case 'r':
    		player_choice = rock;
    		break;
    	case 's':
    		player_choice = scissors;
    		break;
    	case 'g':
    		player_choice = game;
    		break;
    	case 'i':
    		player_choice = instructions;
    		break;
    	case 'q':
    		player_choice = quit;
    		break;
    	default:
    		player_choice = help;
    		break;
    	}
    	return player_choice;
    }

    Code:
    #include "p_r_s.h"
    
    outcome compare(p_r_s player_choice, p_r_s machine_choice)
    {
    	outcome		result;
    
    	if (player_choice == machine_choice)
    		return tie;
    	switch (player_choice) {
    	case paper:
    		result = (machine_choice == rock) ? win : lose;
    		break;
    	case rock:
    		result = (machine_choice == scissors) ? win : lose;
    		break;
    	case scissors:
    		result = (machine_choice == paper) ? win : lose;
    		break;
    	default:
    		printf("PROGRAMMER ERROR: Unexpected choice!\n\n");
    		exit(1);
    	}
    	return result;
    }

    Code:
    #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 lose:
    		++*lose_cnt_ptr;
    		printf("%27sYou lose. \n", "");
    		break;
    	case tie:
    		++*tie_cnt_ptr;
    		printf("%27sYou tie. \n", "");
    		break;
    	default:
    		printf("PRGORAMMER ERROR: Unexpected result!\n\n");
    		exit(1);
    	}
    }
    Last edited by mattflick; 09-29-2005 at 02:44 AM.

  2. #2
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    hi mattflick,

    you posted your "question" in the wrong forum... the right one would be "PLEASE DO MY HOMEWORK"

    jmgk

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Can someone at least point me in the right direction? I'm not where to apply the code too. I'm just asking for a nudge.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mattflick
    I want to change the output from just saying "You win" to You chose (paper rock or scissors) and I chose (paper rock or scissors. You win, You lose or You Tie.
    Perhaps something like this.
    Code:
    void report(outcome result, int *win_cnt_ptr,
                int *lose_cnt_ptr, int *tie_cnt_ptr,
                p_r_s player_choice, p_r_s machine_choice)
    {
       static const char *text[] = { "paper", "rock", "scissors" };
       printf("You chose %s, Machine chose %s : You ", 
              text[player_choice], text[machine_choice]);
       switch ( result )
       {
       case win:
          ++*win_cnt_ptr;
          puts("win.");
          break;
       case lose:
          ++*lose_cnt_ptr;
          puts("lose.");
          break;
       case tie:
          ++*tie_cnt_ptr;
          puts("tie.");
          break;
       default:
          printf("PRGORAMMER ERROR: Unexpected result!\n\n");
          exit(1);
       }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Wow I really suck a this stuff. I would have never thought that. I was thinking if was going have to be an "if" statement followed by multiple "printf" lines utilizing the player_choice and machine_choice. Thanks for the help and better understanding.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Hey Dave,

    It builds with no with no failures, but when I execute it without debugging, if I press r s p for rock, paper or scissor it wants to debug and breaks at the following line:

    Code:
    text[player_choice],text[machine_choice]);
    Any ideas?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You did make corresponding changes to the prototype and calling code, right?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    I changed it too exactly what was shown above.

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Look in the file p_r_s.h. There will be a prototype for the report function, it needs to correspond to the definition that Dave_Sinkula changed.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    ...And you now call it like this?
    Code:
    		switch (player_choice) {
    			case paper:
    			case rock:
    			case scissors:
    				machine_choice = selection_by_machine();
    				result = compare(player_choice, machine_choice);
    				report(result, &win_cnt, &lose_cnt, &tie_cnt, player_choice, machine_choice);
    				break;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    I changed the p_r_s.h to the below:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    enum p_r_s {paper, rock, scissors,
    game, help, instructions, quit};
    
    enum outcome {win, lose, tie, erro};
    
    typedef		enum p_r_s		p_r_s;
    typedef		enum outcome	outcome;
    
    outcome		compare(p_r_s player_choice, p_r_s machine_choice);
    void		prn_final_status(int win_cnt, int lose_cnt);
    void		prn_game_status(int win_cnt,
    							int lose_cnt, int tie_cnt);
    
    void		prn_help(void);
    void		prn_instructions(void);
    void		report(outcome result, int *win_cnt_ptr,
    				int *lose_cnt_ptr, int *tie_cnt_ptr, 
    				p_r_s player_choice, p_r_s machine_choice);
    
    p_r_s		selection_by_machine(void);
    p_r_s		selection_by_player(void);

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    It's working. I changed it in the main.c and p_r_s.h and it's fine. My mistake. Need more attention to detail. Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 09:16 PM
  2. Issues and questions about queues
    By Nazgulled in forum C Programming
    Replies: 36
    Last Post: 12-13-2007, 01:03 PM
  3. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  4. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM
  5. PRS Help
    By SpudNuts in forum C Programming
    Replies: 10
    Last Post: 08-07-2005, 01:14 PM

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