Sei sulla pagina 1di 5

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 Labyrinth 2821, a game


that takes place in a rectangular maze 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. Besides 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
field by default, but can load a custom maze from a file, and
save/load the current game state.
Please visit http://homeworkhelpace.com/computer-science-
homework-help/ for more details
Question 1: The Map
The program maze.c should maintain a two-dimensional array of
single-byte characters for representing the maze.
To this end, declare a global pointer variable char *map and
allocate some memory for it to point to in your main
function. Write the following functions to help with accessing the
individual elements of your array:
char get(unsigned x, unsigned y) returns the character stored at
position (x,y) in the buffer pointed to by map.
void put(unsigned x, unsigned y, char v) stores the character v
at position (x,y) in the array.
void print_map() 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. You can use putchar to print
a single character. To test your code,
allocate enough space for a 5 12 matrix and then initialise it to
consist of spaces, surrounded by walls,
represented by # characters. Use put to store an asterisk in the
top left corner of the empty space,
which represents a piece of treasure. Print the initialised map to
the terminal using print_map(), which
should output:

Question 2: Game Objects


The player character and the monsters are represented as game
objects. Each is identified 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, M for monsters). The monsters are stored
in a linked list.
Declare the following types:
struct game_object to represent monsters or the player.
struct list_node as a node in a singly linked list of game objects.
Consider how to best include the game_object as
a payload of the list node (as a pointer or directly as a value?).
Write the following functions:
void put_object(struct game_object *obj) places an object onto
the map at the cor- rect position and using its
type character.
void add_monster(struct list_node **list, unsigned x, unsigned
y)addsanew node to the front of the list passed as
first argument to represent a monster at position (x,y). The list
argument is passed as a pointer to the head pointer
(hence the **) to allow setting a new head.

Also Read: http://homeworkhelpace.com/urgent-homework-help/

void free_list(struct list_node *iter) to free all memory occupied


by the list objects.
In your main function, add code to create a player game object
and a list with two monsters using add_monster.
Place all of them into the map using put_object. The field should
now be printed similar to this:

Question 3: Game Loop and Input


The game proceeds in rounds. In each round the player can move
by writing a, w, s, d. Monsters move one step in
any of 4 directions at random. Treasure chests never move. No
object must ever be able to move into walls. The
player can exit the game by entering x.
The playing field is printed in every round to show the current
status of the game. Every game object should be
shown only at its current position.
If the player is in the same field as a monster, the player loses. If
the player is in the same field 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 check whether there are still chests left). After winning / losing,
the game should terminate.
To create randomness for moving the monsters, you can use the C
standard library method rand(), which is
defined in stdlib.h. rand() returns a random integer, so to get a
random value in a particular range, you can use the
modulo operation. E.g., to get a value from 0 to 7, use rand() % 8.
At the start of your program you should initialise
the random number generator with a fresh random seed to
prevent deterministic games using srand(), for
example by using srand(time(NULL)); with the time method from
time.h.
Below is an example of a game as it might be played over 6
rounds. For saving space, here we show the last three
rounds to the right instead of below the first three.
You can use the following methods to break down your game logic
into functions:
move_object takes a pointer to a game object and a direction as
argument. It checks whether the move is legal
and adjusts the objects coordinates accordingly. If there is
another non-wall character on the target field, it
returns that.
update_game interprets the users movement input (e.g., a, w,
s, or d) and moves the player character. It then
iterates over the list of monsters and moves all of them randomly.
It returns the new state of the game, i.e.,
whether the player has won, lost, or none of the two.
You can read the user input line by line, simply using scanf or
similar functions in a loop. If you would like to avoid
the player having to press return after every character, please see
the Extras section of this assignment.
Question 4: Saving and Loading
To make the game more interesting, add a mechanism for loading
predefined mazes, as well as saving and loading
the game state so the player can go back to a previous game or
reload if they were eaten by a monster. Add the
following methods (choose types as you see fit):
save_map(const char *file_name) saves the current game state
as a simple text file. When opened in a text
editor, the game state should look just like the game on the
console (Windows notepad may not show the line
breaks correctly, though).
load_map(const char *file_name) loads a saved game or map
from the specified file. Example mazes are
available on Moodle.
parse_map iterates over the current map and initialises the
monster list and the position of the hero according to
the characters stored in the array. Call this after loading a map
from a file to make sure the game is in a welldefined
state. Do not forget to properly free the previous list so your
program does not leak memory.
If the player enters k as a move, the game state should be
saved to a file savegame.txt; if the player enters l,
the game state should be loaded from that file. If the game is
started with a file name as command line argument,
the game state should be loaded from that file. For instance, you
can play one of the example maps from Moodle
using ./maze map1.txt.

Potrebbero piacerti anche