This project is a fun project with three serious goals,
NAME snail --- the snail gate game SYNOPSIS snail [-v] DESCRIPTION As the snail moves towards a gate, the user guesses the one digit key to the door. If the user guesses the key, the door opens (depicted by the ? in the gate disappearing) and the snail can pass through the gate. OPTIONS -v verbose ARGUMENTS None OUTPUT A graphic with a snail @/, an closed gate [?] and an open gate [ ]. HISTORY New for csc421-231.
The nag program, github://csc-courses/csc421/nag.c, is the basis for this project. The nag routine is a thread that from time to time wakes up and checks if something new has been said. If not it nags the user to say something. However if so, it insults the user for what they said.
Note in this program,
The nag and main threads communicates by two methods,
struct Board {
int snail_loc ;
char guess ;
int gate_loc ;
int gate_key ;
int gate_state ;
} ;
struct T_arg {
int req_exit ;
pthread_mutex_t * mutex ;
pthread_cond_t * cond ;
} ;
The board information is held in a global struct of type struct Board. The snail has advanced to integer position snail_loc. The main thread update guess with what the user has guessed. The get is located at gate_loc and is opened by the character gate_key. The gate_key takes one of two values, GATE_IS_CLOSED or GATE_IS_OPEN. This field is used when creating the string tha represents the game.
There are good reasons not to use globals. Keeping track of data is the most important thing in code, so keeping access to data focused is very helpful. The utility code, snail-util.c does not refer to board as a global but is passed in a reference to the global.
I also took this opportunity to exercise how a struct is accessed either directly with the dot or through a pointer with the arrow. By the way, the arrow is simply a convenience, as it is a short had for using the dereferencing star on the pointer, followed by field selecting dot.
p->f is the same as *p.f but just more fun to write (Harbison & Steele 2nd Ed. 7.4.2 p 149).The mutex information is shared through a struct T_arg with a shared pointer, passed into the thread coerced downwards (towards a generic) as a void * then upcast back to it's true type of a struct T_arg *.
You have been provided with utility functions to draw the game board. You task is merge this with what you know from nag.c to communication in a thread safe manner between the main "controller" thread and the draw "viewer" thread. The "model" is latent in the data structures, which are shared between viewer and controller.
Note in the Makefile,
author: burton rosenberg
created: 20 sep 2022
update: 21 sep 2022