Browsing articles in "uni"

Project Morior

Having recently joined Jenny in an endeavour to create an adventure game, I got to indulge an old hobby of mine that I haven’t done so in quite a while; photography. Armed with a Canon PowerShot S3 and a new tripod, we went out to take some Myst-like shots in and around Portsmouth. It was only the first day of picture-taking, but a good start. What we neglected to consider was the weather.

 

Most of the pictures we got came out nicely despite the rain and torrential winds, but we sure got wet. A few pictures did end up suffering from ‘wet-lens’ though, which can be a cool effect but isn’t quite what we’re going for. We did, however, get some nice 360-degree shots of inside some of the structures of Old Portsmouth that should be pretty good for an adventure game!

Now comes the fun decision; flash, HTML 5/JavaScript or something else altogether… Ah well, to be decided.

Check out the project website if you want to follow development.

uop2cal: Subscribe to your @PortsmouthUni timetable as an iCal feed (UPyoursUPlink?)

Anyone who goes to the University of Portsmouth will be familiar with UPlink, the University’s portal system, and with the timetable it contains. Most people (especially anyone under what used to be ECE) will be familiar with the frustration of lectures changing times/dates/moving room/no longer existing despite being marked as “confirmed”, and the univeristy-standard oh-so-helpful response of “You should be checking it everyday”, along with the other dozen-or-so university sites we’re supposed to be checking every day. (Can you notice my frustration yet? I was originally planning to call this UPyoursUPlink but have since decided on the less-offensive but less-descriptive uop2cal)

This script is a pretty simple one, it uses cURL to screen-scrape the portal and retrieve your timetable, an obnoxious set of regexes (<3) to parse the data into an array, and then the iCalcreator class to generate an ICS file of your timetable, complete with the details (name, lecturer, room, confirmed/unconfirmed (not that I can find a difference, mind), group, etc).

Update: Previously, I was supplying a hosted copy for anyone to use, but I have been advised by the University that you willingly giving me your username and password somehow contravenes the Computer Misuse Act 1990. Whether or not this is the case is irrelevant, as they have asked I stopped doing this. In any case, the source code is still available below (unless they later tell me that’s a problem somehow, too), so you can run your own copy.

Update update: Apparently the University still weren’t too happy with the provisions I had made the last time they contacted me, and have once again requested I remove this. As such, this source is no longer publicly available. Queries about why can be directed towards the University itself, queries about the source are welcome to me.

Micro and ProgLog CW1: Line-follower

#include <p18F252.h>

int antiTwist = 0;	// Flag for anti-twist code.
int i = 0;			// Counting var.
int turnedAround = 0;

void allStop(void);
void turnRight(void);
void turnLeft(void);
void goStraight(void);
void adcOff(void);
void triggerPulse(void);
void catchInterrupt(void);
void setupInterrupts(void);
void turnAround(void);

#pragma code int_vector=0x08
void ISR (void) {
	_asm
	goto catchInterrupt
	_endasm
}
#pragma code

#pragma interrupt catchInterrupt
void catchInterrupt(void) {
	int p;
	int turningFlag;

	if(PORTBbits.RB0 == 1){		// If it's rising.
		IPR1bits.TMR2IP = 0;	// Tmr 2, low priority.
		PIR1bits.TMR2IF = 0;	// Tmr 2 flag cleared.
		PIE1bits.TMR2IE = 1;	// Enable overflow interrupt.

		//Timer2 Registers Prescaler= 16 - TMR2 PostScaler = 16 - PR2 = 255 - Freq = 91.91 Hz - Period = 0.010880 seconds
		T2CON |= 120;        // bits 6-3 Post scaler 1:1 thru 1:16
		T2CONbits.TMR2ON = 1;  	 // bit 2 turn timer2 on;
		T2CONbits.T2CKPS1 = 1;	 // bits 1-0  Prescaler Rate Select bits
		T2CONbits.T2CKPS0 = 0;
		PR2 = 15;        	 // PR2 (Timer2 Match value)

		INTCONbits.INT0IF = 0; // Clear interrupt flag.
	}else{
		if(PIR1bits.TMR2IF == 0){
			PORTAbits.RA4 = 0; // LED ON.
			T2CONbits.TMR2ON = 0;	// Timer off.

			allStop();
			INTCONbits.GIEH = 0; // Temporarily disable int.

			while(PORTCbits.RC0 > 0 || PORTCbits.RC1 > 0 || PORTCbits.RC2 > 0){
				if(antiTwist == 0){
					turnRight();
				}else{
					turnLeft();
				}
			}
		}else{
			PORTAbits.RA4 = 1;		// LED OFF.
		}

		PIR1bits.TMR2IF = 0;	// Tmr 2 flag cleared.
		INTCON3bits.INT1IF = 0; // Clear interrupt flag.

		for(p = 0; p <= 5000; p++){
			// Kill 12-ish mS.
		}

		triggerPulse();
	}

}

void main (void) {
	adcOff();

	PORTAbits.RA4 = 1;		// LED OFF.

	setupInterrupts();

	TRISC = 0xff;       // Port C = input
	TRISB = 0xff;		// Port B = input
	TRISA = 0x00;       // Port A = output

	allStop();			// Start with motors off.
	triggerPulse();		// Start the ultrasound.

	while(1){
			// Sensors:     RIGHT           	 MIDDLE     	     	 LEFT	

		while(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 0){
			turnAround();
		}

		if(PORTCbits.RC0 == 1 && PORTCbits.RC1 == 1 && PORTCbits.RC2 == 1){
			allStop();
		}else if(PORTCbits.RC0 == 1 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 0){
			turnRight();
		}else if(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 1 && PORTCbits.RC2 == 0){
			goStraight();
		}else if(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 1){
			turnLeft();
		}else if(PORTCbits.RC0 == 1 && PORTCbits.RC1 == 1 && PORTCbits.RC2 == 0){
			turnRight();
		}else if(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 1 && PORTCbits.RC2 == 1){
		   	turnLeft();
	   	}

		for(i = 0; i <= 2500; i++){
			// Kill some time.
		}

		// Reset the motors.
		allStop();
	}
}

void turnRight(void) {
	PORTAbits.RA0 = 0;  // Right reverse.
	PORTAbits.RA2 = 0;  // Enable right.

	PORTAbits.RA1 = 1;  // Left forward.
	PORTAbits.RA3 = 0;  // Enable left.
}

void turnLeft(void) {
	PORTAbits.RA0 = 1;  // Right forward.
	PORTAbits.RA2 = 0;  // Enable right.

	PORTAbits.RA1 = 0;  // Left reverse.
	PORTAbits.RA3 = 0;  // Enable left.
}

void goStraight(void) {
	PORTAbits.RA0 = 1;  // Right forward.
	PORTAbits.RA2 = 0;  // Enable right.

	PORTAbits.RA1 = 1;  // Left forward.
	PORTAbits.RA3 = 0;  // Enable left.
}

void allStop(void) {
	PORTAbits.RA2 = 1; 	// Disable right.
	PORTAbits.RA3 = 1; 	// Disable left.
}

void adcOff(void) {
	// The next three lines set all of PORTA to be digital,
	// disabling the ADC. According to datasheet, 110 is
	// all pins digital.
	ADCON1bits.PCFG1 = 1;
	ADCON1bits.PCFG2 = 1;
	ADCON1bits.PCFG3 = 0;
}

void triggerPulse(void) {
	int q;

	PORTAbits.RA5 = 1;   // Start pulse.
	for(q = 0; q <= 20; q++){
		// Kill 54.2-ish uS.
	}
	PORTAbits.RA5 = 0;   // Stop pulse.
}

void turnAround(void) {
			if(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 0){
				while(PORTCbits.RC0 == 0 && PORTCbits.RC1 == 0 && PORTCbits.RC2 == 0){
					turnedAround = 1;
					if(antiTwist == 0){
						turnRight();
					}else{
						turnLeft();
					}
				}
			}

			if(turnedAround == 1){
				antiTwist =~ antiTwist;			//Toggle Twist
				turnedAround = 0;
			}

}

void setupInterrupts() {
	RCONbits.IPEN = 1;			// Priorities on.

	INTCONbits.INT0IF = 0;		// Clear IF.
	INTCONbits.INT0IE = 1;		// INT0 on - it's always high priority.
	INTCON2bits.INTEDG0 = 1;	// Catch INT0 on rising edge.

	INTCON3bits.INT1IF = 0;		// Clear IF.
	INTCON3bits.INT1IE = 1;		// INT1 on.
	INTCON3bits.INT1IP = 1;		// High priority.
	INTCON2bits.INTEDG1 = 0;	// Catch INT1 on falling edge.

	INTCONbits.GIEH = 1;		// Enable all high priority interrupts.
}

