Castle Defense Attention brave and noble engineers! Castle CSE is under attack by Enemies from Frankie the Fox's Lair. Queen Chicken, First of Her Name, Protector of the Realm, Lecturer in Charge for...


Castle Defense


Attention brave and noble engineers! Castle CSE is under attack by Enemies from Frankie the Fox's Lair. Queen Chicken, First of Her Name, Protector of the Realm, Lecturer in Charge for COMP1511, has commissioned you to build Her defenses and protect Her Castle.


In this assignment, you will be implementing Castle Defense, a program that simulates an imaginary Realm that is under attack. You will be creating Lands in the Realm as well as building Towers to defend a Castle.


The Enemies will spawn from their Lair and move towards the Castle through the Lands. As the Enemies move, they will be attacked by the towers, which both harms the Enemies and depletes the towers. If the Enemies reach the Castle, they will damage it.


Your program will be managing Enemy movement as they pass through the Lands. It will also be managing the use of Towers against the Enemies and any damage the Castle receives.


Note:If this assignment appears daunting, fear not! The Queen's chosen human (Marc in lectures) will be demonstrating many of the techniques necessary to begin your tasks. Her loyal subjects (the Tutors) will also be teaching some of the techniques in detail in Labs. During Week 8, a great deal of the course will be dedicated to helping you get started with this assignment.


The Realm


The realm is a struct that contains all of the objects that will be used in this assignment. Those are: Lands, The Lair, The Castle, Towers and Enemies. Your job is to manage the Realm, and everything it contains.Image: Realm Structure


The Realm struct has a pointer to the Castle, whence Queen Chicken and her Loyal subjects look down upon their kingdom. It also has a pointer to the lair, from which Frankie the Fox sends his feindish Enemies. Every location in the Realm lies on a linked list that starts at The Castle, and goes down to The Lair. Locations can have one of four types:




  • Castle: The Castle, which is always the first node in the linked list. If the Enemies can reach The Castle, they will deal damage to it.


  • Land: A place where Enemies can gather.


  • Tower: A defense, built by you, to attack Enemies


  • Lair: The place from which Enemies are sent by Frankie



Your code should ensure that at the start of the program, the Castle points to the Lair, and the Lair points toNULL. Your code should then ensure that the Castle is always connected (through other locations) to the Lair; and that the Lair points toNULL.


To ensure this is true, you will never be given a test case where you are asked to put a tower after the Lair. (More details on adding towers later)


As an additional restriction to make the assignment's rules simpler, there will never be two locations with the same name.


Enemies


In these dark times, the realm is under attack. Fiendish Enemies regularly appear at their Lair and make their way, one location at a time, towards The Castle.


Image: Enemies

An Enemy is represented by a struct which records their name, their maximum health and their current health (as integers). These structs should also be set up so that the Enemies can be joined together in a linked list.


Enemies will always be at a location, which is represented by a linked list of Enemies at every location in The Realm. Linked lists of Enemies are always listed in alphabetical order.


An Enemy dies when it has zero or less health. It then is removed from the linked list that contained it and its memory is freed.


There will never be two enemies in the realm with the same name.


Towers


Image: Tower and Location detail

As mentioned before, locations in the realm can be Towers. Towers are able to reduce the HP of any Enemy at their Location. They can only be used a fixed number of times. When they run out of uses, they convert back into lands.


Towers have two important properties:




  • Power: How much an Enemy's HP is reduced when the Tower attacks. This is a whole number, greater than zero.


  • Uses: A count of how many times a Tower can be used before it reverts to being a land. This is a whole number, greater than zero.


The Castle


At the start of the list is The Castle. Enemies that manage to reach The Castle will deal damage to it; and if they move beyond the castle they are removed from the realm.


The Castle starts off withSTARTING_CASTLE_HPhealth. If an Enemy damages The Castle, it causes The Castle's health to decrease by that Enemy's remaining health.

