Question

Good evening, here is a C program question. I wanna to write a very small game....

Good evening, here is a C program question.

I wanna to write a very small game. The rule of the game is like rock scissors. Both of the player and the computer generate a letter taking the value A, B, C. And A<B, B<C, C<A (Like rock scissors game, rock<cloth, cloth< scissor, scissor<rock)

Requirement 1: New users need to register an account with a username and password. Actually, the account should be a structure type variable containing: a username, a password and a record of the game history (The game history should be stored in a two-dimensional array and containing the following: Number of rounds in a game; Number of player wins; Number of computer wins; Number of draws; Whether or not the game was overall a win draw of loss). All of the accounts should be stored in a data file and accessed by the program.

Requirement 2: When the user have successfully registered or log on to an already registered account, the program should have the following choices: a) Start a new game; Review their game history; Clear their game history; log out.

And please write in C and avoid using global pointers. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// ROCK SCISSOR GAME WITH FILE AND STRUCTURE
struct account
{
char username[20],password[20];
int history[50][4];
};

int randno(int n)
{
   int rand_max = RAND_MAX - (RAND_MAX % n);
   int ret;
   while ((ret = rand()) >= rand_max);
   return ret/(rand_max / n);
}

int wrandno(int *tbl, int len)
{
   int i, sum, r;
   for (i = 0, sum = 0; i < len; sum += tbl[i++]);
   if (!sum) return randno(len);

   r = randno(sum) + 1;
   for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
   return i;
}

