Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

must be done in C You are going to create a text based game. The game begins wit

ID: 3730708 • Letter: M

Question

must be done in C You are going to create a text based game. The game begins with a player. 1. The player has 4 lives. 2. The player has no money. 3. Strength equals 0. 4. Attribute of a player: name, lives, money, and strength Choices in the game. 1. Battle a. You will simulate a battle by using a random chance of 50/50. b. Half the time you win a battle the other half you lose. c. Your odds of winning a battle increases by 5 strength points for each battle you win. (ALway have a 20% chance of losing) d. Lose a battle you lose a life and strength is reduced by 5 strength points. (Play alway has at least a 20% chance of winning) e. Win a battle you get 50 dollars (you also get strength...) 2. Buy something a. strength 50 dollars for 5 strength b. a life 100 dollars for one life 3. Display the result of all battles as follows: a. Number of wins b. Number of losses c. Longest winning streak d. Longest losing streak 4. Display top 10 previous high scores (shown highest to lowest) a. Show the most money EVER at GAME OVER. 5. Quit Game should always DISPLAY remaining life, strength, & amount of money. Game ends when you run out of life (zero lives remaining). The High score is for accumulating the most money. Tasks = Points ---------------------- ------ Design Tool & comments 10 Use of Structs 10 Menu System 10 Battles 20 Buy Something 10 Display Battle Results 20 High Scores 10 File Input and Output 10 ---------------------- ------ Total 100

Explanation / Answer

//UPDATED NO BUGS

//I have included the explanation of steps in comments like these. If you found this helpful, please leave a like. I have included a name column in Highscores table
#include <stdio.h>
#include <stdlib.h>

struct playerinfo
{
char name[30];
int lives;
int money;
int strength;
};

struct scoreboard
{
char name1[30];
int highscore;
};
int main()
{
struct scoreboard scores[10];
int tt;
for(tt=0;tt<10;tt++)
{
scores[tt].highscore=0;
strcpy(scores[tt].name1,"NIL");
}
FILE *fp;
fp=fopen("Highscores.txt","rb");
if(fp == NULL)
{
printf("Highscores.txt not found ");
}
else
fread(scores,sizeof(struct scoreboard),10,fp); //Reading highscores from Highscores.txt
fclose(fp);
struct playerinfo player1;
player1.lives=4;
player1.money=0;
player1.strength=0;
int nwin=0,nlos=0,winst=0,losst=0,streak=0,sit=1,mmoney=0,res,tres,r,t;
int gap=300;
int middle=17767;
int randscore,cutoff;
srand(2767);
printf("Enter Player Name:");
scanf(" %[^ ]s",&player1.name);
do
{
printf(" Choose Your Action 1.Battle 2.Buy 3.Display Results 4.Display High Scores 5.Quit Game Enter the number corresponding to your action:");
scanf("%d",&res);
switch(res)
{
case 1:
r=time(0)%20; // Randomizing outcomes
for(t=0;t<r;t++)
rand();
randscore=rand();
cutoff=middle+(gap*(player1.strength));
if(cutoff>26767)
cutoff=26767;
else if(cutoff<8767)
cutoff=8767;
if(randscore<=cutoff) //if battle is won
{
player1.money+=50;
player1.strength+=5;
nwin++;
if(player1.money>mmoney)
mmoney=player1.money;
if(sit==-1)
{
if(streak>losst)
losst=streak;
streak=1;
sit=1;
}
else
{
streak++;
if(streak>losst)
losst=streak;
}
}
else // if battle is lost
{
player1.lives-=1;
player1.strength-=5;
nlos++;
if(sit==1)
{
if(streak>winst)
winst=streak;
streak=1;
sit=-1;
}
else
{
streak++;
if(streak>winst)
winst=streak;
}
}
break;
case 2:
printf(" What do you want to buy? 1.Strength ($50 for 5 strength) 2.Life ($100 for 1 life) Response:");
scanf("%d",&tres);
switch(tres)
{
case 1:
player1.money-=50;
player1.strength+=5;
break;
case 2:
player1.money-=100;
player1.lives+=1;
break;
default:
printf(" Invalid Response");
}
break;
case 3:
printf(" Number of wins:%d Number of losses:%d Longest winning streak:%d Longest losing streak:%d ",nwin,nlos,winst,losst);
break;
case 4:
printf(" HIGHSCORES ");
int k;
for(k=0;k<10&&scores[k].highscore!=0;k++) //Only non zero highscores will be displayed
printf("%d.%s %d ",k+1,scores[k].name1,scores[k].highscore);
break;
case 5:
printf(" Lives:%d Strength:%d Money:%d ",player1.lives,player1.strength,player1.money);
break;
default:
printf("Invalid Response ");
}
}while(res!=5&&player1.lives>0);
if(res!=5)
printf(" Most Money:%d",mmoney);
int p;
for(p=0;p<10;p++)
{
if(mmoney>scores[p].highscore)
{
int q;
for(q=8;q>=p;q--)
{
scores[q+1]=scores[q];
}
scores[p].highscore=mmoney;
strcpy(scores[p].name1,player1.name);
break;
}
}
FILE *fp2;
fp2 = fopen("Highscores.txt", "wb");
if(fp2 == NULL)
{
printf("Error opening file ");
exit(1);
}
fwrite(scores,sizeof(struct scoreboard),10,fp2); // Writing new highscore to Highscores.txt
fclose(fp2);
return 0;
}