Note:Nothing special should happen when the castle goes below 0 HP. It can go negative if enough damage is dealt to it.

The Game


When the game starts, two locations are automatically created - the Castle and the Lair. You will then be given a list of locations on standard input, one per line. The order of the locations as standard input is the same order they will have starting from Castle and proceeding to Lair. These locations will be strings that do not contain any spaces, dashes or square brackets. When the list of locations is complete, an empty line will be input.


After this, a prompt will appear. You will type in commands to indicate changes to the state of the Realm. These commands will all start with a character as the command, then a space, then possibly some additional information about the command. There are three special commands, which are already implemented in the Starter Code provided. They consist of:




  • ?: Print a list of all possible commands.


  • q: Quit the program.


  • /: Do nothing, this line is treated as a comment.


Your Task: Implementation


Your task for this assignment is to write the functions that will manage the state of the Realm. All of these functions are contained inside the filerealm.c. You will also have to write functions intest_realm.cto check your code works.


Starter Code


Download the starter code (castle_defense.zip) hereor copy it to your CSE account using the following command:

1511 setup-castle-defense



The starter code consists of the following files:




main.c
contains the main function for the assignment. It will scan the program's input, then call the functions you will write.You must not change this file.




realm.h
contains the definitions of all the functions you will be writing. It also contains useful constants and type definitions.You must not change this file.




realm.c
contains empty functions which you will need to implement. It already contains some functions. It isstrongly recommendedthat you use these.This file is where you will write your own code.




test_realm.h
contains the prototypes of the testing functions intest_realm.c.You must not change this file




test_realm.c
contains an alternative main function as well as template code that you will modify to make your own tests.This file is where you will write your own test cases.




capture.h
contains the definition of a function that allows you to capture the output of your own code, to use in testing. It can only be used intest_realm.c, and will not be available inrealm.c.You must not change this file.




capture.c
contains the implementation of functions that allow you to capture the output of your own code, to use in testing.You must not change this file.


To run your code interactively, you should use the command:



dcc -o castle_defense realm.c main.c
./castle_defense

To run your tests (written intest_realm.c), you should use the command:



dcc -o test_castle realm.c test_realm.c capture.c
./test_castle

Allowed C Features


In this assignment, there are two restrictions on C Features:



  • You must follow the rules explained inthe Style Guide.

  • You may not use the functionfnmatch, or importfnmatch.h.


It is recommended to only use features that have been taught in COMP1511. Course work in Week 8 will have a particular focus on assistance for this assignment and you will not need any of the subject material from Weeks 9 or 10.


If you choose to disregard this advice, youmuststill follow the Style Guide. You also may be unable to get help from course staff if you use features not taught in COMP1511.


Input Commands


Input to your program will be via standard input (similar to typing into a terminal). Youdo notneed to read this input yourself. Your functions inrealm.cwill be called bymain.cwith the input already scanned in.


Your code should not print or scan from the terminal (standard input or output). Do not use or add anyprintforscanfinrealm.cunless it's temporary and for debugging purposes. All printing and scanning has already been implemented for you in the starter code.


You can assume that input will be handled for you, and that you will never be given an invalid argument to one of your functions (except as described below).


Details of how this input will relate to the functions you call are given below:


Stage One


Stage one involves adding new locations to the realm, as well as being able to print out the game state.


Adding to the Realm


Inrealm.cyou have been given two function stubs (a stub is an unimplemented function):


static Location new_location(char *name) {     return NULL; }  int add_location(Realm realm, char *name) {     return 42; }

These two functions should be implemented as described below.



new_locationshould use malloc to allocate memory for a new Location node. It should then set up it's name (and any other fields you decide you need throughout the assignment).



  • Note that the word "static" on this function will make it accessible within this file, but not elsewhere. We often use static on helper functions that are not part of the header file and aren't used by any other parts of the program. Static will not significantly change how you will use this function.

  • Thenew_realmfunction (already implemented in the starter code) uses thenew_locationfunction. Thenew_realmfunction does not need changing for this stage, but you may want to add code to it at a later stage.



