Advertisement
pablopalacios

memory game

Nov 22nd, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  Exercise 12.16
  4. //
  5. //  Created by Gustavo Silveira on 11/21/13.
  6. //  Copyright (c) 2013 Gustavo Silveira. All rights reserved.
  7. //
  8. // !! The "system("clear")" doesn't work on my compiler, but maybe it will work on yours.
  9.  
  10. #include <iostream>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. #include <cstdlib>
  14. #include <unistd.h>
  15. #include <term.h>
  16.  
  17. using namespace std;
  18.  
  19. int C(4);
  20. int r1, c1, r2, c2; //row one and two, collum one and two
  21. int a,b,c,d;
  22.  
  23. int tries = 0; //accumulator for how many tries untill the end
  24.  
  25.  
  26. char front[4][4] = {
  27.   {'1','2','3','4'},
  28.   {'5','6','7','8'},
  29.   {'1','2','3','4'},
  30.   {'5','6','7','8'},
  31. };
  32. char back [4][4] = {
  33.   {'*','*','*','*'},
  34.   {'*','*','*','*'},
  35.   {'*','*','*','*'},
  36.   {'*','*','*','*'},
  37. };
  38.  
  39. void swap(char&a, char&b);
  40. void sort(char Front[][4]);
  41. void print(char ar[][4]);
  42. bool check(char Back[][4]);
  43. void input();
  44.  
  45.  
  46. int main ()
  47. {
  48.   srand(time(0));
  49.   system("clear");
  50.   sort(front); //Shuffle numbers in the chosen array
  51.   print(back); //Print the game
  52.    
  53.   while(true) {
  54.    
  55.     input(); //Inputs and checks if it is a valid number
  56.        
  57.     if (front[r1][c1] == front[r2][c2]) { //If the two cards match it will print those cards
  58.       back[r1][c1] = front[r1][c1];
  59.       back[r2][c2] = front[r2][c2];
  60.       tries++;
  61.        
  62.        
  63.       system("clear"); //Clear screen (doesn't work on XCode)
  64.        
  65.       check(back);
  66.       print(back);
  67.        
  68.     }
  69.     if (front[r1][c1] != front[r2][c2]) { //If cards don't match it will print and after some seconds it be back "*"
  70.        
  71.       back[r1][c1] = front[r1][c1];
  72.       back[r2][c2] = front[r2][c2];
  73.        
  74.       system("clear");
  75.        
  76.       print(back);
  77.        
  78.       cout << endl;
  79.        
  80.       sleep(3); //Wait to come to the previous screen
  81.        
  82.       system("clear");
  83.        
  84.       back[r1][c1] = '*';
  85.       back[r2][c2] = '*';
  86.        
  87.       print(back);
  88.        
  89.     }
  90.     tries++; //count how many tries untill find all the matches
  91.        
  92.     if (check(back) == true) { //Check if the game is over and tells how many tries were needed
  93.       cout << "You finished in " << tries << " tries.";
  94.       cout << endl;
  95.       break;
  96.     }
  97.   }
  98.    
  99.   return 0;
  100. }
  101.  
  102. void input() {
  103.    
  104.   cout << endl << "Type 4 ints separated by a space, row 1, column 1, row 2, column 2: ";
  105.   cin >> a >> b >> c >> d;
  106.   cout << endl;
  107.    
  108.    
  109.   r1=a-1;
  110.   c1=b-1;
  111.   r2=c-1;
  112.   c2=d-1;
  113.    
  114.    
  115.    
  116.   while (((r1==r2) && (c1==c2)) || ((r1>=C)||(c1>=C)||(r2>=C)||(c2>=C))) {
  117.     cout << "Wrong value, try different cards: ";
  118.     cin >> a >> b >> c >> d;
  119.     cout << endl;
  120.        
  121.     r1=a-1;
  122.     c1=b-1;
  123.     r2=c-1;
  124.     c2=d-1;
  125.        
  126.   }
  127. }
  128.  
  129. bool check(char Back[][4]) {
  130.   for (int r=0; r<C; r++) {
  131.     for (int c=0; c<C; c++) {
  132.       if (back[r][c] == '*') {
  133.     return false;
  134.       }
  135.     }
  136.   }
  137.    
  138.   return true;
  139. }
  140.  
  141. void print(char ar[][4]) {
  142.   cout << "############## Memory Game ##############\n";
  143.   cout << "   1  2  3  4\n";
  144.   cout << "   ----------\n";
  145.   for (int r=0; r<C; r++) {
  146.     cout << r+1 << "| ";
  147.     for (int c=0; c<C; c++) {
  148.       cout << ar[r][c] << "  ";
  149.     }
  150.     cout << endl;
  151.   }
  152. }
  153.  
  154. void swap(char&a, char&b) {
  155.   char tmp = a;
  156.   a = b;
  157.   b = tmp;
  158. }
  159.  
  160. void sort(char Front[][4]) {
  161.   for (int i=0; i<500; i++) {
  162.     swap(front[rand()%4][rand()%4], front[rand()%4][rand()%4]);
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement