/********************************************************************
Write a C++ program that implements a memory game where the user is asked to
remember colors that are displayed randomly for a short period of time.
********************************************************************/
#include <iostream>
#include <ctime>
#include "graph1.h"
using namespace std;
//Function declarations
void getNoColors(int* no_colors);
void setColors(int* colors, int no_colors);
void getDelay(int* no_secs);
void displayColors(int* colors, int no_colors, int delay);
void displayColors2(int* colors, int no_colors, int delay);
void game(int* colors, int no_colors, int delay);
int main()
{
//variable declaration
const int MAX_COLORS = 5;
int no_colors = 0;
int delay = 0;
int no_secs = 0;
int colors[MAX_COLORS];
//display graphics window
displayGraphics();
//assign number of colors
getNoColors(&no_colors);
//set colors randomly
setColors(colors, no_colors);
//get the delay from user
getDelay(&no_secs);
delay = no_secs;
//perform game
game(colors, no_colors, delay);
return 0;
}
//Prompt user for number of colors to display
void getNoColors(int* no_colors)
{
cout << "Enter the number of colors to display (Between 2 and 5 inclusively): ";
cin >> *no_colors;
if ((*no_colors < 2) || (*no_colors > 5)){
cout << "Please enter a valid integer between 2 and 5: ";
cin >> *no_colors;
}
}
//Assign colors randomly to each rectangle
void setColors(int* colors, int no_colors)
{
int i = 0;
int j = 0;
bool duplicate = true;
//Random generator
srand(time(0));
for (i=0; i<no_colors; i++)
{
//Generate random #
colors[i] = rand()%5;
do
{
//Initiate duplicate to false
duplicate = false;
for (j=0; j<i; j++)
{
//if two numbers are same, redo entire random sequence
if (colors[i] == colors[j])
{
//Reset duplicate variable
duplicate = true;
//Generate random #
do
{
colors[i] = rand()%5;
}while(colors[i] == colors[j]);
}
}
}while(duplicate);
}
}
//Prompt user for the delay
void getDelay(int* no_secs)
{
cout << "Enter delay (Between 1 and 3 inclusively): ";
cin >> *no_secs;
if ((*no_secs < 1) || (*no_secs > 3)){
cout << "Please choose a valid integer between 1 and 3: ";
cin >> *no_secs;
}
/*
clock_t endwait;
endwait = clock () + *no_secs * CLOCKS_PER_SEC ;
while (clock() < endwait) {}*/
}
void displayColors(int* colors, int no_colors, int delay)
{
for (int i=0; i<no_colors; i++)
{
//Draw the rectangle, assign it to obj_no
int obj_no = drawRect((50 + (i * 100)),75,75,75);
//Set color of rectangle
if (colors[i] == 0)
setColor(obj_no,255,0,0); //red
else if (colors[i] == 1)
setColor(obj_no,0,255,0); //green
else if (colors[i] == 2)
setColor(obj_no,0,0,255); //blue
else if (colors[i] == 3)
setColor(obj_no,255,255,0); //yellow
else if (colors[i] == 4)
setColor(obj_no,0,255,255); //magenta
//Display text below colors
gout << setPos(50 + (i * 100),170) << "Color #" << (i+1) << endg;
}
/*
clock_t endwait;
endwait = clock () + delay * CLOCKS_PER_SEC ;
while (clock() < endwait) {}*/
int t = clock();
int timer = clock() - t;
while ( (timer / 1000) < delay) // while prog reaches inputted time
{
int timer = clock() - t;
//cout << "Seconds since started: " << timer / 1000 << endl; // Print the time since the program started in seconds
if ((timer/1000) == delay){
clearGraphics();
break;
}
}
}
//Display second set of colors
void displayColors2(int* colors, int no_colors, int delay)
{
for (int i=0; i<no_colors; i++)
{
//Draw the rectangle, assign it to obj_no2
int obj_no2 = drawRect((50 + (i * 100)),300,75,75);
//Set color of rectangle
if (colors[i] == 0)
setColor(obj_no2,255,0,0); //red
else if (colors[i] == 1)
setColor(obj_no2,0,255,0); //green
else if (colors[i] == 2)
setColor(obj_no2,0,0,255); //blue
else if (colors[i] == 3)
setColor(obj_no2,255,255,0); //yellow
else if (colors[i] == 4)
setColor(obj_no2,0,255,255); //magenta
}
}
//Run game
void game(int* colors, int no_colors, int delay)
{
//display colors
displayColors(colors, no_colors, delay);
//Rerun the setcolors
setColors(colors, no_colors);
//Re-display colors in separate order below
displayColors2(colors, no_colors, delay);
int score = 0;
int x = 0;
int y = 0;
bool correct = false;
bool incorrect = false;
while(true)
{
if (leftMouse(x, y))
{
//check for colors
//box 1
if ( (x < 50) && (x > 125) && (y < 300) && (y > 375) ){
if...
}
//Set second set of colored boxes as buttons
//Set click event ( leftMouse(x,y) )
//If correct color, previous rectangle is redrawn at original point and +2 is added to score
//Else, -2 from score
//Continues until player has correctly selected all colors as they were in original displayColors function
//Display message with score and time elapsed in graphics window
}