1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
//Displays a random squence of squares and the user
//needs to memorize and try to recreate the sequence
#include <iostream>
#include <ctime>
#include "graph1.h"
using namespace std;
void getNoColors(int* no_colors);
void setColors(int* colors, int no_colors);
void getDelay(int* secs);
void displayColors(int* colors, int no_colors);
void game(int* colors, int no_colors);
//Add prototypes here
int main()
{
//Variable Declaration/Initialization
const int MAX_COLORS = 5;
int no_colors = 0;
int colors[MAX_COLORS];
//Display Graphics Window
displayGraphics();
//1. Get the # of colors
getNoColors(&no_colors);
//2. Set the colors
setColors(colors, no_colors);
//3. Display the colors in the form of 75x75 rectangles
displayColors(colors,no_colors);
game(colors,no_colors);
return 0;
}
//Implement functions here
//Get Number of Colors function
void getNoColors(int* no_colors)
{
cout << "Input a number of colors:" ;
cin>>*no_colors;
}
//Randomly generate numbers for the Squares
void setColors(int* colors, int no_colors)
{
int i = 0;
int j= 0;
bool duplicate = false;
//initialize random # generator
srand(time(0));
//Generate random (but unique) colors
for (i = 0; i< no_colors; i++)
{
do
{
colors[i] = rand()%no_colors;
duplicate = false;
//check for duplicates
for (j= i-1; j >= 0; j--)
{
if (colors[i] == colors[j])
{
duplicate = true;
break;
}
}
}
while(duplicate);
}
}
//Display squares matched to the randomly generated number
void displayColors(int* colors, int no_colors)
{
int x=25, obj_no,i;
for(i=0; i < no_colors ; i++)
{
obj_no = drawRect(x,25,75,75);
gout<<setPos(x,150)<<"Color #"<<(i+1);
for (int m=0; m< no_colors; m++)
{
if (colors[i] == 0)
{
setColor(obj_no,255,0,0);
}
if (colors[i] == 1)
{
setColor(obj_no,0,255,0);
}
if (colors[i] == 2)
{
setColor(obj_no,0,0,255);
}
if (colors[i] == 3)
{
setColor(obj_no,255,0,255);
}
if (colors[i] == 4)
{
setColor(obj_no,0,255,255);
}
x =x+25;
}
}
Sleep(2000);
for (i=0;i<no_colors;i++)
{
removeObject (obj_no);
}
}
void game(int* colors, int no_colors)
{
//Declared varibles
int i,obj_no,m,x=0,y=0;
obj_no=drawRect(25,250,75,75);
setColor(obj_no, 255,0,0);
obj_no=drawRect(125,250,75,75);
setColor(obj_no,0,255,0);
obj_no=drawRect(225,250,75,75);
setColor(obj_no,0,0,255);
obj_no=drawRect(325,250,75,75);
setColor(obj_no,255,255,0);
obj_no=drawRect(425,250,75,75);
setColor(obj_no,255,0,255);
if (leftMouse(x,y))
{
for (int m=0; m< no_colors; m++)
{
if ((x>25)&&(x<100)&&(y>250)&&(y<325))
{
obj_no=colors[i];
}
}
}
}
|