Advertisement
nmh-illusion

gameOpenTheSamePair

Mar 29th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int status[5][5], answer[4][4], c, x1, x2, y11, y2;
  5. char cover[5][5];
  6.  
  7.  
  8. void khoitao(void);
  9. void openPoint(int* , int*);
  10. void display(void);
  11. void kiemtra(void);
  12.  
  13.  
  14. /*              Start CODE              */
  15.  
  16. int main(void){
  17.    
  18.     srand(time(0));
  19.     khoitao();
  20.    
  21.     while(1){
  22.        
  23.         c= 0;
  24.         display();
  25.        
  26.         openPoint(&x1, &x2);
  27.         system ("cls");
  28.         status[x1][x2]=1;
  29.         display();
  30.        
  31.         openPoint(&y11, &y2);
  32.         system ("cls");
  33.         status[y11][y2]=1;
  34.         display();
  35.    
  36.         kiemtra();
  37.    
  38.     /*  Kiem tra Ket Thuc   */
  39.         int i, j;
  40.         for (i=0; i<5; i++)
  41.             for (j=0; j<5; j++)
  42.                 if (status[i][j]==0) c++;
  43.            
  44.         if (c==0) break;
  45.    
  46.    
  47.     /*  Exit?   */     
  48.         char t;
  49.         printf ("\n\t'x' de thoat: "); fflush(stdin); scanf ("%c", &t);
  50.         if (t=='x' || t=='X') return 0;
  51.         fflush(stdin);
  52.        
  53.         system("cls");
  54.     }
  55.    
  56.    
  57.     /*      Chuc mung   */
  58.     printf ("\n\n\t\tCongratulation!\n\tBan da mo thanh cong het tat ca cac o!\n\n");
  59.     getch();
  60.     printf ("\n\n\tCam on da choi game, hen gap lai!\n");
  61.    
  62.     getch();
  63.     return 0;
  64. }
  65.  
  66. /*   KET THUC CHUONG TRINH  */
  67.  
  68.  
  69.  
  70. // Khoi tao
  71. void khoitao(void){
  72.     int ra[16], h=0, i, j;
  73.    
  74.     for (i=0; i<5; i++)
  75.         for (j=0; j<5; j++){
  76.             if (i==0 || j==0) {
  77.                 status[i][j]=1;
  78.                 if (i==0) cover[i][j]= (char) i;
  79.                     else if (j==0) cover[i][j]= (char) j;
  80.             }
  81.             else {
  82.                 status[i][j]= 0;
  83.                 cover[i][j]= '*';
  84.             }
  85.         }
  86.    
  87.     /* Tao bang ket qua*/
  88.    
  89.     ra[0]= (rand()%8)+1;
  90.    
  91.     for (i=1; i<16;){
  92.         int x, t;
  93.        
  94.         t=0;
  95.         x= rand()%8+1;
  96.        
  97.         for (j=0; j<i;j++){
  98.             if (x==ra[j]) t++;
  99.         }
  100.        
  101.         if (t<2){
  102.             ra[i]=x;
  103.             i++;
  104.         }
  105.     }
  106.        
  107.     for (i=0; i<4; i++){
  108.         for (j=0; j<4; j++){
  109.             answer[i][j]= ra[h];
  110.             h++;
  111.         }
  112.     }
  113.  
  114.    
  115.     /*  END Tao bang ket qua*/
  116. }
  117. //End khoi tao
  118.  
  119.  
  120. // Chon diem
  121. void openPoint(int *x, int *y){
  122.     int xa, ya, i, j;  
  123.    
  124.     //x1=*x; y1= *y;
  125.     loop:
  126.     printf ("\nChon vi tri de mo (x, y): "); scanf ("%d%d", &xa, &ya);
  127.    
  128.     if (xa<1 || ya<1 || xa>4 || ya>4) {
  129.         printf ("\nVi tri ban chon khong hop le, vui long chon lai.");
  130.         getch();
  131.         goto loop;
  132.     }
  133.     else{
  134.         *x=xa; *y=ya;
  135.     }
  136. }
  137. //End Chon Diem
  138.  
  139.  
  140. // Hien Thi
  141. void display(void){
  142.     int i, j;
  143.        
  144.         /*      Tao BANNER      */
  145.         printf ("\t\t\t     \t\t\t\tcoder:NguyenMinhHieu\n\n\t\t   ==Game OPEN THE SAME PAIR==\n\n");
  146.         /*       Het BANNER         */
  147.        
  148.         for (i=0; i<5; i++)
  149.         for (j=0; j<5; j++){
  150.             if (i==0 || j==0) {
  151.                 status[i][j]=1;
  152.                 if (i==0) cover[i][j]= 'y';
  153.                     else if (j==0) cover[i][j]= 'x';
  154.             }
  155.             else {
  156.                 cover[i][j]= '*';
  157.             }
  158.         }
  159.        
  160.         cover[0][0]=2;
  161.    
  162.     printf ("\t");
  163.    
  164.     for (i=0; i<5; i++){
  165.         for (j=0; j<5; j++){           
  166.             if (status[i][j]==1 && i!=0 && j!=0) cover[i][j]=answer[i-1][j-1]+48;
  167.             printf ("\t%c", cover[i][j]);
  168.         }
  169.         printf ("\n\n\n\t");
  170.     }
  171. }
  172. //End  Hien thi
  173.  
  174.  
  175. //Kiem Tra
  176. void kiemtra(void){
  177.    
  178.     if (x1==y11 && x2==y2) printf ("\nBan da chon cung mot o!");
  179.         else if (answer[x1-1][x2-1]!=answer[y11-1][y2-1])
  180.             status[x1][x2]=status[y11][y2]=0;
  181.    
  182.     y11=y2=x1=x2=0;
  183.  
  184. }
  185. //End Kiem Tra
  186.  
  187. /*                 END                 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement