Name Game - C String Processing
Source on github
I made this game as a side project to explore string processing in C, which my CSC190 professor specifically mentioned not to do. The game requires a Unix based user list and quizzes you on the user's real names given their username.
- Clone code onto your computer from github or using cmd
git clone git://github.com/LemonPi/LameNameGame.git
- Get user list from shared computer (Unix based)
cp /etc/passwd ./user_list
- Compile game using gcc or clang
gcc -std=c99 -o namegame namegame.c
clang -o namegame namegame.c
- Run game with first argument being the unix user list
- Apply group filter (your classmates are all in the same group)
- Guess their names!
Quite simple in concept, but strangely addicting...
Design for Robustness ¶The code was designed with the objective of making it usable by as many people as possible.
- Anyone with a user list and C compiler (most Unix-based OSes) can compile and play the game
- Program filters by groups to only question you on relevant people
- Users can guess any part of the name and any capitalization on the first character
Making Process ¶It might seem masochistic to do processing with C rather than a language like Python or Perl, but I wanted to give myself a challenge. The best way to learn seems to be
- taking on a project at the edge of your skill
- assume implementation is done for necessary functions and build with them
- then figure out the implementation
Clearly define all the data structure at the start
Each person's name(s) and id are stored inside a name_data object for easy referencing. Names are stored in an array of C-strings to accommodate for multiple names; dynamic allocation is a possibility for optimization. The master_storage doesn't store the objects directly because they unnecessarily uses up memory; instead the name_data objects are dynamically created. In the future master_storage could also be dynamically allocated for optimization.
I altered the existing strtok function (which splits strings) because it skipped neighbouring delimiting characters. My strsplit function returns empty string instead, which enables a regular format - name field is always the 5th element.
Based off of GNU libc's implementation
Parsing the user file and playing the game were implemented as separate functions for modularity. The initialize function scans each user list line, skipping if it doesn't contain the filter string used to define a group. Name_data objects are created after filtering so that no memory is wasted on people that won't be quizzed.
The valid lines are then broken into fields, the first always being their id and the fifth always being their full name. The full name is further split into parts to award part matches.
The play function randomly picks a name; a future update could be to make it so that the same name is not repeated. The play function is similar in splitting the strings up, then it compares each part of the guess to each part of the name.
Messing around with pointers of pointers of pointers inevitably introduced some errors. It was a great opportunity to practice **backtracing with gdb** and debugging C code in general (the run time errors are not informative at all).
Wonderful feeling when everything finally works
Gains from Experience ¶
- C pointer manipulation experience
- GDB backtracing experience
- Appreciation that C is probably not the best language for string processing
- More defined programming process (define components such as data structure and functions first, then implement)