Question

This is a multipart question In C language: You are to design a fight game between...

This is a multipart question

In C language:

You are to design a fight game between Neo versus Smith. The program needs to be organized using at least three functions. The specifications are as follows:

  1. The program should first call a password function.
    1. Request a password, multiple characters followed by return.
    2. For every character entered, display * character.
    3. The correct password is EGC251.
    4. If the correct password is entered, proceed to the game.
    5. If a wrong password is entered, prompt the user with a statement indicating the password is incorrect and prompt the user for a correct password. If the second password is also incorrect, prompt the user that this is the last attempt. If the third password is entered incorrectly, ignore further attempts.
  1. The game consists of three rounds. At each level, a new health-level is calculated. At the end of the third round, the one with the higher health level is announced the winner.
  2. Initially, value of health-level is randomized between 20.0 and 24.99.
  3. During each round, two sequential integer inputs punch_N and punch_S between 2 and 5 are accepted.
  4. Following each punch, the damage (in float) to the opponent is calculated using

damage_S = punch_N * (health_level_S*randomized(0.05 - .1)

damage_N = punch_S * (health_level_N*randomized(0.05 - .1)

  1. Using individual damage, a new health level is calculated using

health_S = health_S * skill– damaged_S

health_N = health_N * skill – damaged_N

Note that skill is a float variable which needs to be randomized between 0.9 and 1.1 each time it is used.   

  1. If the health level of a fighter drops below 0.5, the fight is over by a knock out.
  2. Otherwise, proceed to the next round.
  3. After three rounds using the health_level of fighters, determine the winner.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the program code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<termios.h>
#include<unistd.h>
#include<string.h>

//function declarations
int checkPassword();
int getch();
void startGame();
float generateHealth();
void startRound(float*, float*);
float getRandomDamage();
float getRandomSkill();
int checkKnockOut(float, float);

int main()
{
   srand(time(0)); //use current time to seed random number generator
  
   if(checkPassword() == 0)
   {
       printf("Welcome to the game.....\n");
       startGame();
   }
}

int checkPassword()
{ /* this function accepts a password and verify if its correct or not
returns 0 if password matches
else returns 1 after 3 failed attempts*/
   int chances = 3; //chances to enter password
   char truePassword[] = "EGC251";
   while(chances>0)
   {
       chances--;//decrease the chances
       printf("Enter password: ");
  
       char password[55];
       int p=0;
   do{
   password[p]=getch(); //getch() is used to accept a character but not print it on screen
   if(password[p]!='\n'){ //check if return is pressed or not
   printf("*"); //print * on screen
   }
   p++;
   }while(password[p-1]!='\n'); //if last character is return, then exit loop
   password[p-1]='\0';
  
   printf("\n");
   if(strcmp(truePassword,password) == 0) //compare the string
   {
       return 0;
       }
       else
       printf("Wrong password.\n");
   }
  
   printf("All 3 attempts are over.\n"); //display message after 3 failed attempts
   return 1;
}

void startGame()
{
int res = 0, rounds = 3, i;
float neoHealth = generateHealth(); //initialize Neo's health
float smithHealth = generateHealth(); // initialize Smith's health
  
printf("\nHealth at the start:\n");
printf("Neo\t\tSmith\n");
printf("%f\t%f\n", neoHealth, smithHealth);
  
for(i=1; i <= rounds; i++)
{
startRound(&neoHealth, &smithHealth);
printf("\nHealth after Round %d:\n", i);
printf("Neo\t\tSmith\n");
printf("%f\t%f\n", neoHealth, smithHealth);
  
res = checkKnockOut(neoHealth,smithHealth); //check for knockout scenarios
if(res!=0)
return; //move out of the loop and function as someone is knocked out
}
  
//check health after 3 rounds result in no knockout
if(neoHealth > smithHealth)
printf("Neo wins with more health\n");
else if(smithHealth < neoHealth)
printf("Smith wins with more health\n");
else
printf("Match tied\n");
}

float generateHealth()
{
/*this function returns random health value from 20 to 24.99*/
float x = 20 + (float)rand() * (4.99f/RAND_MAX);
return x;
}

void startRound(float *hN, float *hS)
{
/*this function starts a round.
It accepts two health parameters as pass by reference.
It calculates damage dealt and health changes
*/
int punchN=0, punchS=0;
float damageN, damageS;
while(!(punchN>=2 && punchN<=5))//loop to accept valid punches only
{
printf("Enter punchN (2-5): ");
scanf("%d",&punchN);
}
while(!(punchS>=2 && punchS<=5))//loop to accept valid punches only
{
printf("Enter punchS (2-5): ");
scanf("%d",&punchS);
}
  
//calculate the damages
damageS = punchN * (*hS) * getRandomDamage();
damageN = punchS * (*hN) * getRandomDamage();
  
//calculate new health values.
*hN = (*hN) * getRandomSkill() - damageN;
*hS = (*hS) * getRandomSkill() - damageS;
}

float getRandomDamage()
{
/*this function returns random damage value from 0.05 to 0.1*/
float x = 0.05 + (float)rand() * (0.05f/RAND_MAX);
return x;
}

float getRandomSkill()
{
/*this function returns random skill value from 0.9 to 1.1*/
float x = 0.9 + (float)rand() * (0.02f/RAND_MAX);
return x;
}

int checkKnockOut(float neoHealth, float smithHealth)
{
/*this function check for knockout scenarios
if a player is knocked out or both players have health <0.5 it returns 1
else it returns 0*/
if(neoHealth < 0.5f && smithHealth < 0.5f)
{
printf("Neo and Smith left unconscious. Fight Over.\n");
return 1;
}
  
if(neoHealth < 0.5f)
{
printf("Neo is knocked out. Smith wins.\n");
return 1;
}

if(smithHealth < 0.5f)   
{
printf("Smith is knocked out. Neo wins.\n");
return 1;
}
  
return 0;
}

int getch()
{
   /* reads from keypress, doesn't echo */
   /* taken form conio.h*/
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}

------------------------------------------------------------

Below is the code screenshot:

1 #include<stdio.h> 2 #include<stdlib.h> #include<time.h> #include<termios.h> 5 #include<unistd.h> 6 #include<string.h> 8 //f31 29 int checkPassword() 30{ /* this function accepts a password and verify if its correct or not returns @ if password matc58 } printf(All 3 attempts are over. In); //display message after 3 failed attempts return 1; 62 } 64 void startGame) 65 in//check health after 3 rounds result in no knockout if(neoHealth > smith Health) printf(Neo wins with more healthin); else115 while( ! (punchs >=2 && punchs<=5))//loop to accept valid punches only 116 117 printf(Enter punchs (2-5): ); 118 scanf(144 int check knockout(float neoHealth, float smith Health), 145 { 146 /*this function check for knockout scenarios 147 if a174 175 IIIL Belli 171 - { 172 /* reads from keypress, doesnt echo */ 173 /* taken fors conio.h*/ struct termios oldattr, ne

------------------------------------------------------------

Output Screenshot:

Enter password: **** Wrong password. Enter password: ****** Welcome to the game. ... Health at the start: Neo Smith 23.179728

**You may also check and run my code at: https://onlinegdb.com/HytulSEBL

The code is well commented and easy to understand. Start reading from main() function and you will easily get the flow of the program.

///////////////////////////////////////////////////////////////////

In case you find my answer helpful, please up-vote.

Please leave a feedback in comments. If you get any queries, ask in comments. Thank you.  

Add a comment
Know the answer?
Add Answer to:
This is a multipart question In C language: You are to design a fight game between...
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
  • This is a multipart question, needs to be in c language, and be able to compile...

    This is a multipart question, needs to be in c language, and be able to compile into a windows program, i.e. code blocks, specifically. In C language: You are to design a fight game between Neo versus Smith. The program needs to be organized using at least three functions. The specifications are as follows: The program should first call a password function. Request a password, multiple characters followed by return. For every character entered, display * character. The correct password...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • help me with the loops and the last part of this question .i am using c...

    help me with the loops and the last part of this question .i am using c language....speed=distance/time....in the 1st part of the program i had to write a program to calculate speed...we had to input distance in meters and time in seconds. 2. Strength. The robots will attempt to lift several heavy objects 3. Combat effectiveness. The robots will battle against 100 humans and then receive a score based on the number of victories. Create three functions (one for each...

  • Case Program 10 – 1: Part 1: Previously, you created a Contestant class for the Greenville Idol c...

    Case Program 10 – 1: Part 1: Previously, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field Fee that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

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