Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Building Paper, Rock, Scissors

Reply
Thread Tools

Building Paper, Rock, Scissors

 
 
TooNaive
Guest
Posts: n/a
 
      06-14-2004
Hello all,

I'm taking a C class and am having to write a program to play a game
of rock, paper scissors, and with the output of:

You chose paper and I chose rock. You Win

where paper is the players choice, and rock is the computers choice,
for reference i was given the p_r_s.h, compare.c, main.c, selection.c,
and wrt.c, and i have had to build the report.c to describe the
output. I will post what i have so far in this venture, but when i
compile i keep getting 2 fatal errors and i cannot figure out what to
do next. Any help would be greatly appreciated. The files are as
follows:

the report.c file that i am working on is this:

report.c file


#include "p_r_s.h"

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)

{
switch (result) {
case win:
++*win_cnt_ptr;
if (player_choice == paper)
printf("%27sYou chose paper I chose rock. You win.\n", "");
else if (player_choice == rock)
printf("%27sYou chose rock I chose scissors. You win.\n", "");
else if (player_choice == scissors)
printf("%27sYou chose scissors I chose paper. You win.\n", "");
break;
case lose:
++*lose_cnt_ptr;
if (player_choice == paper)
printf("%27sYou chose paper I chose scissors. You lose.\n", "");
else if (player_choice == rock)
printf("%27sYou chose rock I chose paper. You lose.\n", "");
else if (player_choice == scissors)
printf("%27sYou chose scissors I chose rock. You lose.\n", "");
break;
case tie:
++*tie_cnt_ptr;
if (player_choice == paper)
printf("%27sYou chose paper I chose paper. We tie.\n", "");
else if (player_choice == rock)
printf("%27sYou chose rock I chose rock. We tie.\n", "");
else if (player_choice == scissors)
printf("%27sYou chose scissors I chose scissors. We tie.\n", "");
break;
default:
printf("PROGRAMMER ERROR: Unexpected result!\n\n");
exit(1);

}
}

__________________________________________________ ___________________________

the
p_r_s.h file:

/* The game of paper, rock, scissors. */

#include <ctype.h> /* for isspace() */
#include <stdio.h> /* for printf(), etc */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */

enum p_r_s {paper, rock, scissors,
game, help, instructions, quit};
enum outcome {win, lose, tie, error};

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 wrt_final_status(int win_cnt, int lose_cnt);
void wrt_game_status(int win_cnt, int lose_cnt, int tie_cnt);
void wrt_help(void);
void wrt_instructions(void);
void report_and_tabulate(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);

__________________________________________________ __________________________

the
compare.c file:

#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("\nPROGRAMMER ERROR: Unexpected choice!\n\n");
exit(1);
}
return result;
}

__________________________________________________ _______________________

the
main.c file:

#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)); /* seed the random number generator
*/
wrt_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_and_tabulate(result, &win_cnt, &lose_cnt, &tie_cnt);
break;
case game:
wrt_game_status(win_cnt, lose_cnt, tie_cnt);
break;
case instructions:
wrt_instructions();
break;
case help:
wrt_help();
break;
default:
printf("\nPROGRAMMER ERROR: Cannot get to here!\n\n");
exit(1);
}
wrt_game_status(win_cnt, lose_cnt, tie_cnt);
wrt_final_status(win_cnt, lose_cnt);
return 0;
}

__________________________________________________ __________________________

the
selection.c file:

#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: ");
while (isspace((int) (c = getchar()))) /* skip white space
*/
;
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;
}

__________________________________________________ _______________________

and the wrt.c file:

#include "p_r_s.h"

void wrt_final_status(int win_cnt, int lose_cnt)
{
if (win_cnt > lose_cnt)
printf("CONGRATULATIONS - You won!\n\n");
else if (win_cnt == lose_cnt)
printf("A DRAW - You tied!\n\n");
else
printf("SORRY - You lost!\n\n");
}

