[c]#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//windows.h for color
#include <windows.h>
//globals...
char * data[13];
int answers[13] = { 0, 4, 2, 4, 1, 3, 3, 2, 4, 4, 2, 5, 4 };
bool set[13];
//globals
HANDLE hConsole;
const int COLOR_RED = 12;
const int COLOR_GREEN = 10;
const int COLOR_BLUE = 1;
const int COLOR_YELLOW = 14;
const int COLOR_WHITE = 15;
const int COLOR_FORE = 15;
const int COLOR_BACK = 8;
/*
* Colors:
* forecolor + (backcolor * 16) = full color setup
* 0 is black
*/
//function prototypes
void loadData();
void prepData();
void showData(int line);
void printHR();
void printHR(int width);
void setColors(int fore, int back);
void setColors(int colorValue);
//main function - entry point
void main()
{
//get handle to the console
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
srand(time(NULL));
loadData();
//setup colors to start
setColors(COLOR_FORE, COLOR_BLUE);
//setColors(115);
system("cls");
printf("\n");
printf("\tWelcome to the game!\n\tTo play, choose the one that doesn't belong.\n");
bool quit = false;
bool ingame = false;
while (!quit)
{
printf("\n");
printf("\t==Main Menu==\n");
printf("\t1. Play Game\n");
printf("\t2. Quit\n");
//print hr
printHR();
char menuChoice;
printf("\tYour choice: ");
//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);
//cin >> menuChoice;
scanf("%s", &menuChoice);
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
switch(menuChoice)
{
case '1':
system("cls");
ingame = true;
break;
case '2':
system("cls");
ingame = false;
quit = true;
break;
default:
system("cls");
printf("\n\tPlease make a valid choice!\n");
ingame = false;
break;
}
printf("\n");
int completed = 0;
int totalQuestions = 12;
int correct = 0;
bool gameCompleted = false;
prepData();
char initials[256];
bool initialsFlag = false;
if (ingame)
{
printf("\n");
printf("\tPlease enter your initails: ");
//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);
//get the user's initials (or anything up to 256 chars)
//cin.ignore(1, '\n');
//cin.getline(initials,256);
scanf("%256s", &initials);
printf("\n");
//reset colors
setColors(COLOR_FORE, COLOR_BLUE);
initialsFlag = true;
}
while (ingame)
{
if (!initialsFlag)
{
printf("\n");
printf("\tPlease enter your initails: ");
//cin >> initials;
scanf("%256s", &initials);
printf("\n");
initialsFlag = true;
}
if (completed >= totalQuestions)
{
gameCompleted = true;
ingame = false;
break;
}
int qnum = 0;
bool validQuestion = false;
while (!validQuestion)
{
qnum = rand()%12 + 1; //1-12
if (set[qnum])
continue;
else
validQuestion = true;
}
set[qnum] = true;
printf("\n");
showData(qnum);
char answerChoice;
bool ready = false;
while (!ready)
{
printf("\tQ = Quit.\n");
//print HR
printHR();
printf("\tYour choice: ");
//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);
//cin >> answerChoice;
scanf("%s", &answerChoice);
//reset colors
setColors(COLOR_FORE, COLOR_BLUE);
int a = (int)answerChoice - 48;
if (a == (int)'q' - 48)
{
ready = true;
ingame = false;
system("cls");
}
else if (a < 1 || a > 5)
{
//system("cls");
//cout << endl;
//print HR
printHR();
//set colors
setColors(COLOR_RED, COLOR_BLUE);
printf("\tInvalid Choice!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
}
else if (a == answers[qnum])
{
//system("cls");
//cout << endl;
//print HR
printHR();
//set colors
setColors(COLOR_GREEN, COLOR_BLUE);
printf("\tCorrect!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
correct++;
completed++;
ready = true;
printf("\t");
system("PAUSE");
system("cls");
}
else
{
//system("cls");
//cout << endl;
//print HR
printHR();
//set colors
setColors(COLOR_RED, COLOR_BLUE);
printf("\tIncorrect! Nice try!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
completed++;
ready = true;
printf("\t");
system("PAUSE");
system("cls");
}
}
}
if (gameCompleted)
{
system("cls");
double percentCorrect = ((double)correct / (double)totalQuestions) * 100;
printf("\n");
if (!initialsFlag)
printf("\tCongratulations on completing the game!\n");
else
printf("\tCongratulations on completing the game %s!\n", initials);
//print HR
printHR();
//set colors
setColors(COLOR_GREEN, COLOR_BLUE);
printf("\tYou got %d out of %d correct!", correct, totalQuestions);
printf("\n");
printf("\tThat is %d Percent!\n", (int)percentCorrect);
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
printHR();
printf("\t");
system("PAUSE");
system("cls");
}
}
}
//load data
void loadData()
{
for (int i = 1; i < 13; i++)
{
data = (char*)malloc(65);
}
data[1]="1 rose, 2 daffodil, 3 tulip, 4 cauliflower, 5 carnation";
data[2]="1 hen, 2 hare, 3 duck, 4 goose, 5 turkey";
data[3]="1 beech, 2 elm, 3 oak, 4 violet, 5 ash";
data[4]="1 slate, 2 gold, 3 silver, 4 iron, 5 lead";
data[5]="1 potato, 2 turnip, 3 lemon, 4 carrot, 5 beetroot";
data[6]="1 oats, 2 wheat, 3 scones, 4 barley, 5 rye";
data[7]="1 salmon, 2 whale, 3 herring, 4 mackerel, 5 cod";
data[8]="1 man, 2 boy, 3 master, 4 princess, 5 uncle";
data[9]="1 shield, 2 lance, 3 dagger, 4 gun, 5 spear";
data[10]="1 orange, 2 potato, 3 cherry, 4 apple, 5 banana";
data[11]="1 cottage, 2 mansion, 3 palace, 4 bungalow, 5 warehouse";
data[12]="1 red, 2 blue, 3 yellow, 4 skin, 5 green";
}
void prepData()
{
for (int i = 0; i < 13; i++)
{
set = false;
}
}
//show data
void showData(int line)
{
printf("\tQuestion: %s \n", data[line]);
}
//print a horizontal rule - default = 50
void printHR()
{
printf("\t__________________________________________________\n\n");
}
void printHR(int width)
{
printf("\t");
for (int i = 0; i < width; i++)
{
printf("_");
}
printf("\n\n");
}
//change the console colors
void setColors(int fore, int back)
{
SetConsoleTextAttribute(hConsole, (fore + (back * 16)));
//must put a system("cls") after this because
//colors only change fully if the screen is cleared.
//if no cls, then only the text and its direct
//background are affected. calling cls does the whole
//console.
}
void setColors(int colorValue)
{
SetConsoleTextAttribute(hConsole, colorValue);
}[/c]