Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Feb 2nd, 2013  |  syntax: C++  |  size: 5.10 KB  |  hits: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /********************************************************************
  2. Write a C++ program that implements a memory game where the user is asked to
  3. remember colors that are displayed randomly for a short period of time.
  4. ********************************************************************/
  5.  
  6. #include <iostream>
  7. #include <ctime>
  8. #include "graph1.h"
  9.  
  10.  
  11. using namespace std;
  12.  
  13. //Function declarations
  14. void getNoColors(int* no_colors);
  15. void setColors(int* colors, int no_colors);
  16. void getDelay(int* no_secs);
  17. void displayColors(int* colors, int no_colors, int delay);
  18. void displayColors2(int* colors, int no_colors, int delay);
  19. void game(int* colors, int no_colors, int delay);
  20.  
  21.  
  22. int main()
  23. {
  24.         //variable declaration
  25.         const int MAX_COLORS = 5;
  26.  
  27.         int no_colors = 0;
  28.         int delay = 0;
  29.         int no_secs = 0;
  30.         int colors[MAX_COLORS];
  31.  
  32.         //display graphics window
  33.         displayGraphics();
  34.  
  35.  
  36.         //assign number of colors
  37.         getNoColors(&no_colors);
  38.  
  39.         //set colors randomly
  40.         setColors(colors, no_colors);
  41.  
  42.         //get the delay from user
  43.         getDelay(&no_secs);
  44.         delay = no_secs;
  45.        
  46.         //perform game
  47.         game(colors, no_colors, delay);
  48.        
  49.  
  50.  
  51.         return 0;
  52. }
  53. //Prompt user for number of colors to display
  54. void getNoColors(int* no_colors)
  55. {
  56.  
  57.         cout << "Enter the number of colors to display (Between 2 and 5 inclusively): ";
  58.         cin >> *no_colors;
  59.  
  60.         if ((*no_colors < 2) || (*no_colors > 5)){
  61.                 cout << "Please enter a valid integer between 2 and 5: ";
  62.                 cin >> *no_colors;
  63.         }
  64.  
  65.  
  66. }
  67. //Assign colors randomly to each rectangle
  68. void setColors(int* colors, int no_colors)
  69. {
  70.         int i = 0;
  71.         int j = 0;
  72.         bool duplicate = true;
  73.  
  74.         //Random generator
  75.         srand(time(0));
  76.  
  77.         for (i=0; i<no_colors; i++)
  78.         {
  79.                 //Generate random #
  80.                 colors[i] = rand()%5;          
  81.  
  82.                 do
  83.                 {
  84.                         //Initiate duplicate to false
  85.                         duplicate = false;
  86.  
  87.                         for (j=0; j<i; j++)
  88.                         {
  89.                                 //if two numbers are same, redo entire random sequence
  90.                                 if (colors[i] == colors[j])
  91.                                 {
  92.                                         //Reset duplicate variable
  93.                                         duplicate = true;
  94.  
  95.                                         //Generate random #
  96.                                         do
  97.                                         {
  98.                                                 colors[i] = rand()%5;
  99.                                         }while(colors[i] == colors[j]);
  100.                                 }
  101.                         }
  102.                 }while(duplicate);
  103.         }
  104. }
  105. //Prompt user for the delay
  106. void getDelay(int* no_secs)
  107. {
  108.         cout << "Enter delay (Between 1 and 3 inclusively): ";
  109.         cin >> *no_secs;
  110.  
  111.         if ((*no_secs < 1) || (*no_secs > 3)){
  112.                 cout << "Please choose a valid integer between 1 and 3: ";
  113.                 cin >> *no_secs;
  114.         }
  115.  
  116.         /*
  117.         clock_t endwait;
  118.         endwait = clock () + *no_secs * CLOCKS_PER_SEC ;
  119.         while (clock() < endwait) {}*/
  120.  
  121.  
  122. }
  123.  
  124. void displayColors(int* colors, int no_colors, int delay)
  125. {
  126.         for (int i=0; i<no_colors; i++)
  127.         {
  128.                 //Draw the rectangle, assign it to obj_no
  129.                 int obj_no = drawRect((50 + (i * 100)),75,75,75);
  130.  
  131.                 //Set color of rectangle
  132.                 if (colors[i] == 0)
  133.                         setColor(obj_no,255,0,0);       //red
  134.                 else if (colors[i] == 1)
  135.                         setColor(obj_no,0,255,0);       //green
  136.                 else if (colors[i] == 2)
  137.                         setColor(obj_no,0,0,255);       //blue
  138.                 else if (colors[i] == 3)
  139.                         setColor(obj_no,255,255,0);     //yellow
  140.                 else if (colors[i] == 4)
  141.                         setColor(obj_no,0,255,255);     //magenta      
  142.  
  143.  
  144.                 //Display text below colors
  145.                 gout << setPos(50 + (i * 100),170) << "Color #" << (i+1) << endg;
  146.         }
  147.         /*
  148.         clock_t endwait;
  149.         endwait = clock () + delay * CLOCKS_PER_SEC ;
  150.         while (clock() < endwait) {}*/
  151.  
  152.         int t = clock();
  153.         int timer = clock() - t;
  154.         while ( (timer / 1000) < delay) // while prog reaches inputted time
  155.         {              
  156.                 int timer = clock() - t;
  157.                 //cout << "Seconds since started: " << timer / 1000 << endl; // Print the time since the program started in seconds
  158.  
  159.                 if ((timer/1000) == delay){
  160.                         clearGraphics();
  161.                         break;
  162.                 }
  163.         }
  164.  
  165.  
  166. }
  167. //Display second set of colors
  168. void displayColors2(int* colors, int no_colors, int delay)
  169. {
  170.         for (int i=0; i<no_colors; i++)
  171.         {
  172.                 //Draw the rectangle, assign it to obj_no2
  173.                 int obj_no2 = drawRect((50 + (i * 100)),300,75,75);
  174.  
  175.                 //Set color of rectangle
  176.                 if (colors[i] == 0)
  177.                         setColor(obj_no2,255,0,0);      //red
  178.                 else if (colors[i] == 1)
  179.                         setColor(obj_no2,0,255,0);      //green
  180.                 else if (colors[i] == 2)
  181.                         setColor(obj_no2,0,0,255);      //blue
  182.                 else if (colors[i] == 3)
  183.                         setColor(obj_no2,255,255,0);    //yellow
  184.                 else if (colors[i] == 4)
  185.                         setColor(obj_no2,0,255,255);    //magenta      
  186.  
  187.         }
  188.  
  189. }
  190. //Run game
  191. void game(int* colors, int no_colors, int delay)
  192. {      
  193.         //display colors
  194.         displayColors(colors, no_colors, delay);
  195.  
  196.         //Rerun the setcolors
  197.         setColors(colors, no_colors);
  198.  
  199.         //Re-display colors in separate order below
  200.         displayColors2(colors, no_colors, delay);
  201.        
  202.         int score = 0;
  203.         int x = 0;
  204.         int y = 0;
  205.         bool correct = false;
  206.         bool incorrect = false;
  207.  
  208.         while(true)
  209.         {
  210.                 if (leftMouse(x, y))
  211.                 {
  212.                         //check for colors
  213.                         //box 1
  214.                         if ( (x < 50) && (x > 125) && (y < 300) && (y > 375) ){
  215.                                 if...
  216.                         }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.         //Set second set of colored boxes as buttons
  223.         //Set click event ( leftMouse(x,y) )
  224.         //If correct color, previous rectangle is redrawn at original point and +2 is added to score
  225.         //Else, -2 from score
  226.         //Continues until player has correctly selected all colors as they were in original displayColors function
  227.         //Display message with score and time elapsed in graphics window
  228.        
  229. }