int main()
{
   char uname[20], password[20],un[20],pwd[20];
   struct account acc;
   char umove[10], cmove[10], line[255];
   int user, comp;
   int tbl[]={0,0,0};
   int tbllen=3;
   int choice,n,i,j,nrounds,cwins,pwins,gdraws,n1,n2,n3,n4;
   FILE *fp,*fptemp;
   clrscr();
  
   printf("\n***** Welcome to Rock-Cloth-Scissors *****\n");
   printf("\n1. Already Registered User");
   printf("\n2. New User");
   printf("\nEnter your type : ");
   scanf("%d", &n);
   if (n == 1)
   {
       fp = fopen("users.txt","r");
       printf("Username : ");
       scanf("%s", uname);
       printf("Password : ");
       scanf("%s", password);
       if (fp == NULL)
       {
           printf("Error reading the details!!");
           exit(1);
       }
       while (fp)
       {
           fscanf(fp, "%s %s %d %d %d %d ", un, pwd, &nrounds, &cwins,&pwins,&gdraws);
           if (strcmp(uname, un) == 0 && strcmp(password, pwd) == 0)
           {
               printf("Successfully logged in.");
               strcpy(acc.username, uname);
               strcpy(acc.password, password);
               acc.history[0][0] = nrounds;
               acc.history[0][1] = cwins;
               acc.history[0][2] = pwins;
               acc.history[0][3] = gdraws;
              
               break;
           }
       }
       fclose(fp);
   }
   else if (n == 2)
   {
       fp = fopen("users.txt", "a");
       printf("Username : ");
       scanf("%s", uname);
       printf("Password : ");
       scanf("%s", password);
       if (fp == NULL)
       {
           printf("Error reading the details!!");
           exit(1);
       }
       nrounds = cwins = pwins = gdraws = 0;
       fprintf(fp, "%s %s %d %d %d %d ", uname, password, &nrounds, &cwins, &pwins, &gdraws);
          
       printf("Account created successfully.");
       strcpy(acc.username, uname);
       strcpy(acc.password, password);
       acc.history[0][0] = nrounds;
       acc.history[0][1] = cwins;
       acc.history[0][2] = pwins;
       acc.history[0][3] = gdraws;      
       fclose(fp);
   }
   choice = 0;
   while (choice != 4)
   {
       printf("\n\n Welcome to the Game");
       printf("\n1. Start a new game");
       printf("\n2. Review game history");
       printf("\n3. Clear History");
       printf("\n4. Logout");
       scanf("%d", &choice);
       switch (choice)
       {
       case 1:
           mainloop:
           while (1)
           {
               printf("\n\nPlease press 1 for Rock, 2 For Cloth, 3 for Scissors, 4 to Quit\n");
               srand(time(NULL));
               comp = (wrandno(tbl, tbllen) + 1) % 3;
               scanf("%d",&user);
               if ((user > 4) || (user < 1))
               {
                   printf("Please enter a valid number!\n");
                   continue;
               }
               switch (comp)
               {
               case 1:
                   strcpy(cmove, "Rock");
                   break;
               case 2:
                   strcpy(cmove, "Cloth");
                   break;
               case 3:
                   strcpy(cmove, "Scissors");
                   break;
               default:
                   printf("\nComputer Error, set comp=1\n");
                   comp = 1;
                   strcpy(cmove, "Rock");
                   break;
               }
               switch (user)
               {
               case 1:
                   strcpy(umove, "Rock");
                   break;
               case 2:
                   strcpy(umove, "Cloth");
                   break;
               case 3:
                   strcpy(umove, "Scissors");
                   break;
               case 4:
                   break;                  
               default:
                   printf("\nError! Enter number between 1 to 4.");
                   goto mainloop;
               }
               if (user == 4)
                   break;
               if ((user + 1) % 3 == comp)
               {
                   nrounds++;
                   cwins++;
                   printf("\nComp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);
               }
               else if (comp == user)
               {
                   nrounds++;
                   gdraws++;
                   printf("\nComp Played: %s\nYou Played: %s\nDraw!! :p\n", cmove, umove);
               }
               else
               {
                   nrounds++;
                   pwins++;
                   printf("\nComp Played: %s\nYou Played: %s\nYou Won!!!\n", cmove, umove);
               }
               acc.history[0][0] = nrounds;
               acc.history[0][1] = cwins;
               acc.history[0][2] = pwins;
               acc.history[0][3] = gdraws;
               tbl[user - 1]++;
           }
           break;
       case 2:
           printf("\n\nYOUR GAME HISTORY");
           printf("\nNo of rounds played : %d", nrounds);
           printf("\nYour score : %d", pwins);
           printf("\nComputer's score : %d", cwins);
           printf("\nNo of draws : %d", gdraws);
           if (pwins > cwins)
               printf("\nCongrats!! You are the WINNER!!!");
           else
               printf("\nComputer wins!!!");
           break;
       case 3:
           nrounds = cwins = pwins = gdraws = 0;
           acc.history[0][0] = nrounds;
           acc.history[0][1] = cwins;
           acc.history[0][2] = pwins;
           acc.history[0][3] = gdraws;
           printf("\nHISTORY CLEARED.");
           break;
       case 4:
           fp = fopen("users.txt", "r");
           fptemp = fopen("tmp.txt", "w");
           while (!feof(fp))
           {
               fscanf(fp, "%s %s %d %d %d %d", un, pwd, &n1, &n2, &n3, &n4);

               if (strcmp(uname, un) == 0)
               {
                   fprintf(fptemp, "%s %s %d %d %d %d\n", uname, password, nrounds, cwins, pwins, gdraws);
               }
               else
               {
                   fprintf(fptemp, "%s %s %d %d %d %d\n", un, pwd, n1, n2, n3, n4);
               }
           }
           fclose(fp);
           fclose(fptemp);
           remove("users.txt");
           rename("tmp.txt", "users.txt");
           printf("Goodbye! Thanks for playing!\n");
           return 0;
       }
   }

  
}

Add a comment
Know the answer?
Add Answer to:
Good evening, here is a C program question. I wanna to write a very small game....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Good evening, here is a C program question. I wanna to write a very small game....

    Good evening, here is a C program question. I wanna to write a very small game. The rule of the game is like rock scissors. Both of the player and the computer generate a letter taking the value A, B, C. And A<B, B<C, C<A (Like rock scissors game, rock<cloth, cloth< scissor, scissor<rock) Requirement 1: New users need to register an account with a username and password. Actually, the account should be a structure type variable containing: a username, a...

  • (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character. Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If the...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and...

    Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...

  • Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive:...

    Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive: human vs. computer! Making the game interactive presents a new issue for us -- how do we get the computer&#39;s choice? To solve this, we will use a &quot;random number generator&quot;, which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually &quot;thinking&quot; and making its own decisions. This is the largest, most involved program...

  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT