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!
I have updated the content "A<B, B<C, C<A (Like rock scissors game, rock<cloth, cloth< scissor, scissor<rock)"
#include <stdio.h>
#include <string.h>
#include<time.h>
struct account {
char username[50];
char password[50];
int history[50][5];
int games;
};
void printHistory(struct account accounts[], int id) {
printf("\nPlayer %s history\n", accounts[id].username);
printf("Total Games played: %d\n\n", accounts[id].games);
if (accounts[id].games > 0) {
printf("%5s %20s %20s %20s %20s %20s\n", "Game", "Rounds", "Player wins", "Computer wins", "Draws", "overall Result");
for (int i = 0; i < accounts[id].games; i++) {
printf("%5d %20d %20d %20d %20d %20s\n", i + 1, accounts[id].history[i][0], accounts[id].history[i][1],
accounts[id].history[i][2], accounts[id].history[i][3], (accounts[id].history[i][4] == -1 ? "Computer wins" : accounts[id].history[i][4] == 0 ? "Draw" : "You Win"));
}
printf("\n");
}
else
printf("Play atleast one game to generate history!\n\n");
}
//returns the number of accounts read
int readAccountsFile(char filename[], struct account accounts[]) {
FILE *infile;
struct account read;
infile = fopen(filename, "r");
if (infile == NULL)
{
return 0;
}
int count = 0;
while (fread(&accounts[count], sizeof(struct account), 1, infile)) {
count++;
}
fclose(infile);
return count;
}
int writeAccountsFile(char filename[], struct account accounts[], int count) {
FILE *outfile;
outfile = fopen(filename, "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit(1);
}
for (int i = 0; i < count; i++) {
fwrite(&accounts[i], sizeof(struct account), 1, outfile);
}
if (fwrite != 0){}
else
printf("error writing file !\n");
fclose(outfile);
}
//returns accounts array index of registered user if login sucessfull else -1
int login(struct account accounts[], int n) {
char username[50], password[50];
printf("Enter Username: ");
gets(username);
printf("Enter Password: ");
gets(password);
for (int i = 0; i < n; i++) {
if (strcmp(accounts[i].username, username) == 0 && strcmp(accounts[i].password, password) == 0) {
return i;
}
}
return -1;
}
//returns total number of accounts after registering or -1 if registration failed
int registerUser(struct account accounts[], int n) {
char username[50], password[50];
printf("Enter Username: ");
gets(username);
printf("Enter Password: ");
gets(password);
for (int i = 0; i < n; i++) {
if (strcmp(accounts[i].username, username) == 0 ){
printf("username exists\n");
return -1;
}
}
//initialize array
for (int j = 0; j < 50; j++) {
for (int k = 0; k < 5; k++) {
accounts[n].history[j][k] = 0;
}
}
strcpy(accounts[n].username, username);
strcpy(accounts[n].password, password);
accounts[n].games = 0;
return n + 1;
}
void game(struct account accounts[], int id) {
char choice;
int c=0;
srand(time(0));
while (1)
{
//clear input buffer
while ((c = getchar()) != '\n' && c != EOF) {}
printf("A, B or C? :");
choice = getchar();
choice = toupper(choice);
int ichoice = choice - 'A';
//input validation
if (ichoice > 2 || ichoice < 0) {
printf("Enter A, B or C only!:\n");
continue;
}
int comp = rand() % 3;
printf("Computer choose %c and you choose %c\n",'A' + comp, choice);
if (ichoice == comp) {
accounts[id].history[accounts[id].games][3]++;
printf("Draw\n");
}
else if (ichoice == 0 && comp == 2) {
//player win
accounts[id].history[accounts[id].games][1]++;
printf("You win\n");
}
else if (ichoice == 2 && comp == 1) {
//player win
accounts[id].history[accounts[id].games][1]++;
printf("You win\n");
}
else if (ichoice == 1 && comp == 0) {
//player win
accounts[id].history[accounts[id].games][1]++;
printf("You win\n");
}
else {
//computer wins
accounts[id].history[accounts[id].games][2]++;
printf("Computer wins\n");
}
//increment no. of games
accounts[id].history[accounts[id].games][0]++;
//update stats
if (accounts[id].history[accounts[id].games][2] > accounts[id].history[accounts[id].games][1]) {
accounts[id].history[accounts[id].games][4] = -1;
}
else if (accounts[id].history[accounts[id].games][1] > accounts[id].history[accounts[id].games][2]) {
accounts[id].history[accounts[id].games][4] = 1;
}
else {
accounts[id].history[accounts[id].games][4] = 0;
}
printf("-1 to quit, anything else to continue: ");
scanf("%d", &c);
if (c == -1) {
accounts[id].games++;
printf("Game ended\n");
break;
}
}
}
int main() {
//array for storing all the accounts in the datafile
struct account accounts[50];
int choice = 0, loggedin = -1, ntemp;
char c;
//call function to read accounts from data file into accounts array and return the number
//of accounts read
int n = readAccountsFile("accounts.txt", accounts);
//login or register loop
while (1)
{
printf("1. Login\n2. Register\n");
scanf("%d", &choice);
//clear input buffer
while ((c = getchar()) != '\n' && c != EOF) {}
switch (choice)
{
case 1:
loggedin = login(accounts, n);
break;
case 2:
ntemp = registerUser(accounts, n);
if (ntemp >=0) {
n = ntemp;
loggedin = n - 1;
//write data file after every registration of account
writeAccountsFile("accounts.txt", accounts, n);
}
break;
default:
continue;
}
if (loggedin >= 0) {
printf("Logged in sucessfully! \n");
break;
}
else {
printf("Log in failed! \n");
}
}
//menu after login or register
while (1)
{
printf("1. Start new game\n2. Review game history\n3. Clear game history\n4. Logout\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
game(accounts, loggedin);
//write data file after every game
writeAccountsFile("accounts.txt", accounts, n);
break;
case 2:
printHistory(accounts,loggedin);
break;
case 3:
//clear history
accounts[loggedin].games = 0;
printf("Your game history has been cleared.\n");
//write to data file after clearing history
writeAccountsFile("accounts.txt", accounts, n);
break;
case 4:
return 0;
break;
default:
break;
}
}
return 0;
}
Output

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 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...
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...
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...
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...
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 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 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...
*3.17 (Game: scissor, rock, paper) Write a JAVA program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws ** Have to...
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's choice? To solve this, we will use a "random number generator", which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually "thinking" and making its own decisions. This is the largest, most involved program...