Intro To Computing CW2: Text modification

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DELIMITER        "."
#define NEWLINE_DELIM    ".\n"

#define STATE_COMMAND     1
#define STATE_INPUT       2
#define STATE_EXIT        3

#define BUFFER_SIZE     1024
#define CMD_DELIMITER   ";"

#define CMD_PRINT       'p'
#define CMD_QUIT        'q'
#define CMD_APPENDS     'a'
#define CMD_REPLACE     'r'
#define CMD_DELETE      'd'
#define CMD_INSERT      'i'

int main (void) {
	// Setup whatever we need.
	char* buffer = NULL;
	int buffer_size = 0;
	int buffer_length = 0;
	int chars_read = 0;
	int appState = STATE_COMMAND;
	int lineNumber = 0;
	int char_pos = 0;

	char valid_cmds[BUFFER_SIZE + 1] = {'\0'};
	char* all_cmds = NULL;
	char* cmd_line = NULL;

	int location = 0;
	int locationB = 0;
	char commandLetter;
	char notModifier;
	char rangeModifier;
	char* string = "";

	int skipPrint = 0;

	// We're going to loop through here until we're all done. (ie, STATE_EXIT)
	do {
		// Read input into the buffer.
		chars_read = getline(&buffer, &buffer_size, stdin);

		// Make sure it exists.
		if(chars_read < 1){
			break;
		}

		// Delimiters make the state increase.
		if(strcmp(buffer, DELIMITER) == 0 || strcmp(buffer, NEWLINE_DELIM) == 0){
			appState = appState + 1;
			continue;
		}

		// Command state.
		if(appState == STATE_COMMAND){

			// Get some commands from the buffer.
			// Go through each character of the command, and see if it matches a
			// real command, if it does, put it in the valid commands buffer.
			buffer_length = strlen(buffer);
			for(char_pos = 0; char_pos < buffer_length; char_pos++){
					if(islower(buffer[char_pos])){
						if(buffer[char_pos] == CMD_PRINT ||
							buffer[char_pos] == CMD_QUIT ||
							buffer[char_pos] == CMD_APPENDS ||
							buffer[char_pos] == CMD_REPLACE ||
							buffer[char_pos] == CMD_DELETE ||
							buffer[char_pos] == CMD_INSERT ){
								strcat(valid_cmds, buffer);
								strcat(valid_cmds, CMD_DELIMITER);
								break;
						}

					appState  = STATE_EXIT;
					break;
				}
			}
		}

		// Text input state.
		if(appState == STATE_INPUT){
			skipPrint = 0; // This must must MUST get set to 0 on each loop, or everything will break.
			lineNumber = lineNumber + 1; // Increment the line number each time we go through.
			all_cmds = strdup(valid_cmds); // And now all_cmds is the same as valid_cmds.

			// Get a command from the commands buffer.
			for(cmd_line = strtok(all_cmds, CMD_DELIMITER); cmd_line != NULL; cmd_line = strtok(NULL, CMD_DELIMITER)){

				// Match this one first, because it's the most specific - put the various bits of the command
				// into the various variables. Also, make sure the range modifier is one of the valid ones,
				// and that the not modifier is what we expect.
				if(sscanf(cmd_line, "%d%c%d%c%c%s", &location, &rangeModifier, &locationB, &notModifier, &commandLetter, string) == 6
					&& (rangeModifier == ',' || rangeModifier == '~')
					&& notModifier == '!'){

					// Now check the command issued. Appends? Yeah, check the range --
					// For comma, make sure the line number isn't in the range specified. (Remember the '!'.)
					// For tilde, check that A) the line number isn't the one specified, B) the line number minux
					// the starting point divded by the second number has a remainder, C) the line number is less than
					// the starting point. If A and B or A and C are true, we'll match.
					if(commandLetter == CMD_APPENDS && (
						(rangeModifier == ',' && (location > lineNumber || lineNumber > locationB))
						|| (rangeModifier == '~' && location != lineNumber && ((lineNumber - location) % locationB > 0  || lineNumber < location))
					)){
						// Append, stick the string to the end of the existing buffer and attach a newline.
						// Append will use this same method for the rest off the programme.
						strcat(buffer, string);
						strcat(buffer, "\n");
					}

					// Same method for matching replace.
					if(commandLetter == CMD_REPLACE && (
						(rangeModifier == ',' && (location > lineNumber || lineNumber > locationB))
						|| (rangeModifier == '~' && location != lineNumber && ((lineNumber - location) % locationB > 0  || lineNumber < location))
					)){
						// Just replace the buffer with the new string and add a newline to the end.
						// Replacement will be done the same way as this for the rest of the app.
						strcpy(buffer, string);
						strcat(buffer, "\n");
					}

					// And insert.
					if(commandLetter == CMD_INSERT && (
						(rangeModifier == ',' && (location > lineNumber || lineNumber > locationB))
						|| (rangeModifier == '~' && location != lineNumber && ((lineNumber - location) % locationB > 0  || lineNumber < location))
					)){
						// Add a newline to the end of the new string, stick the buffer on after that, and then
						// move it all in to the buffer. We'll reuse this method for insert throughout the app.
						strcat(string, "\n");
						strcat(string, buffer);
						strcpy(buffer, string);
					}

				// We're moving down in order of most specific matching regime to least specific. So this one's next.
				}else if(sscanf(cmd_line, "%d%c%d%c%c", &location, &rangeModifier, &locationB, &notModifier, &commandLetter) == 5
					&& (rangeModifier == ',' || rangeModifier == '~')
					&& notModifier == '!'){

					// We're using the same matching system as before to match the ranges for print, remembering that
					// there's still an !.
					if(commandLetter == CMD_PRINT && (
						(rangeModifier == ',' && (location > lineNumber || lineNumber > locationB))
						|| (rangeModifier == '~' && location != lineNumber && ((lineNumber - location) % locationB > 0  || lineNumber < location))
					)){
						printf("%s", buffer);
					}

					// And delete.
					if(commandLetter == CMD_DELETE && (
						(rangeModifier == ',' && (location > lineNumber || lineNumber > locationB))
						|| (rangeModifier == '~' && location != lineNumber && ((lineNumber - location) % locationB > 0  || lineNumber < location))
					)){
						skipPrint = 1;
					}

				// This is the next one in order, this time no !
				}else if(sscanf(cmd_line, "%d%c%d%c%s", &location, &rangeModifier, &locationB, &commandLetter, string) == 5
					&& (rangeModifier == ',' || rangeModifier == '~')){   

					// For appends, we're checking if the range is a comma or a tilde. If it's comma, is the line number
					// in between the addresses specified? If it's a tilde, check A) is line number equal to the location
					// specified, B) does the line number minus the location, divided by the second number,
					// have NO remainder? and C) is the line number greater than the location specified?
					// If A is true, or B and C are true, we match.
					if(commandLetter == CMD_APPENDS && (
						(rangeModifier == ',' && location <= lineNumber && lineNumber <= locationB)
						|| (rangeModifier == '~' && (location == lineNumber || (((lineNumber - location) % locationB) == 0 && lineNumber > location)))
					)){
						strcat(buffer, string);
						strcat(buffer, "\n");
					}

					if(commandLetter == CMD_REPLACE && (
						(rangeModifier == ',' && location == lineNumber)
						|| (rangeModifier == '~' && (location == lineNumber || (((lineNumber - location) % locationB) == 0 && lineNumber > location)))
					)){
						strcpy(buffer, string);
						strcat(buffer, "\n");
					}

					// Using this to for the ranged replace for a comma-range only, once we've replaced the first line,
					// We make sure that none of the lines after it print until we're out of the range.
					// Ref: http://cnfolio.com/IntroComputingTutorial05#example05
					if(commandLetter == CMD_REPLACE && (
						(rangeModifier == ',' && lineNumber > location && lineNumber <= locationB)
					)){
						skipPrint = 1;
					}

					if(commandLetter == CMD_INSERT && (
						(rangeModifier == ',' && location <= lineNumber && lineNumber <= locationB)
						|| (rangeModifier == '~' && (location == lineNumber || (((lineNumber - location) % locationB) == 0 && lineNumber > location)))
					)){
						strcat(string, "\n");
						strcat(string, buffer);
						strcpy(buffer, string);
					}

				// And then this one goes next.
				}else if(sscanf(cmd_line, "%d%c%d%c", &location, &rangeModifier, &locationB, &commandLetter) == 4
					&& (rangeModifier == ',' || rangeModifier == '~')){

					// Same method as before for dealing with the different ranges.
					if(commandLetter == CMD_PRINT && (
						(rangeModifier == ',' && location <= lineNumber && lineNumber <= locationB)
						|| (rangeModifier == '~' && (location == lineNumber || (((lineNumber - location) % locationB) == 0 && lineNumber > location)))
					)){
						printf("%s", buffer);
					}

					if(commandLetter == CMD_DELETE && (
						(rangeModifier == ',' && location <= lineNumber && lineNumber <= locationB)
						|| (rangeModifier == '~' && (location == lineNumber || (((lineNumber - location) % locationB) == 0 && lineNumber > location)))
					)){
						skipPrint = 1;
					}

				// This one's next but it doesn't have a range, just the not modifier.
				}else if(sscanf(cmd_line, "%d%c%c%s", &location, &notModifier, &commandLetter, string) == 4
					&& notModifier == '!'){

					// So basically, because of the !, we only make sure that the line number DOESN'T match.
					// We don't have to use the range detection here.
					if(commandLetter == CMD_APPENDS && location != lineNumber){
						strcat(buffer, string);
						strcat(buffer, "\n");
					}

					// And here.
					if(commandLetter == CMD_REPLACE && location != lineNumber){
						strcpy(buffer, string);
						strcat(buffer, "\n");
					}

					// And here.
					if(commandLetter == CMD_INSERT && location == lineNumber){
						strcat(string, "\n");
						strcat(string, buffer);
						strcpy(buffer, string);
					}

				// We do have a not modifier here, so we need to not match everything.
				}else if(sscanf(cmd_line, "%d%c%c", &location, &notModifier, &commandLetter) == 3
					&& notModifier == '!'){

					// Same here.
					if(commandLetter == CMD_PRINT && location != lineNumber){
						printf("%s", buffer);
					}

					// And here.
					if(commandLetter == CMD_DELETE && location != lineNumber){
						skipPrint = 1;
					}

				// This time we don't have the not modifier.
				}else if(sscanf(cmd_line, "%d%c%s", &location, &commandLetter, string) == 3){

					// So we check that the line numbers DO match. Again, no worry about ranges.
					if(commandLetter == CMD_APPENDS && location == lineNumber){
						strcat(buffer, string);
						strcat(buffer, "\n");
					}

					// Same here.
					if(commandLetter == CMD_REPLACE && location == lineNumber){
						strcpy(buffer, string);
						strcat(buffer, "\n");
					}

					// And here.
					if(commandLetter == CMD_INSERT && location == lineNumber){
						strcat(string, "\n");
						strcat(string, buffer);
						strcpy(buffer, string);
					}

				// We don't really have to make sure location > 0, but it's better to
				// validate as much of the input as you can, to weed out falsely-matching
				// input.
				}else if(sscanf(cmd_line, "%d%c", &location, &commandLetter) == 2
					&& location > 0){

					// Again, it's a simple case of does the line number match?
					if(commandLetter == CMD_PRINT && location == lineNumber){
						printf("%s", buffer);
					}

					// Here too.
					if(commandLetter == CMD_QUIT && location == lineNumber){
						appState = STATE_EXIT;
					}

					// This one as well.
					if(commandLetter == CMD_DELETE && location == lineNumber){
						skipPrint = 1;
					}

				// We have a not modifier here.
				}else if(sscanf(cmd_line, "%c%c", &notModifier, &commandLetter) == 2
					&& notModifier == '!'){

					// But no line numbers, so it matches every line.
					if(commandLetter == CMD_PRINT){
						// well, if we DON'T (from the !) print every line an additional time,
						// then it just does what it normally does so this doesn't
						// actually need to do anything?
					}

					/// Same here.
					if(commandLetter == CMD_DELETE){
						// likewise, if we don't delete every line, then it acts
						// normally and again, this doesn't do anything.
						// I don't actually understand the point in !p and !d.
					}

				// We're back to a simple command that effects every line, but not ! modifier.
				}else if(sscanf(cmd_line, "%c%s", &commandLetter, string) == 2){

					// So we don't need to worry about line matching.
					if(commandLetter == CMD_APPENDS){
						strcat(buffer, string);
						strcat(buffer, "\n");
					}

					// Or here.
					if(commandLetter == CMD_REPLACE){
						strcpy(buffer, string);
						strcat(buffer, "\n");
					}

					// Or here.
					if(commandLetter == CMD_INSERT){
						strcat(string, "\n");
						strcat(string, buffer);
						strcpy(buffer, string);
					}   

				// Very simple one letter commands.
				}else if(sscanf(cmd_line, "%c", &commandLetter) == 1){

					// Which match every line.
					if(commandLetter == CMD_PRINT){
						printf("%s", buffer);
					}

					// And here.
					if(commandLetter == CMD_QUIT){
						appState = STATE_EXIT;
					}

					// Same here.
					if(commandLetter == CMD_DELETE){
						skipPrint = 1;
					}

				}

				// If nothing's told it not to print since the beginning of the loop,
				// print the buffer. By this point, the buffer may have been modified
				// by a replace, append, or insert. Doesn't matter, print it anyway as
				// long as nothing set skipPrint to 1.
				if(skipPrint == 0){
					printf("%s", buffer);
				}
			}

		}
	} while(appState != STATE_EXIT);

	return 0;
}

