Question

WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly...

WRITE IN LANGUAGE C. THANKS

Heroic Game

Write a program that plays a simple game. Mostly the purpose of the program is to practice using structs. Here is a struct that contains information about a character in the game:

typedef struct

{   int hitPoints;    /* how much life */   int strength;     /* fighting strength */   char name[MAXNAME];

} Hero;

The same structure is used for all characters (even non-heroic.) First write the function void printHero( Hero hr )that prints a Hero on the monitor. Note that this is call by value and that the value passed to this function is a complete struct. Here is a testing program:

void main()

{

Hero goodGuy = {10, 5, "Frodo" };

Hero badGuy = { 5, 7, "Grendel" };   printHero( goodGuy );   printHero( badGuy );

}

Next, implement the function void incrementStrength( Hero *hero, int inc ) which adds an increment amount to the strength in the Hero pointed to by the pointerparamenter hero. Note that this is call-by-value, but now the value is a pointer to a struct. Study the C-puzzles if this is not clear. Here is a testing program for that:

void main()

{

Hero goodGuy = {10, 5, "Frodo" };   incrementStrength( &goodGuy, 5 )   printHero( goodGuy );

}

Next, implement the function void incrementHitPoints( Hero *hero, int inc ) which adds an increment amount to the hitPoints of a Hero.  

Then write the function int identical( Hero a, Hero b ) which compares the data in two structs and return 1 if it is identical and 0 if not. This function is call-by-value with struct-values (not pointers.) You will need to use strcmp() from the string library.

Write the function int isAlive( Hero h) which returns 1 if the Hero is alive (hitPoints greather than zero) and 0 if not.

Write the function void fight( Hero *heroA, Hero *heroB ) which implements a fight between two heroes. The outcome of a fight is one hero or the other has their hitPoints decremented by one. Which hero’s points are decremented is determined by: pick a random number 1..heroA strength, and another random number 1..heroB strength. Which ever one is largest wins the fight, and the other looses a hitpoint. The function parameters are call-by-value-with-pointer, so the hitpoints in the caller’s struct can be changed. The function should write out an informative message for each round. Here is a testing function:

void main()

{

srand( time(NULL) );

Hero goodGuy = {10, 5, "Frodo" };

Hero badGuy = { 5, 7, "Grendel" };   printHero( goodGuy );   printHero( badGuy );

    fight( &badGuy, &goodGuy );   printHero( goodGuy );   printHero( badGuy );

  

}

If all of the above is working, implement the game. The game starts by initializing a good guy and a bad guy (as above, but change values if you want.) Then in a loop body while both Heroes are alive they fight, their status is printed, and the program is paused until the user hits “enter”. When one (or both) heroes die, print out the final results. There is no strategy in this game other than picking initial values for hitPoints and strength. There are many ways the game could be made more interesting, but do that on your own.

PS C:\Csource > gcc heroFun.c

PS C:\Csource > a

*** starting the game ***

     Frodo: hitPoints:   7, strength:   5

   Grendel: hitPoints:   4, strength:   7

Hit enter

    Frodo   4 ties Grendel   4

     Frodo: hitPoints:   7, strength:   5

   Grendel: hitPoints:   4, strength:   7

Hit enter

    Grendel   6 beats Frodo   5

     Frodo: hitPoints:   6, strength:   5

   Grendel: hitPoints:   4, strength:   7

Hit enter

    Grendel   4 beats Frodo   1

     Frodo: hitPoints:   5, strength:   5

   Grendel: hitPoints:   4, strength:   7

Hit enter

    Frodo   2 beats Grendel   1

     Frodo: hitPoints:   5, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

    Grendel   7 beats Frodo   3

     Frodo: hitPoints:   4, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

    Grendel   7 beats Frodo   1

     Frodo: hitPoints:   3, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

    Grendel   7 beats Frodo   4

     Frodo: hitPoints:   2, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

    Grendel   5 beats Frodo   3

     Frodo: hitPoints:   1, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

    Grendel   7 beats Frodo   5

     Frodo: hitPoints:   0, strength:   5

   Grendel: hitPoints:   3, strength:   7

Hit enter

Final Outcome:

Grendel wins!!

PS C:\Csource\CS153Source\prog09heroStruct>

Turn in one source file heroGame.c. You don’t need to do separate compilation for this program, although do factor it into functions as above. Not all of the functions are used in the final game, but include them anyway.

