C Programming Homework Help - Labyrinth 2821
It's a c programming assignment. Please follow the directions to the assignment very carefully and precisely and do exactly what it's told nothing extra, and I want the code as simple as it can be and bare in mind that it's a student level not an expert. Hope it's plagiarism free. The goal of this assignment is to build “abyrinth “abyrinth !"!#$, a game that ta%es place in a rectangular ma&e drawn on the terminal. There is a single player character who can be moved around by typing in either of a, w, s, or d, for west, north, south, or east, respectively. esides the player, there are monsters, treasure chests, and walls. The goal of the game is to reach a treasure chest while avoiding monsters. The game creates a simple empty playing (eld by default, but can load a custom ma&e from a (le, and save)load the current game state. Please visit http*))homewor%helpace http*))homewor%helpace.com)computer .com)computer+science+ +science+ homewor%+help) for homewor%+help) for more details Question 1: The Map The program ma&e.c should maintain a two+dimensional array of single+byte characters for representing the ma&e. To To this end, declare declare a global pointer variable char map and allocate some memory for it to point to in your main function. -rite the following functions to help with accessing the individual elements of your array* char get/unsigned x, unsigned y0 returns the character stored at position /x,y0 in the bu1er pointed to by map. void put/unsigned x, unsigned y, char v0 stores the character v at position /x,y0 in the array. void print2map/0 iterates through the array and prints all its characters to the screen, row by row. This function will be used to print the game state. 3ou can use putchar to print a single character. To test your code, allocate enough space for a 4 5 #! matrix and then initialise it to consist of spaces, surrounded by walls, represented represented by “6$ characters. 7se put to store an asteris% in the top left corner of the empty space,
which represents a piece of treasure. Print the initialised map to the terminal using print2map/0, which should output*
Question 2: ame !b"e#ts The player character and the monsters monsters are represented represented as game game ob8ects. 9ach is identi(ed by an x and y coordinate and its type. The type is most easily represented as a single character, which you can also use to write it into the map /: for the player, ; for monsters0. The monsters are stored in a lin%ed list. 0. -rite the following functions* void put2ob8ect/struct game2ob8ect ob80 places an ob8ect o b8ect onto the map at the cor+ rect position and using its type character. void add2monster/struct list2node list, unsigned x, unsigned y0addsanew node to the front of the list passed as (rst argument to represent a monster at position /x,y0. The list argument is passed as a pointer to the head pointer /hence the 0 to allow setting a new n ew head.
?lso @ead* http*))homewor%helpace.com)urgent+homewor%+help) void free2list/struct list2node iter0 to free all memory occupied by the list ob8ects. In your main function, add code to create a player game ob8ect and a list with two monsters using add2monster.
Place all of them into the map using put2ob8ect. The (eld should now be printed similar to this*
Question $: ame Loop an% &nput The game proceeds proceeds in rounds. In each round round the player can move by writing a, w, s, d. ;onsters move one step in any of A directions at random. Treasure chests never move. Bo ob8ect must ever be able to move into walls. The player can exit the game by entering x. The playing (eld is printed printed in every round to to show the current current status of the game. 9very game ob8ect should be shown only at its current position. If the player is in the same (eld as a monster, the player loses. If the player is in the same (eld as a treasure chest, the player wins. If a monster moves onto a treasure chest, the chest is destroyed and disappears /you do not have to chec% whether there are still chests ch ests left0. ?fter ?fter winning ) losing, the game should terminate. To To create randomness randomness for moving the monsters, you can can use the = standard library method rand/0, which is de(ned in stdlib.h. rand/0 returns a random integer, so to get a random value in a particular range, you can use the modulo operation. 9.g., to get a value from f rom C to D, use rand/0 E ". ?t the start of your program you should initialise the random number generator with a fresh random seed to prevent deterministic deterministic games using srand/0, for example by using srand/time/B700F with the time method from time.h. elow is an example of a game as it might be played over G rounds. or saving space, here we show the last three rounds to the right instead of below the (rst three.
3ou 3ou can use the following methods methods to brea% down down your game logic into functions* move2ob8ect ta%es ta%es a pointer to a game ob8ect and a direction as argument. It chec%s whether the move is legal and ad8usts the ob8ects coordinates accordingly. If there is another non+wall character on the target (eld, it returns that. update2game interprets the users movement input /e.g., / e.g., a, w, s, or d0 and moves the player character. character. It then iterates over the list of monsters and moves all of them randomly. randomly. It returns the new state of the game, i.e., whether the player has won, lost, or none of the two. 3ou 3ou can read the user input input line by line, simply simply using scanf or similar functions in a loop. If you would li%e to avoid the player having to press return after every character, please see the “9xtras$ section of this assignment. Question ': (a)ing an% Loa%ing
To To ma%e the game more more interesting, interesting, add a mechanism for loading prede(ned ma&es, as well as saving and loading the game state so the player can go bac% to a previous game or reload if they were eaten by a monster. ?dd the following methods /choose types as you see (t0* save2map/const char (le2name0 saves the current game state as a simple text (le. -hen opened in a text editor, the game state should loo% 8ust li%e the game on the console /-indows notepad may not show the line brea%s correctly, though0. load2map/const char (le2name0 loads a saved game or map from the speci(ed (le. 9xample ma&es are available on ;oodle. parse2map iterates over the current map and initialises the monster list and the position of the hero h ero according to the characters stored in the array. =all this after loading a map from a (le to ma%e sure the game is in a wellde(ned state.