Digital Systems CW2: Stopwatch

C – micro

#include <reg66x.h>

/////////////////////////
//// PIN DEFINITIONS ////
/////////////////////////

// Port 0 - in - in from CPLD
#define inDigits P0

// Port 1 - all out - out to CPLD
sbit outClock = P1^0;
sbit outReset = P1^1;

// Port 2 - all out - out to display
#define outDisplay P2

// Port 3 - in/out - buttons & speaker & CPLD control
sbit btnStartStop = P3^3;  // in
sbit outAcknowledge = P3^7;// out

/////////////////////////////
//// DECLARE SUBROUTINES ////
/////////////////////////////

void setupTimer();
void setupInputs();
void runningLoop();
void timerCallback();
void digitsCallback();
void controlCallback();
void resetCPLD();

///////////////////////////
//// DECLARE VARIABLES ////
///////////////////////////

int modeState = 0;
unsigned char hunths, tenths, seconds, tenSecs;

////////////////////////
//// PROGRAMME CODE ////
////////////////////////

void main() {
    setupTimer();
    setupInputs();
    resetCPLD();
   runningLoop();
}

void runningLoop() {
    while(1){
        outDisplay = tenSecs + 64;
        outDisplay = seconds + 128;
        outDisplay = tenths + 192;
        outDisplay = hunths + 0;
    }
}

void resetCPLD(){
    outReset = 1;
    outReset = 0;

    return;
}

void setupTimer() {
    TMOD = 0x01;        // M0 = 1 (Timer mode 1 - 16 bit mode)
    TL0 = 0xFF;         // 400Hz = 2304 delay count, 65535-2304 = 63231
    TH0 = 0xF6;         // TH0 = 0xF6 :: TL0(0xFF) = 0xF6FF = 63231
    ET0 = 1;                // T0 Interrupt enabled.
    EA = 1;             // Interrupts enabled.
    TR0 = 1;                // Begin timer.

    P2 = 0x00000000;

    return;
}

void setupInputs() {
    EA = 1;             // Interrupts enabled.
    IT0 = 1;                // Set on falling edge.
    IT1 = 1;                // Set on falling edge.
    EX0 = 1;            // Enable external interrupt 0.
    EX1 = 1;                // Enable external interrupt 1.

    modeState = 0;
    outAcknowledge = 0;

    return;
}

//////// INTERRUPT CALLBACKS ////////

void timerCallback() interrupt 1 using 2 {
   TR0 = 0;
    TL0 = 0xFF;         // 400Hz = 2304 delay count, 65535-2304 = 63231
    TH0 = 0xF6;         // TH0 = 0xF6 :: TL0(0xFF) = 0xF6FF = 63231
    TF0 = 0;
    TR0 = 1;                // Begin timer.
    outClock =~ outClock;   // Invert the clock output pin.
}

void digitsCallback() interrupt 0 {
    if(modeState == 0){
        seconds = inDigits & 0x0f;
    }else if(modeState == 1){
        tenSecs = inDigits & 0x0f;
    }else if(modeState == 2){
        hunths = inDigits & 0x0f;
    }else if(modeState == 3){
        tenths = inDigits & 0x0f;
    }

    if(modeState < 3){
        modeState++;
    }else{
        modeState = 0;
    }

    outAcknowledge = 1;
    outAcknowledge = 0;
}

void controlCallback() interrupt 2 {
    TR0 =~ TR0;
}

VHDL – CPLD