void wrt_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 wrt_help(void)
{
printf("\n%s\n",
"---\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 wrt_instructions(void)
{
printf("\n%s\n",
"---\n"
"PAPER, ROCK, SCISSORS:\n"
" In this game p is for \"paper,\" r is for \"rock,\" and"
" s is for \"scissors.\"\n"
" Both the player and the machine\n"
" will choose one of p, r, or s."
" If the two choices are the same,\n"
" then the game is a tie. Otherwise:\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 (the number of wins so far),\n"
" h for help,\n"
" i for instructions (reprint these instructions),\n"
" q for quit (to quit the game).\n"
"\n"
" This game is played repeatedly until q is entered.\n"
"\n"
" Good luck!\n");
}




Thanks in advance for any help!
 
Reply With Quote
 
 
 
 
T.M. Sommers
Guest
Posts: n/a
 
      06-14-2004
TooNaive wrote:
>
> 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)


This is the function you wrote, ...

> void report_and_tabulate(outcome result,
> int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr);


.... but this is the function the rest of the code expects.

The function you wrote is never called, and the one that is
called does not exist.

 
Reply With Quote
 
 
 
 
TooNaive
Guest
Posts: n/a
 
      06-14-2004
Thanks T.M. for your help, but i am still getting an error when i
compile. I am using Microsoft Visual Studio.net and whenever i compile
i get this error:

report.cpp(5) : error C2447: '{' : missing function header (old-style
formal list?)

the line it references is this one:

1.#include "p_r_s.h"
2.
3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
4.*lose_cnt_ptr, int *tie_cnt_ptr);
5."This line here"
6. {
7. switch (result) {

I have tried adding more {'s, taking away {'s and even went and
re-studied the entire definition of "function headers" and have still
drawn a blank. Could it be my compiler? I know microsoft just loves
adding little bugs in all their software. Any suggestions would be
greatly appreciated. Thanks again!
 
Reply With Quote
 
Case
Guest
Posts: n/a
 
      06-14-2004
TooNaive wrote:
> Thanks T.M. for your help, but i am still getting an error when i
> compile. I am using Microsoft Visual Studio.net and whenever i compile
> i get this error:
>
> report.cpp(5) : error C2447: '{' : missing function header (old-style
> formal list?)
>
> the line it references is this one:
>
> 1.#include "p_r_s.h"
> 2.
> 3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
> 4.*lose_cnt_ptr, int *tie_cnt_ptr);
> 5."This line here"
> 6. {
> 7. switch (result) {
>
> I have tried adding more {'s, taking away {'s and even went and
> re-studied the entire definition of "function headers" and have still
> drawn a blank. Could it be my compiler? I know microsoft just loves
> adding little bugs in all their software. Any suggestions would be
> greatly appreciated. Thanks again!


Try the usual technique: strip away as much code as possible, ending
up with a very small program that stil has this problem. There's a
good chance you'll find what's wrong in the process. If not, post
the few lines you've left here.

HTH

Case

 
Reply With Quote
 
Yu SONG
Guest
Posts: n/a
 
      06-14-2004
TooNaive wrote:
> Thanks T.M. for your help, but i am still getting an error when i
> compile. I am using Microsoft Visual Studio.net and whenever i compile
> i get this error:
>
> report.cpp(5) : error C2447: '{' : missing function header (old-style
> formal list?)
>
> the line it references is this one:
>
> 1.#include "p_r_s.h"
> 2.
> 3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
> 4.*lose_cnt_ptr, int *tie_cnt_ptr);
> 5."This line here"
> 6. {
> 7. switch (result) {
>
> I have tried adding more {'s, taking away {'s and even went and
> re-studied the entire definition of "function headers" and have still
> drawn a blank. Could it be my compiler? I know microsoft just loves
> adding little bugs in all their software. Any suggestions would be
> greatly appreciated. Thanks again!


It said "report.cpp(5)"...
(try making a file named "report.c")


--
Song

/* E-mail.c */
#define User "Y.Song"
#define Warwick "dcs.warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s@%s", User, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
__________________________________________________ _____

 
Reply With Quote
 
Simon Massey
Guest
Posts: n/a
 
      06-14-2004
Try removing the ; at the end of line 4, with this it is a function
prototype and the body is not part of it.

"TooNaive" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Thanks T.M. for your help, but i am still getting an error when i
> compile. I am using Microsoft Visual Studio.net and whenever i compile
> i get this error:
>
> report.cpp(5) : error C2447: '{' : missing function header (old-style
> formal list?)
>
> the line it references is this one:
>
> 1.#include "p_r_s.h"
> 2.
> 3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
> 4.*lose_cnt_ptr, int *tie_cnt_ptr);
> 5."This line here"
> 6. {
> 7. switch (result) {
>
> I have tried adding more {'s, taking away {'s and even went and
> re-studied the entire definition of "function headers" and have still
> drawn a blank. Could it be my compiler? I know microsoft just loves
> adding little bugs in all their software. Any suggestions would be
> greatly appreciated. Thanks again!



 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-14-2004
TooNaive wrote:
> Thanks T.M. for your help, but i am still getting an error when i
> compile. I am using Microsoft Visual Studio.net and whenever i compile
> i get this error:
>
> report.cpp(5) : error C2447: '{' : missing function header (old-style
> formal list?)
>
> the line it references is this one:
>
> 1.#include "p_r_s.h"
> 2.
> 3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
> 4.*lose_cnt_ptr, int *tie_cnt_ptr);

^^^
What is this semicolon doing here?

> 5."This line here"

^^^^^^^^^^^^^^^^
And why is this line outside the braces of the function body?

> 6. {
> 7. switch (result) {
>
> I have tried adding more {'s, taking away {'s and even went and
> re-studied the entire definition of "function headers" and have still
> drawn a blank. Could it be my compiler? I know microsoft just loves
> adding little bugs in all their software.


It looks like you have inserted two massive bugs into your software.

> Any suggestions would be
> greatly appreciated. Thanks again!

 
Reply With Quote
 
T.M. Sommers
Guest
Posts: n/a
 
      06-14-2004
TooNaive wrote:
> Thanks T.M. for your help, but i am still getting an error when i
> compile. I am using Microsoft Visual Studio.net and whenever i compile
> i get this error:
>
> report.cpp(5) : error C2447: '{' : missing function header (old-style
> formal list?)
>
> the line it references is this one:
>
> 1.#include "p_r_s.h"
> 2.
> 3.void report_and_tabulate(outcome result, int *win_cnt_ptr, int
> 4.*lose_cnt_ptr, int *tie_cnt_ptr);


Get rid of the semi-colon at the end.


--
Thomas M. Sommers -- http://www.velocityreviews.com/forums/(E-Mail Removed) -- AB2SB

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paper scissors stone angelojoseph803@googlemail.com Ruby 2 11-01-2007 05:39 PM
Rock paper scissors population algorithm, need help!!! hAmmeRoL@gmail.com C++ 2 08-09-2007 12:59 PM
[SUMMARY] Paper Rock Scissors (#16) Ruby Quiz Ruby 1 01-27-2005 04:39 PM
[QUIZ] Paper Rock Scissors (#16) Ruby Quiz Ruby 24 01-25-2005 08:11 PM
Building to Building wireless Patriot Cisco 2 11-04-2003 05:07 PM



Advertisments