Start your source file with a small block of comments that gives the author and date and describes briefly what the program does. As always, avoid mixed tabs and spaces. Format the output nicely.   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Hero

{

public:

virtual ~Hero() {}

virtual const char* getAttack() = 0;

ptotected:

Hero(int startingStrength)

: strength_(startingStrength)

{ }

private

int strength_; // current strength.

// STARTING ANOTHER CLASS Breed

class Breed

{

public:

Breed (int strength, const char* attack)

: strength_(strength),

attack_(attack)

{ }

int getStrength() {return strength_; }

private:

int strength_; // starting strength.

const char* attack_;

};

//Container for two data field: the starting starting and the attack string.

//let's we can see how the hero ues this.

class Hero

{

public:

Hero (Breed& breed) : strength_(breed.getStrength()),

{ }

const char+ getAttack( )

{

return breed_ . getAttack( )

}

private :

int strength_ ;// current strength.

Breed& breed_;

} ;

// Here we define the hero's breed instead of subclasses. In the constuctor, Hero uses the breed to determine its //starting health. To get the attack string, the hero simply forwards the call to its breed.

//To access a breed's attribute, now we just return the field.

int gethealth( ) {return health_; }

const char* getattack() {return attack_; }

// Allow Hero to return its Breed and the implementation is as follows.

class Hero

{

public:

Breed& getBreed( ) { return breed_; }

// existing code...

};

// Change the hero's attack string when it's near to death

const char* hero :: getAttack()

{

if (strength_ < LOW_STRENGTH)

{

return " the hero is weak"";

}

return breed_ . getAttack();

}

Add a comment
Know the answer?
Add Answer to:
WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly...
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++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

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

  • Program in C computer language In this program you will be mimicking a game of Battleship....

    Program in C computer language In this program you will be mimicking a game of Battleship. Your game board is a line of 10 slots. You have to implement 3 methods. The following methods must be implemented: void setBoard(int *) This method sets up the human’s game board. The method prompts the user for 2 slots to place the “ship”. The program should ensure that the slots are consecutive. In other words, your ship cannot be placed at slot 2...

  • *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the...

    *********C Language******* Write a file final_main.c 4. Inside main(void): Write a loop that oops until the entered number is 0 or negative 5. Ask the user to enter a positive int between 0 and 10 inclusive 6. If the number is < 1, it terminates. 7. Else: call the above four functions 8. Print the result of each function after its call. Q2. Write a program that removes the punctuations letters from a file and display the output to the...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Summary task: C++ language; practice combining tools(functions, arrays, different kind of loops) to solve somewhat complex...

    Summary task: C++ language; practice combining tools(functions, arrays, different kind of loops) to solve somewhat complex problem. Description A Valiant Hero is about to go on a quest to defeat a Vile Monster. However, the Hero is also quite clever and wants to be prepared for the battle ahead. For this question, you will write a program that will simulate the results of the upcoming battle so as to help the hero make the proper preparations. Part 1 First, you...

  • Plz help!! The programming language is C and could you write comments as well? Problem 2....

    Plz help!! The programming language is C and could you write comments as well? Problem 2. Calculate Grades (35 points) Arrays can be used for any data type, such as int and char. But they can also be used for complex data types like structs. Structs can be simple, containing simple data types, but they can also contain more complex data types, such as another struct. So you could have a struct inside of a struct. This problem deals with...

  • Write a program (C++) that shows Game sales. The program should use a structure to store...

    Write a program (C++) that shows Game sales. The program should use a structure to store the following data about the Game sale: Game company Type of Game (Action, Adventure, Sports etc.) Year of Sale Sale Price The program should use an array of at least 3 structures (3 variables of the same structure). It should let the user enter data into the array, change the contents of any element and display the data stored in the array. The program...

  • The project in C is to create a game in which a player goes through a...

    The project in C is to create a game in which a player goes through a series of rooms in which they can find a prize or a monster to fight. Game Rules: • A player will enter the number of rooms (greater than 1) they want to endure. The program will dynamically allocate an array of Rooms and populate it with prizes and monsters. Then, the player will enter each room in which an action will occur: o If...

  • Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program sh...

    Please write this program in C++, thanks Task 9.3 Write a complete C++ program to create a music player Your program should read in several album names, each album has up to 5 tracks as well as a genre. First declare genre for the album as an enumeration with at least three entries. Then declare an album structure that has five elements to hold the album name, genre, number of tracks, name of those tracks and track location. You can...

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