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
  • 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...

  • 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...

  • need help, dont understand how to set up. 1 You are a game developer at Microsoft...

    need help, dont understand how to set up. 1 You are a game developer at Microsoft Corporation and are charged with the task of developing games in C++. 2 Please develop a Rock Paper Scissor game for two players. Each player should have one of the following options: • 1. Rock • 2. Paper • 3. Scissor A. Player One: person You need to ask the person to enter one option from the above three. 10 B. Player Two: computer...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

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
ADVERTISEMENT