C program Assignment RolePlaying Game (You can do this individually or on a self
ID: 3594465 • Letter: C
Question
C programAssignment RolePlaying Game (You can do this individually or on a self-formed team of 3 or less) 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. C program
Assignment RolePlaying Game (You can do this individually or on a self-formed team of 3 or less) 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. C program
Assignment RolePlaying Game (You can do this individually or on a self-formed team of 3 or less) 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.
Explanation / Answer
Please find the final programme.Please comment if any doubts. Please read comments in code
#include <stdio.h>
//Winning Rate default 50
#define DEFAULT_WINRATE 50
#define DEFAULT_LIVES 4
struct player{
char name[50];
int lives;
int money;
int strength;
int numwins;
int numLoss;
};
void flush()
{
int c;
while ((c = getchar()) != ' ' && c != EOF)
;
}
int main()
{
int numMaxScores = -1;
//Save recent 10 Max scores
int max_scores[10] ;
//run loop to play many games one at a Time , CTRL+C to exit the programme
while(1)
{
numMaxScores++;
if(numMaxScores == 10){
numMaxScores = 0;
}
//Create a player Object of structure
struct player p;
p.strength = 0;
p.money = 0;
p.numLoss = 0;
p.numwins = 0;
//Set default 4 lives
p.lives = 4;
//Ask user enter name , after enter name Game will start
printf("Enter your name or CTRL+C to exit the programme ");
if (!fgets(p.name, sizeof p.name, stdin)) {
exit(1);
}
//Run loop infinetely and ask user to enter choices until Exit option is entered
while(1)
{
printf("Enter your choic 1.Battle 2.Buy Something 3.Display Results 4. Display Top 10 scores 5.Quit Game ");
int choice = 0 ;
//Take choice from user
scanf("%d",&choice);
switch (choice) {
case 1:
{
//If no lives can't play , ask user to Buy lives
if(p.lives == 0){
printf("No lives . Please Buy or end game ");
continue;
}
//Set winning percentage based on current strength
int winRate = DEFAULT_WINRATE + p.strength;
//Win perecentage should be <= 80 and >= 20
if(winRate > 80){
winRate = 80;
}
else if(winRate < 20){
winRate = 20;
}
/**
* To simmulate percentage based winning
* Generate a random number between 1 to 100 , if it is < the winning percentage then you win otherwise you are loose
*/
srand(time(0));
int r = ((rand() % 100) + 1);
int win = ( r < winRate) ? 1 : 0;
//If win
if(win)
{
printf("You Win ");
p.strength += 5;
p.money += 50;
p.numwins++;
}
else{
printf("You Loose ");
p.lives--;
p.strength -= 5;
p.numLoss++;
}
}
break;
case 2 :
{
//Get user choice to Buy 1.Strength 2.Life
printf("Enter choice to Buy 1.Strength 2.Life ");
int buych ;
scanf("%d",&buych);
//Strenght
if(buych == 1)
{
//Can't Buy strength if points < 50
if(p.money >= 50){
p.strength += 5*(p.money / 50);
p.money -= 50;
}
}
//Life
else if(buych == 2){
//Can't Buy life if points < 100
if(p.money >= 50){
p.lives += 1*(p.money / 100);
p.money -= 100;
}
}
}
break;
//Display Results
case 3 :
{
//Print Number of wins
printf("No of wins :: %d " , p.numwins);
//Print Number of losses
printf("No of Loss :: %d " , p.numLoss);
//TODO : Longest winning streak
//TODO : Longest loosing streak
}
break;
case 4 :
{
//Print previous Max 10 scores
printf("Max 10 scores :: ");
//Sort
int min;
for (int i = 0; i < 10; ++i)
{
for (int j = i + 1; j < 10; ++j)
{
if (max_scores[i] > max_scores[j])
{
min = max_scores[i];
max_scores[i] = max_scores[j];
max_scores[j] = min;
}
}
}
for (int i = 0; i < 10; ++i)
{
if(max_scores[i] != 0)
printf("%d ", max_scores[i]);
}
}
break;
//Quit game
case 5 :
{
//Store score in Max score array
max_scores[numMaxScores] = p.money;
printf("Game Over. ");
//Print remaining lives
printf("Remaining Lefes :: %d " , p.lives);
//Print Strength
printf("Strength :: %d " , p.strength);
//Amount of Money
printf("Amount of Money :: %d " , p.money);
}
break;
default:
break;
}
if(choice == 5){
flush();
break;
}
}
}
return 0;
}
Output :-
----------------
Enter your name or CTRL+C to exit the programme
1
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
1
You Win
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
1
You Win
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
1
You Win
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
1
You Win
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
1
You Win
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
3
No of wins :: 5
No of Loss :: 0
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
2
Enter choice to Buy
1.Strength
2.Life
2
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
2
Enter choice to Buy
1.Strength
2.Life
1
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
2
Enter choice to Buy
1.Strength
2.Life
2
Enter your choic
1.Battle
2.Buy Something
3.Display Results
4. Display Top 10 scores
5.Quit Game
5
Game Over.
Remaining Lefes :: 7
Strength :: 40
Amount of Money :: 0
Enter your name or CTRL+C to exit the programme
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.