add_locationwill be given a Realm and a Location name. It will then insert a new location node directly before theLairin the linked list of locations. It returns the number of locations in the linked list.


These functions will never receive invalid inputs.Image: Add Location


Printing Out the Realm


Inrealm.cyou have been given the function stub:


void print_realm(Realm realm) {  }
as well as four fully implemented functions:
void print_tower(char *name, int power, int uses, Effect effect); void print_land(char *name); void print_castle(char *name, int defense); void print_enemy(char *name, int cur_hp, int max_hp);

print_realmwill be given a realm, and will print out information about that realm. You should use the functions given in the starter code to print it out, instead of calling printf yourself.

Specifically,print_realmwill list locations in order from The Castle to The Lair. If there are any enemies at a location, it will list them in order before listing the next location.


Note:the castle starts off withSTARTING_CASTLE_HP, so in the early stages, you will need to passdefenseasSTARTING_CASTLE_HP. You will not need theprint_towerandprint_enemyfunctions until Stage 2.



Show Example: Stage 1 Example

Run the tests for this stage with:

1511 autotest-stage 01 ass2_castle_defense

Please note, the files you testmustbe named correctly, or this test will not work properly.

To pass all the tests for this stage, you will need to add tests intest_realm.cSeeWriting your own automated testsfor more info.


Stage Two


Stage Two has two new commands. They involve adding enemies to a location, and adding Towers to protect the realm.


Add Enemies

When running the program, you can use the following command to add a new enemy:
e location_name name HP // for instance e Lair EnemyName 4

Which means "Create a new enemy, at the locationlocation_name, calledname, with maximum HPhp.


You have been given the function stub, which will be called bymain.cwhen the above command has been given:


int new_enemy(Realm realm, char *location_name, char *name, int hp) {  }
Your job is to implement this function, such that the code does the following:

Given a realm, the name of a Location in the realm, and some stats about a new enemy, you should (in this order):



  • Find the Location calledlocation_name. If one does not exist, returnERROR_NO_LOCATION

  • Ensure that the stats you have been given for HP are not below 1. If they are, returnERROR_INVALID_STAT.

  • Place a new enemy, with the given stats, directly after the last enemy at the location you found above.

  • ReturnSUCCESSto indicate success.


You are guaranteed that you will only receive enemies in sorted order, and will never receive the same enemy name twice. You do not need to check this. This means that for any list of enemies,strcmp(enemy->name, enemy->next->name)will bestrictly less than zero. This guarantee will only be relevant for Stage 4.


New Towers

When running the program, you can use the following command to add a new tower:
t prev_name name power uses // for instance t Castle NewTower 2 2

Which means "Create a new tower, directly below the Locationprev_name, calledname, that causespowerdamage to each enemy at it, and that can be usedusestimes".

You have been given the function stub, which will be called bymain.c:
int new_tower(Realm realm, char *prev_name, char *name, int power, int uses){  }
Your job is to implement this function, such that the code does the following:

Given a realm, the name of a Location in the realm, and some stats about a new tower, you should:



  • Find the Location called prev_name. If one does not exist, returnERROR_NO_LOCATION

  • Ensure that the stats you have been given for power and uses are not below 1. If they are, returnERROR_INVALID_STAT.

  • Place a new tower, with the given stats, directly after the location which you found above. This tower is inserted into the linked list, which adds an element and does not replace the Location prev_name.

  • ReturnSUCCESSto indicate success.


Image: Adding a Tower


Show Example: Stage 2 - Printing Example With Towers/Enemies

Run the tests for this stage with:

1511 autotest-stage 02 ass2_castle_defense

Please note, the files you testmustbe named correctly, or this test will not work properly.

To pass all the tests for this stage, you will need to add tests intest_realm.cSeeWriting your own automated testsfor more info.

Aug 07, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here