----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date:    12:25:53 03/18/2009
-- Design Name:
-- Module Name:    clock - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity clock is
    Port ( clockSource : in STD_LOGIC;  -- clock pulse from micro
           reset : in  STD_LOGIC;        -- reset pulse from micro
           microAck : in STD_LOGIC;      -- acknowledgement from micro
              digit : out  STD_LOGIC_VECTOR (3 downto 0);
                                                      -- numbers to micro
              cpldRts : out STD_LOGIC);     -- ready to send to micro (ACTIVE LOW)
end clock;

architecture Behavioral of clock is

signal hunthsCount : std_logic_vector(3 downto 0);
signal tenthsCount : std_logic_vector(3 downto 0);
signal secondCount : std_logic_vector(3 downto 0);
signal tenSecCount : std_logic_vector(3 downto 0);
signal controlFlag : std_logic_vector(1 downto 0);

begin                                                         

process(reset, clockSource)
begin
if reset = '1' then
    hunthsCount
Jan 27, 2009

Technology Context CW2: Transportation Network

“Select and research a specific transportation network. Describe and analyse how computers are used to reduce the environmental impact of that specific transportation network. Discuss the potential financial, legal, or political effects of using that computer system for other networks.”


Britain is becoming more and more dependent on public transportation networks, which many feel are vital for the replacement of personal transportation (such as cars) in order to reduce congestion as well as to reduce the environmental impact caused by the release of such great volumes of carbon dioxide. As Foley explains, “the severity of congestion in the South East is second only to London” (Foley, 2004, p.50), which suggests that this area of the country would benefit greatly from more efficient transportation networks. In such areas, where even the public transportation networks are congested, they should be monitored to ensure that their own environmental impact is minimal.

The chosen research area is therefore centred on the bus network that operates in the Portsmouth area, specifically, the usage of interactive bus timetabling and monitoring computers known as ‘The Portsmouth Online Real-Time Traveller Information System’, otherwise known as ‘PORTAL’. This system includes kiosks that are located at bus stops across Portsmouth City and the surrounding areas of southern Hampshire, as well as individual real-time monitoring systems contained within each bus, called ‘On-Board Units’ or ‘OBUs’. In addition, at certain bus terminals, the PORTAL system is used to power real-time displays indicating which bus at which stand, what times the busses operate at and where the bus goes, all in real-time. All this information is then tied together over a mesh network known as ‘MeshNetworks Enabled Architecture’ or ‘MEA’, which is a mesh-topology mobile broadband netork. “The PORTAL system also provides city administrators and bus operators with real-time vehicle, driver and route information, as well as bus maintenance, speed and route tracking. This information is used to improve logistics management, passenger safety, and vehicle maintenance.” (City Council, 2004, p.4) Improving all these points, logistics management especially, is important because it can greatly increase the efficiency of the network. This has an overall impact on the quality of the network and therefore on private car usage, as Chien explains, “Poor transportation systems do not attract ridership, hence the usage of passenger cars increases. Therefore, traffic and environmental conditions deteriorate. Efficient public transportation has been recognized as one of the potential ways of mitigating air pollution, reducing energy consumption improving mobility and alleviating traffic congestion.” (Chien, 2005, p.360)

These kiosks, situated at popular and convenient bus stops, are called ‘i+ kiosks’. These i+ kiosks allow transportation users to access bus timetables, train timetables, local taxi information and walking directions. The monitoring systems installed on the busses allow the system to keep track of bus location and usage so that the timetables on the kiosks and bus terminals’ displays are kept up to date. This type of system can influence several environmental factors including an increase in the efficiency of the bus network by not running busses along underused routes at certain times, but it can also affect the road congestion through providing easier access to public transportation of all types for the average person by allowing them to access information for all types of local transportation. (Great Britain. Portsmouth City Council, 2005, p.9-43) After all, according to Gleaves (1995, p.63), “a key deficiency to overcome [in getting people to adopt public transportation] is the lack of coherence of the overall public transport network”. This congestion issue is what this paper will be focussing on.

The kiosks provide some crucial information to passengers of busses and trains alike, as well as pedestrians, allowing them to complete their journeys without the need for private transportation. “One of the sustainable development objectives of the South East’s Regional Economic Strategy (2002) is to reduce road traffic and congestion through reducing the need to travel by car and improving travel choice.” (Foley, 2004, p.51)

However, these kiosks fall short in some areas. Specifically, the kiosks are not necessarily positioned in locations that benefit the largest number of people possible. As of 2004, it “provides real-time travel information to passengers from more than 300 buses and is displayed at 45 locations including 36 bus stops and nine kiosks throughout the city.” (City Council Project, 2004, p.4) Clearly, this leaves room for improvement before these kiosks can become as useful as they are planned to be. In addition, few people realise that these kiosks are capable of providing this type of information, which ultimately causes them not to be used. Some type of advertising or awareness programme for these kiosks could help them to achieve the desired effect.

In practice, this system has already been applied to the train network in some ways, but its primary focus appears to be for the city’s bus network. The bus network was designed from the ground up to utilise the PORTAL system for tracking and information, and while the PORTAL system has been connected to existing systems for monitoring train times and status, the train network has always had its own system for doing this. Further integration of the train networks with the PORTAL system would be redundant of the train network’s own systems, and would require a great deal of cooperation between the city council and the train operators. It is, however, expected that journey tickets, train included, will be available for purchase from the i+ kiosks, meaning that further degrees of integration are in the works. (Great Britain. Portsmouth City Council, 2005, p.9-43) The system could be adapted to ferries and light rail as well, however this would require these systems to be connected to the city’s central PORTAL system for monitoring, and would also require that these systems billing and ticketing systems be integrated with the PORTAL system and the i+ kiosks as well.

Furthermore, the system could be installed in other cities and locations for their own local bus networks. In some locations, this process has already begun. However, the backend of the PORTAL system can be quite expensive to install, maintain and upgrade, costing £14,437.40 per month to maintain just the kiosks in 2006. The cost of maintaining the PORTAL system and the MEA mesh network powering it is not included in this figure. (Great Britain. Portsmouth City Council, 2006, p.2) Some localities may find the figure, when added to the cost of something similar to the PORTAL system and the systems powering this, quite prohibitive when considering the installation of a system similar to the i+ kiosks, especially since these maintenance costs are a recurring cost, rather than a one-off investment.

In conclusion, the PORTAL system combined with the MEA mesh network that powers it and the i+ kiosks that allow the consumers to access the system is an excellent system for promoting the use of public transport over private transport. By providing easy access to bus and train timetables, routes and schedules, the average person will find it easier than ever to get from one point to another without having to resort to driving. If the system were expanded to a larger number of kiosks available more frequently placed throughout the city, people would be easily more aware of their existence simply from seeing them more often. In addition, an advertising strategy would be beneficial as well, creating further awareness about these kiosks. By increasing public awareness about these kiosks, people would be more inclined to use them, and they would encourage these users to use public transportation, or even walk, rather than driving to their destination, which would help in resolving the ever-growing congestion in this region. Maintenance, installation and operating costs aside, if other regions were to institute this system, and were to advertise it and encourage the public’s awareness of their system, they would likely find that it would help in encourage the public to utilise the various public transport networks available in the applicable region, and ultimately would assist in relieving traffic congestion throughout the area.


References.

Chien, S. I. (2005). Optimization Of Headway, Vehicle Size and Route Choice for Minimum Cost Feeder Service [Electronic Version]. Transportation Planning and Technology, Volume 28. (issue 5), p.359-380.

City Council Project Makes Portsmouth “Europe’s First Mesh-Enabled Municipality” [Electronic Version]. (2004). Computer & Control Engineering, Volume 15. (issue 4), p.4.

Foley, J. (2004). Driving Toward Sustainability [Electronic Version]. New Economy, Volume 11. (issue 1), p.50-56.

Gleave, S. D. (1995). Alternatives to Traffic Growth: The Role of Public Transport and the Future for Freight. London: Transport 2000.

