Browsing articles tagged with " c"

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

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