Great Britain. Portsmouth City Council. (2006). iPlus Point Maintenance and Operation Agreement. Portsmouth: Portsmouth City Council. (Retrieved from:http://www.portsmouth.gov.uk/media/et20060607r11.pdf)

Great Britain. Portsmouth City Council. (2005). The City of Portsmouth INFORM Conference Presentation. Portsmouth: Portsmouth City Council. (Retrieved from: http://www.inform-rtig.org/conference/conferences/2005/presentations/5.%20John%20Domblides.pdf)


Digital Systems CW1: Calculator

$include (reg66x.inc)

; At the very beginning, jump to 40H...
ORG 0
TIMECOUNT EQU 8h
MBUFFER EQU 10h
KBUFFERA EQU 18h
KBUFFERB EQU 20h
KEY EQU 28h
MODE EQU 30h

DISPLAYA EQU 38H
DISPLAYB EQU 40H
DISPLAYC EQU 48H
DISPLAYD EQU 50H

DISPLAYR EQU 58H
CLEARCALLED EQU 60H
LJMP CLEAR

; ...which is where our code begins.
ORG 40H

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MAIN:             ; Defines the MAIN loop.
ACALL INPUT       ; INPUT subroutine.
ACALL CALCULATE   ; CALCULATE subroutine.
ACALL OUTPUT      ; OUTPUT subroutine.
SJMP MAIN         ; Jump back to the beginning.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: CALCULATE
;;;;; Purpose: Prepares calculations for output.
;;;;; MODE: 1 = ADD, 2 = SUB, 3 = MULT
;;;;;       4 = DIV, 9 = RESULT
CALCULATE:
MOV A, MODE
CJNE A, #0, STOREMODE
SJMP NONEOFTHEABOVE

STOREMODE:
CJNE A, #9, DOSTOREMODE
SJMP RESULT
DOSTOREMODE:
MOV MBUFFER, MODE
LJMP CALCULATEDONE

RESULT:
MOV A, MBUFFER
ADDITION:
CJNE A, #1, SUBTRACT
MOV A, KBUFFERA
ADD A, KBUFFERB
MOV R0, A
MOV B, #10
DIV AB
CJNE A, #0, ADDTWODIGITS
MOV DISPLAYA, R0
SJMP CLEANUP
ADDTWODIGITS:
MOV DISPLAYB, A
MOV DISPLAYA, B
SJMP CLEANUP

SUBTRACT:
CJNE A, #2, MULTIPLY
MOV A, KBUFFERA
SUBB A, KBUFFERB
MOV DISPLAYA, A
SJMP CLEANUP

MULTIPLY:
CJNE A, #3, DIVIDE
MOV A, KBUFFERA
MOV B, KBUFFERB
MUL AB
MOV R0, A
MOV B, #10
DIV AB
CJNE A, #0, MULTWODIGITS
MOV DISPLAYA, R0
SJMP CLEANUP
MULTWODIGITS:
MOV DISPLAYB, A
MOV DISPLAYA, B
SJMP CLEANUP

DIVIDE:
CJNE A, #4, CALCULATEDONE
MOV A, KBUFFERA
MOV B, KBUFFERB
DIV AB
MOV DISPLAYA, B
MOV DISPLAYR, #1
MOV DISPLAYC, A
SJMP CLEANUP

NONEOFTHEABOVE:
MOV A, MBUFFER
CJNE A, #0, STOREB
STOREA:
MOV KBUFFERA, KEY
SJMP STOREDONE
STOREB:
MOV KBUFFERB, KEY
STOREDONE:
MOV A, KEY
CJNE A, #0, DISPLAYKEY
SJMP CALCULATEDONE
DISPLAYKEY:
MOV DISPLAYA, KEY
SJMP CALCULATEDONE

CLEANUP:
MOV KBUFFERA, #0
MOV KBUFFERB, #0
MOV MBUFFER, #0
MOV KEY, #0
MOV MODE, #0
MOV A, #0
MOV B, #0
CALCULATEDONE:
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: OUTPUT
;;;;; Purpose: Updates the display.
OUTPUT:
MOV P2, #3
MOV P1, DISPLAYD
ACALL TIMER

MOV P2, #2
MOV P1, DISPLAYC
ACALL TIMER

MOV P2, #1
MOV A, DISPLAYR
CJNE A, #1, DISPLAYBNOW
MOV P1, #0FFh
CPL P2.4
CPL P2.5
SJMP DISPLAYANOW
DISPLAYBNOW:
MOV P1, DISPLAYB

DISPLAYANOW:
ACALL TIMER
MOV P2, #0
MOV P1, DISPLAYA
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: INPUT
;;;;; Purpose: Read buttons pressed on keypad.
INPUT:
MOV MODE, #0
MOV P0, #0FFH
CLR P0.4
ACALL TIMER
MOV A,P0
ANL A, #0FH
CJNE A, #0FH, ROWONECOLONE

SETB P0.4
CLR P0.5
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWTWOCOLONE

SETB P0.5
CLR P0.6
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWTHREECOLONE

SETB P0.6
CLR P0.7
ACALL TIMER
MOV A, P0
ANL A, #0FH
CJNE A, #0FH, ROWFOURCOLONE
RET

ROWONECOLONE:
CJNE A, #14, ROWONECOLTWO
MOV KEY, #1h
RET
ROWONECOLTWO:
CJNE A, #13, ROWONECOLTHREE
MOV KEY, #2h
RET
ROWONECOLTHREE:
CJNE A, #11, ROWONECOLFOUR
MOV KEY, #3h
RET
ROWONECOLFOUR:
MOV MODE, #1 ; mode 1 = ADD
RET

ROWTWOCOLONE:
CJNE A, #14, ROWTWOCOLTWO
MOV KEY, #4h
RET
ROWTWOCOLTWO:
CJNE A, #13, ROWTWOCOLTHREE
MOV KEY, #5h
RET
ROWTWOCOLTHREE:
CJNE A, #11, ROWTWOCOLFOUR
MOV KEY, #6h
RET
ROWTWOCOLFOUR:
MOV MODE, #2 ; mode 1 = SUB
RET

ROWTHREECOLONE:
CJNE A, #14, ROWTHREECOLTWO
MOV KEY, #7h
RET
ROWTHREECOLTWO:
CJNE A, #13, ROWTHREECOLTHREE
MOV KEY, #8h
RET
ROWTHREECOLTHREE:
CJNE A, #11, ROWTHREECOLFOUR
MOV KEY, #9h
RET
ROWTHREECOLFOUR:
MOV MODE, #3 ; mode 1 = MULT
RET

ROWFOURCOLONE:
CJNE A, #14, ROWFOURCOLTWO
MOV CLEARCALLED, #1
ACALL CLEAR
RET
ROWFOURCOLTWO:
CJNE A, #13, ROWFOURCOLTHREE
MOV KEY, #0h
RET
ROWFOURCOLTHREE:
CJNE A, #11, ROWFOURCOLFOUR
MOV MODE, #9 ; mode 9 = EQUALS
RET
ROWFOURCOLFOUR:
CJNE A, #7, NOKEYSCAN
MOV MODE, #4 ; mode 4 = DIVIDE
RET

NOKEYSCAN:
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: CLEAR
;;;;; Purpose: Reset the shit out of it.
CLEAR:
MOV KBUFFERA, #0
MOV KBUFFERB, #0
MOV MBUFFER, #0
MOV DISPLAYA, #0
MOV DISPLAYB, #0
MOV DISPLAYC, #0
MOV DISPLAYD, #0
MOV DISPLAYR, #0
MOV KEY, #0
MOV MODE, #0
MOV A, #0
MOV B, #0
MOV A, CLEARCALLED
CJNE A, #0, CLEARRET
LJMP MAIN
CLEARRET:
MOV A, #0
MOV CLEARCALLED, #0
RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; Subroutine: TIMER
;;;;; Purpose: Produce a 58ms-ish delay on ACALL.
TIMER:
MOV R0, #255
TIMELOOPA:
DJNZ R0, TIMELOOPA
TIMEDONE:
RET

END

Intro To Computing CW1: 20 Questions

User Guide

Contents

  1. Installation, compilation and execution
  2. Main menu
  3. Gameplay
  4. About the database file

Installation, compilation and execution

Before playing the game, it must be installed and compiled. There are a few requirements to do this:

  • UNIX-like environment, including:
    • Linux
    • BSD
    • Mac OS X
    • Cygwin on Windows
  • GNU C Compiler (gcc)
  • Various standard C header files:
    • stdio.h
    • stdlib.h
    • string.h
    • termios.h
    • regex.h

Once all these requirements are met, download the myguess.c and animals.db files from below this userguide, and save them into the same directory. Then you need to use ’gcc’ to build the source file into an executable from within that directory:
gcc -o ./myguess ./myguess.c

Once the executable is built, executing it is as simple as typing ./myguess and pressing return – after that it should launch and you can go ahead and move on to the next section of this guide.

Main menu

Main menu

From this menu, you have three options. Simply press the coloured letter to select that option. In this case, pressing ’n' will start a new game, pressing ’l' will re-list all the animals the game knows, and pressing ’q' will quit the game. It doesn’t mater if the letter you press is uppercase or lowercase when you select it – the game is case-insensitive.

It’s also worth noting that all menus in this game work the same way. When you are presented with a selection, your options will have a bolded letter and pressing that letter on the keyboard will select that option.

Gameplay

When you begin a new game by selecting the new game option from the main menu, you need to think of one of the animals the game knows. The game will then ask you questions in an attempt to determine which animal you have selected in your mind:
Gameplay: Questions
Answer the questions, and when it has reached a conclusion, it will guess the animal you have thought of:
Gameplay: Guessing
If it fails to reach a conclusion, then you have tricked the game (or cheated!) and the game will admit defeat:
Gameplay: Defeated

About the database file

The animals.db file contains a list of animals and their attributes. The format of this file is controlled by several parameters in the source of myguess.c, but the default format is quite simple. There is one animal per line. The line begins with the name of the animal, followed by it’s attributes (space-separated). There is a single colon (:) separating the animal’s name from it’s attributes. This results in the database file shown towards the bottom of this page.

This file allows the game to be expanded to additional animals without needing to be recompiled. Only certain attributes are recognised by the game at this time. These include:

  • swimming
  • flying
  • underground
  • feline
  • arachnid
  • insect
  • canine
  • jungle
  • farm
  • desert
  • herbivore
  • domesticated
  • eggs
  • camouflage
  • milk

Source Code: myguess.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <regex.h>

/* Animal database setup */

// In the DB file, this separates the animal from it's attributes.
#define S_DBFILE_KEY_DELIM ":"
// This separates each attribute from the others.
#define S_DBFILE_FLAG_DELIM " "
// This separates each entry containing attributes and animals.
#define S_DBFILE_ENTRY_DELIM "\n"
/*
Under default settings, the DB file would look like:
animal1:attribute1 attribute2
animal2:attribute2 attribute3
[..etc..]
*/

// The path to the DB file itself.
#define P_DBFILE "./animals.db"
// You can download the default file from:
//     http://kevinsnet.com/subsites/uni/IntroComputing/animals.db

// The number of entries in the DB file. The default file has 30.
#define I_TOTAL_ANIMALS 30;

/* Game setup */

// The name of the game.
#define S_NAME "20 Questions"
// The version of the game.
#define S_VERSION "0.1c"
// Enable debug mode?
#define I_DEBUG 0
/* Debug mode, when set to 1, outputs extra information about
 * the data loaded from the database at startup and the process
 * of deducting the impossible answers during gameplay. Enable it
 * to see this information.
 /*

/* Output setup */

// Prefix status output with this:
#define S_STATUS "-\e[97;1m:\e[0m- "
// Prefix informational output with this:
#define S_INFO "-\e[94;1mi\e[0m- "
// Prefix error output with this:
#define S_ERROR "-\e[91;1m!\e[0m- "
// Prefix requests for input from the user with this:
#define S_QUESTION "-\e[93;1m?\e[0m- "
// Prefix win-notices with this:
#define S_WINNER "-\e[92;1m$\e[0m- "
// Prefix lose-notices with this:
#define S_LOSER "-\e[93;1m|\e[0m- " 

// ANSI code to clear the screen:
#define A_CLEAR "\e[2J"
// ANSI code for bold text:
#define A_BOLD "\e[93;1m"
/* Note: ANSI bolded text doesn't look right on all the systems
 * tested on, so instead I'm colouring it yellow. It seems to work
 * better than bolding it on some terminals, plus, it looks nice.
 */
// ANSI code to reset output to normal:
#define A_NORM "\e[0m"

/* End of configuration */

// Declare the functions we're gonna make later.
int unbufGetc();
void startGame();
void quitGame();
void listAnimals();
void doMainMenu();
int getUserAnswer();
void endGame();

// The following variables are defined in a global context,
// because these won't always pass between function correctly.
// Their data is actually defined in the configuration section above.
int totalAnimals = I_TOTAL_ANIMALS;
int debugMode = I_DEBUG;

// The main subroutine.
int main( void ){
	// Variable declaration. These are all variables we're gonna use in main().
	FILE *dbHandle;
	char *stringBuffer;
	char fileBuffer[128];
	char strAnimalList[1024] = "";
	char tmpBuffer[128] = "";
	char dbAnimals[totalAnimals][128];
	char dbFlags[totalAnimals][128];
	int lineCount = 0;
	int cycleCount = 0;
	int phaseCount = 0;
	int loopCount = 0;
	int tmpInt = 0;
	fpos_t dbPosition;

	printf("%s%s%s v%s starting up...\n", A_CLEAR, S_STATUS, S_NAME, S_VERSION);

	// Open the DB file for reading, or prompt the user to download it if it can't be read.
	if((dbHandle = fopen(P_DBFILE, "r")) == NULL){
		printf("%sCannot open the DB file. Enter the following URL in a web browser,\n", S_ERROR);
		printf("%sand save the resulting file into the directory you're currently in:\n", S_ERROR);
		printf("%shttp://kevinsnet.com/subsites/uni/IntroComputing/animals.db\n", S_ERROR);
		// We output our error, so exit with an error code.
		exit(1);
	}

	// Set &dbPosition to the beginning of the file, so that we can read some data from it, and then
	// set it back to this position to re-read some of it.
	fgetpos(dbHandle, &dbPosition);	

	// While we're not at the end of the DB file,
	while(!feof(dbHandle)){
		if(fgets(fileBuffer, 126, dbHandle)){
			// increase the linecount by one if it's not null.
			lineCount++;
		}
	}

	// And now we put ourself back at the beginning of the DB file.
	fsetpos(dbHandle, &dbPosition);

	// Set some counters to zero.
	cycleCount = 0;
	phaseCount = 0;

	// While we're not at the end of the DB file,
	while(!feof(dbHandle)){
		// read some of the data into fileBuffer
		if(fgets(fileBuffer, 126, dbHandle)){
			// then use strtok to split it by the entry delimiter defined above
			stringBuffer = strtok(fileBuffer, S_DBFILE_KEY_DELIM);
			// and if the result's not empty,
			while(stringBuffer != NULL){
				// and the phase counter is zero
				if(phaseCount == 0){
					// then we have an animal name. store it in the array.
					strcpy(dbAnimals[cycleCount], stringBuffer);
					// and toggle the phase for the next strtok operation.
					phaseCount = 1;
				// but if the phase count isn't zero
				}else{
					// then we have attribute flags. store it in the flags array.
					strcpy(dbFlags[cycleCount], stringBuffer);
					// and toggle the phase for the next strtok operation.
					phaseCount = 0;
				}
				// repeat strtok til we've read in all the entries in the DB.
				stringBuffer = strtok(NULL, S_DBFILE_ENTRY_DELIM);
			}
		// and increase the cycleCount.
		cycleCount++;
		}
	}
	// when we're done, we close the DB file.
	fclose(dbHandle);

	// more counters go back to zero.
	tmpInt = 0;
	phaseCount = 0;
	loopCount = 0;

	// while the loop counter is less than the number of lines in the file,
	while(loopCount < lineCount){ 		// print an animal and it's attributes, but only if debug mode is on. 		printf(debugMode>0?"%sAnimal '%s' with attributes '%s' loaded.\n":"",
			S_STATUS, dbAnimals[loopCount], dbFlags[loopCount]);
		// if this isn't the first iteration of the loop,
		if(loopCount > 0){
			// and the phase counter is more than one
		 	if(phaseCount > 1){
		 		// enter a newline and a tab into a temporary buffer.
				strcpy(tmpBuffer, "\n\t");
				// then set the phase counter back to zero.
				phaseCount = 0;
			// but if the phase counter isn't more than one,
			}else{
				// check the temporary integer (which should contain a strlen)
				// if it's more than seven,
				if(tmpInt > 7){
					// enter a single tab in the temporary buffer.
					strcpy(tmpBuffer, "\t");
				// if it's less than seven
				}else{
					// enter two tabs in the temporary buffer.
					strcpy(tmpBuffer, "\t\t");
				}
				// then increase the phase counter by one.
				phaseCount++;
			}
		// otherwise, if it is the first iteration of the loop,
		}else{
			// enter a single tab in the temporary buffer.
			strcpy(tmpBuffer, "\t");
		}
		// set the temporary integer to the length of the name of the current iteration numbered animal
		tmpInt = strlen(dbAnimals[loopCount]);
		// and add that animal's name to the temporary buffer.
		strcat(tmpBuffer, dbAnimals[loopCount]);
		// then put the contents of the temporary buffer into the animal list
		strcat(strAnimalList, tmpBuffer);
		// and increase the loop count by one.
		loopCount++;
	}
	/* This whole while loop results in the animal list
	 * string containing a nice, evenly formatted,
	 * three-column list of all the animals in the DB file.
	 * the strlen is used to calculate the amount of space
	 * needed between animal names to space the columns easily.
	 */

	if(cycleCount < 1 || strlen(dbAnimals[0]) == 0 || strlen(dbFlags[0]) == 0){
		printf("%sThe DB file is corrupt. Enter the following URL in a web browser,\n", S_ERROR);
		printf("%sand save the resulting file into the directory you're currently in:\n", S_ERROR);
		printf("%shttp://kevinsnet.com/subsites/uni/IntroComputing/animals.db\n", S_ERROR);
		exit(1);
	}

	// Display the number of animals loaded.
	printf("%s%u of %u animals loaded from the database.\n", S_STATUS, cycleCount, lineCount);
	// And call the function to output the nicely formatted list.
	listAnimals(strAnimalList);
	// Then just print out some help information.
	printf("\n%sTIP: At any menu, press the bolded key to select that option.\n", S_INFO);

	// And show them the main menu, passing it some useful information needed to generate the menu.
	doMainMenu(lineCount, dbAnimals, dbFlags, strAnimalList);	

	return 0;
}

// This function generates the main menu. It should never return, because it should always be there somewhere.
// It takes an integer containing the number of DB entries, an array of animals, an array of flags, and
// the stringed animal list as parameters.
void doMainMenu(int lineCount, char dbAnimals[totalAnimals][128], char dbFlags[totalAnimals][128], char strAnimalList[1024]) {
	// Give us an empty integer please.
	int keypress = 0;

	// Infinite loop. Really, if this exists, then maths as *I* know it has come to an end.
	while(1 == 1){
		// Print the menu, nicely formatted please.
		printf("\n%s[GAME MENU] %sN%sew game, %sQ%suit, %sL%sist known animals.\n",
			S_QUESTION, A_BOLD, A_NORM, A_BOLD, A_NORM, A_BOLD, A_NORM);

		// Get the user's selection from the standard input using unbufGetc().
		// unbufGetc is a custom function for getting input.
		keypress = unbufGetc(stdin);	

		// If the keypress is...
		switch(keypress) {
			// .. lowercase n
			case 78:
				// start the game, pass it some useful parameters.
				startGame(lineCount, dbAnimals, dbFlags);
				break;
			// .. uppercase n
			case 110:
				// start the game, pass it some useful parameters.
				startGame(lineCount, dbAnimals, dbFlags);
				break;
			// .. lowercase q
			case 81:
				// go to where we exit the application.
				quitGame();
				break;
			// .. uppercase q
			case 113:
				// go to where we exit the application.
				quitGame();
				break;
			// .. lowercase l
			case 76:
				// list the animals, pass it our animal list string
				listAnimals(strAnimalList);
				break;
			// .. uppercase l
			case 108:
				// list the animals, pass it our animal list string
				listAnimals(strAnimalList);
				break;
		}
	}
}

// This function quits the game with a successful exit code.
// It returns nothing and requires no parameters -- THERE IS NO RETURN.
void quitGame() {
	// Let the user know that they wanted to exit.
	// We'll try to make them feel bad about leaving, but it's really too late.
	printf("\n%sUser is exiting... Please don't leave ;_;\n", S_STATUS);
	// Exit, successfully, since there were no errors.
	exit(0);
}

// This function actually runs the game. It takes a couple parameters -- the number of files in the DB
// and the arrays of animals and flags. It returns nothing, as when it returns, the game ended somehow
// (either the user ended it or someone won/lost) and it returns to the main menu.
void startGame(int lineCount, char dbAnimals[totalAnimals][128], char dbFlags[totalAnimals][128]) {
	// Setup our variables.
	// For the most part, these are integers or empty strings/arrays we need.
	int userResponse = 0;
	// stateInt is special. We start this at 11, see below for the reasoning.
	int stateInt = 11;
	int oldInt = 0;
	int loopCount = 0;
	int possibleCount = 0;
	int tmpCount = 0;
	int questionCount = 0;
	// This is a multidimensional array. Each state will have it's own entry of
	// totalAnimals entries in the array. See below for information about the states.
	char possibleAnimals[99][totalAnimals][128];
	char possibleFlags[99][totalAnimals][128];
	// Also declare regex and regmatch pointers. These are used for the regexing.
	// .. I'll explain a below.
	regex_t *regex;
	regmatch_t *matches;

	/* Information:
	 * A lot happens below. For a start, there's going to be a series of while loops based
	 * around an integer, stateInt. Originally this was just a way to control the asking of
	 * questions, to allow us to just loop through a question until we got some kind of valid
	 * input, and the integers were just gonna be sequential from 0 up.

	 * In the end, it was decided that the state integer could also be used to prevent
	 * contradicting questions from being asked, so, questions in similar "groups" will have
	 * a similar digits in the tens-place (ie, transportation method - swimming/flying/underground
	 * all have a 1 in the tens-place) and then the question number is in the ones place.
	 * This makes it easier to keep track of.

	 * Since, as a result of this state numbering system, the tens place can go up to 9, the
	 * animals and flags arrays declared above require 99 arrays rather than the just-under-20
	 * that would otherwise be required.

	 * In addition, I decided to switch from using strtok() to parse up the parameters to
	 * using regexes. strtok() requires a lot of repetition and there is simply nothing better
	 * than a good cup of regexes when you're sifting through strings.
	 */

	// While the state is 11,
	while(stateInt == 11){
		// get the user's response to the question using a custom function, getUserAnswer().
		userResponse = getUserAnswer("Can your animal swim?");

		// If their answer was ...
		switch(userResponse){
			// ... yes,
			case 1:
				// zero some counters,
				possibleCount = 0;
				loopCount = 0;
				// then loop through the lines in the db, and for each one,
				while(loopCount < lineCount){ 					// compile a simple regex: /swimming/ 					regcomp(regex, "swimming", 0); 					// if the regex matches 					if(regexec(regex, dbFlags[loopCount], 0, matches, 0) == 0){ 						// and the entry is blank, 						if(strlen(dbAnimals[loopCount]) == 0){ 							// skip this cycle 							break; 						}; 						// but if it matches and it's not blank, print out some debug info if we want, 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, dbAnimals[loopCount]);
						// and then copy the animal and it's flags into the new arrays.
						strcpy(possibleAnimals[stateInt][possibleCount], dbAnimals[loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], dbFlags[loopCount]);
						possibleCount++;
					}
					// and the loop counter goes up one.
					loopCount++;
				}

				// if there are no animals that match at all,
				if(possibleCount == 0){
					// we lose
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					// print some stats
					endGame(questionCount);
					// and go back to the main menu.
					return;
				}
				// if there's one possible match,
				if(possibleCount == 1){
					// we won!
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					// print some stats
					endGame(questionCount);
					// and go back to the main menu.
					return;
				}
				// set oldInt to our current state integer,
				oldInt = stateInt;
				// increase our question count, and then set the state integer to the next state.
				questionCount++;
				stateInt = 21;
				break;
			// ... no,
			case 2:
				// zero some counters
				possibleCount = 0;
				loopCount = 0;
				// loop through each DB entry,
				while(loopCount < lineCount){ 					// compile a simple regex: /swimming/ 					regcomp(regex, "swimming", 0); 					// and then if it DOESN'T match, 					if(regexec(regex, dbFlags[loopCount], 0, matches, 0) != 0){ 						// and it's blank, 						if(strlen(dbAnimals[loopCount]) == 0){ 							// skip this one. 							break; 						}; 						// but if it's not blank, spit out some debug info, 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, dbAnimals[loopCount]);
						// and then copy it to the new arrays.
						strcpy(possibleAnimals[stateInt][possibleCount], dbAnimals[loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], dbFlags[loopCount]);
						possibleCount++;
					}
					// loop counter goes up.
					loopCount++;
				}
				// we lose again...
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				// we win again...
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				// oldInt is set to our state.
				oldInt = stateInt;
				// question counter goes up.
				questionCount++;
				// change state.
				stateInt = 12;
				break;
			// ... quit,
			case 3:
				// then quit back to the main menu
				return;
				break;
		}
	}

	// We're going to repeat this for each state, using new flags, and we're going to regex it
	// against the results in the arrays from the last state, rather than the array with the animals
	// from the DB. This results in the arrays getting smaller and smaller, resulting in a win or a
	// lose through the process of elimination.
	while(stateInt == 12){
		userResponse = getUserAnswer("Can your animal fly?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "flying", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 21;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "flying", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 13;
				break;
			case 3:
				return;
				break;
		}
	}

	// And again.
	while(stateInt == 13){
		userResponse = getUserAnswer("Does your animal live underground?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "underground", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 21;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "underground", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 21;
				break;
			case 3:
				return;
				break;
		}
	}

	// .... aaaand again.
	while(stateInt == 21){
		userResponse = getUserAnswer("Is your animal a feline?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "feline", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 31;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "feline", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 22;
				break;
			case 3:
				return;
				break;
		}
	}

	// ... Guess what we're doing again!?
	while(stateInt == 22){
		userResponse = getUserAnswer("Is your animal an arachnid?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "arachnid", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 31;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "arachnid", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 23;
				break;
			case 3:
				return;
				break;
		}
	}

	// Third time's a charm, but hey, we're well past our third run.
	while(stateInt == 23){
		userResponse = getUserAnswer("Is your animal an insect?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "insect", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 31;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "insect", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 24;
				break;
			case 3:
				return;
				break;
		}
	}

	// Again and again, and again and again.
	while(stateInt == 24){
		userResponse = getUserAnswer("Is your animal a canine?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "canine", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 31;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "canine", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 31;
				break;
			case 3:
				return;
				break;
		}
	}

	// Do it again, do it again! -- these are song lyrics, just so you know.
	while(stateInt == 31){
		userResponse = getUserAnswer("Can your animal be found in a jungle?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "jungle", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 91;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "jungle", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 32;
				break;
			case 3:
				return;
				break;
		}
	}

	// It's called "Again and again".
	while(stateInt == 32){
		userResponse = getUserAnswer("Can your animal be found on a farm?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "farm", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 91;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "farm", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 33;
				break;
			case 3:
				return;
				break;
		}
	}

	// "Again and again" which is by The Bird and the Bee.
	while(stateInt == 33){
		userResponse = getUserAnswer("Can your animal be found in the desert?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "desert", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 91;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "desert", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 91;
				break;
			case 3:
				return;
				break;
		}
	}

	// The song was used in a popular Mac music video. Oddly enough, it's called "Again and again"
	while(stateInt == 91){
		userResponse = getUserAnswer("Is your animal a herbivore?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "herbivore", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 92;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "herbivore", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 92;
				break;
			case 3:
				return;
				break;
		}
	}

	// Look it up on YouTube -- search for "Again and again".
	while(stateInt == 92){
		userResponse = getUserAnswer("Can your animal be domesticated?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "domesticated", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 93;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "domesticated", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 93;
				break;
			case 3:
				return;
				break;
		}
	}

	// In fact, I've done it for you -- "Again and again": http://uk.youtube.com/watch?v=6kxDxLAjkO8
	while(stateInt == 93){
		userResponse = getUserAnswer("Does your animal lay eggs?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "eggs", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 94;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "eggs", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 94;
				break;
			case 3:
				return;
				break;
		}
	}

	// You can also buy the song on iTunes -- it's called "Again and again"
	while(stateInt == 94){
		userResponse = getUserAnswer("Can your animal camouflage itself?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "camouflage", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 95;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "camouflage", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 95;
				break;
			case 3:
				return;
				break;
		}
	}

	// This is the last one. That's good, cause I'm running out of witty commentary.
	while(stateInt == 95){
		userResponse = getUserAnswer("Can you get milk from your animal?");

		switch(userResponse){
			case 1:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "milk", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) == 0){ 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){
							break;
						};
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 100;
				break;
			case 2:
				possibleCount = 0;
				loopCount = 0;
				while(loopCount < lineCount){ 					regcomp(regex, "milk", 0); 					if(regexec(regex, possibleFlags[oldInt][loopCount], 0, matches, 0) != 0){ 						if(strlen(possibleAnimals[oldInt][loopCount]) == 0){ 							break; 						}; 						printf(debugMode>0?"%sIt could be '%s'.\n":"", S_INFO, possibleAnimals[oldInt][loopCount]);
						strcpy(possibleAnimals[stateInt][possibleCount], possibleAnimals[oldInt][loopCount]);
						strcpy(possibleFlags[stateInt][possibleCount], possibleFlags[oldInt][loopCount]);
						possibleCount++;
					}
					loopCount++;
				}
				if(possibleCount == 0){
					printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
					endGame(questionCount);
					return;
				}
				if(possibleCount == 1){
					printf("%sYour animal is: %s!\n", S_WINNER, possibleAnimals[stateInt][0]);
					endGame(questionCount);
					return;
				}
				oldInt = stateInt;
				questionCount++;
				stateInt = 100;
				break;
			case 3:
				return;
				break;
		}
	}	

	// Using the default animals.db, there should be no way to get this far. But if you did, we admit defeat.
	printf("%sNope, I can't guess your animal. You outsmarted me...this time!\n", S_LOSER);
	endGame(questionCount);
	// And then we go back to the main menu so you can defeat us again. Cheater.
	return;

}

// This function generates and generates some statistics about the game played.
// It takes the question count as a paramter and returns nothing.
void endGame(int questionCount) {
	printf("%sYou answered %u questions.\n", S_INFO, (questionCount + 1));
	return;
}

// And this function takes our nicely formatted list of animals we made when the game
// first started as a parameter, and prints the list of animals. Then it returns nothing.
void listAnimals(char strAnimalList[1024]) {
	printf("%sI know the following animals:\n%s\n", S_INFO, strAnimalList);
	return;
}

// This displays the question (passed as parameter) we're asking the user, and their choices.
// It returns 1 if they selected YES, 2 if they selected NO, 3 if they selected QUIT, or
// 0 if the button they pressed didn't match any of the available selections.
int getUserAnswer(char *userQuestion) {
	// Output the question & choices.
	printf("%s[MENU] %s %sY%ses/%sN%so/%sQ%suit to menu\n",
		S_QUESTION, userQuestion, A_BOLD, A_NORM, A_BOLD, A_NORM, A_BOLD, A_NORM); 

	// Set the integer keypress to the number identifying the button the user pressed.
	// Call our own unbufGetc to do this, and get it from the standard input.
	int keypress = unbufGetc(stdin);

	// If the user pressed...
	switch(keypress){
			// .. lowercase y
			case 89:
				// we give a 1.
				return 1;
				break;
			// .. capital y
			case 121:
				// we give a 1.
				return 1;
				break;
			// .. lowercase n
			case 78:
				// we give a 2.
				return 2;
				break;
			// .. capital n
			case 110:
				// we give a 2.
				return 2;
				break;
			// .. lowercase q
			case 81:
				// and a 3...
				return 3;
				break;
			// .. capital q
			case 113:
				// and a 3...
			 	return 3;
			 	break;
			// or, if the user can't follow directions...
			default:
				// they get NOTHING.
				return 0;
				break;
	}

	// if we get this far, switch() broke, so we'll give you nothing.
	return 0;
}

// and the function you heard so much about before, unbufGetc.
// this takes a file point as a parameter (it'll usually be stdin)
// and returns a keypress code. it's a little more complicated though.
int unbufGetc(FILE *getcHandle) {
	// this function is why I included termios.h...
	// make two structs to hold terminal data in.
	struct termios termIOold;
	struct termios termIOnew;

	// store the current terminal atrributes in one of those structs.
	tcgetattr(0, &termIOold);

	// and make the other struct match the first.
	termIOnew = termIOold;
	// now we're going to change some of the terminals parameters so that
	// when the user presses a button, it is directly passed to us without
	// waiting for them to press return/enter to pass it to us.
	// (this is what the unbuf means, it's an unbuffered input.)
	termIOnew.c_lflag &= ~(ICANON|ECHO);
	tcsetattr(0, TCSANOW, &termIOnew);

	// get the keypress! it'll only be a single one since we're not buffering
	// keypresses at this point.
	int keypress = getc(getcHandle);	

	// put the terminal back to normal.
	tcsetattr(0, TCSANOW, &termIOold);

	/* we could just leave the terminal in an unbuffered state for the duration of
	 * the execution and set it back to normal on exit, but if we do that then we
	 * risk the user having a cocked up terminal if they stop execution uncleanly
	 * (for example by pressing ^C or killing our PID)... so changing it back and
	 * forth several dozen times is a slightly more friendly way to do it.
	 */

	// and return the keypress as an integer.
	return keypress;
}

Database file: animals.txt

giraffe:herbivore
trout:jungle herbivore swimming domesticated eggs
tiger:jungle feline
elephant:jungle herbivore
jellyfish:herbivore swimming eggs
eagle:flying eggs
piranha:jungle swimming eggs
hummingbird:herbivore flying eggs
parrot:jungle flying
dolphin:swimming
spider:arachnid eggs
housecat:domesticated feline farm
dog:domesticated canine farm
wolf:canine
squid:swimming camouflage herbivore eggs
iguana:camouflage herbivore desert domesticated eggs
camel:desert milk herbivore
cow:farm milk
rabbit:herbivore desert domesticated
chicken:flying domesticated herbivore eggs
fly:flying insect eggs
squirrel:herbivore
hamster:herbivore domesticated
roadrunner:flying eggs desert
sheep:herbivore farm milk
butterfly:insect flying
badger:underground
ferrets:domesticated
ant:insect eggs
toucan:domesticated flying eggs