Advertisement
Mazamin

Gioco del 15

Dec 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void show_array(int (*)[4]);
  5. void move_tile(int (*)[4], char);
  6. int check_win(int (*)[4]);
  7.  
  8. main(){
  9.     char key;
  10.     int array[4][4]={
  11.         {1,   2,   3,   4},
  12.         {5,   6,   7,  8},
  13.         {9,   10,  11,  12},
  14.         {13,  14,   16,  15}
  15.     };
  16.     do{
  17.         show_array(array);
  18.         key=getchar();
  19.         move_tile(array, key);
  20.         while ((key = getchar()) != '\n' && key != EOF) { } /* clear stdin buffer */
  21.     }while((check_win(array))==0);
  22.     puts("You won");
  23.     return 0;
  24. }
  25.  
  26. void show_array(int (*arrayPtr)[4]){
  27.     int i, j;
  28.     system("clear");
  29.     puts("Use wasd to play");
  30.     for(i=0;i<4;i++){
  31.         for(j=0;j<4;j++)
  32.             if(arrayPtr[i][j]!=16)
  33.                 printf("%d\t", arrayPtr[i][j]);
  34.             else
  35.                 printf("\t");
  36.         puts("");
  37.     }
  38. }
  39.  
  40. void move_tile(int (*arrayPtr)[4], char c){
  41.     void get_current_pos(int (*)[4], int *, int *);
  42.     int i, j;
  43.     get_current_pos(arrayPtr, &i, &j);
  44.     switch(c){
  45.         case 'w':
  46.             if(i<3){
  47.                 arrayPtr[i][j]^=arrayPtr[i+1][j]^=arrayPtr[i][j]^=arrayPtr[i+1][j]; /* fast and hot swap */
  48.             }
  49.             else{
  50.                 puts("Illegal operation: press enter to continue");
  51.                 getchar();
  52.             }
  53.             break;
  54.         case 'd':
  55.             if(j>0){
  56.                 arrayPtr[i][j]^=arrayPtr[i][j-1]^=arrayPtr[i][j]^=arrayPtr[i][j-1]; /* fast and hot swap */
  57.             }
  58.             else{
  59.                 puts("Illegal operation: press enter to continue");
  60.                 getchar();
  61.             }
  62.             break;
  63.         case 's':
  64.             if(i>0){
  65.                 arrayPtr[i][j]^=arrayPtr[i-1][j]^=arrayPtr[i][j]^=arrayPtr[i-1][j]; /* fast and hot swap */
  66.             }
  67.             else{
  68.                 puts("Illegal operation: press enter to continue");
  69.                 getchar();
  70.             }
  71.             break;
  72.         case 'a':
  73.             if(j<3){
  74.                 arrayPtr[i][j]^=arrayPtr[i][j+1]^=arrayPtr[i][j]^=arrayPtr[i][j+1]; /* fast and hot swap */
  75.             }
  76.             else{
  77.                 puts("Illegal operation: press enter to continue");
  78.                 getchar();
  79.             }
  80.             break;
  81.         default:
  82.             puts("Wrong key: press enter to continue");
  83.             getchar();
  84.     }
  85. }
  86.  
  87. int check_win(int (*arrayPtr)[4]){
  88.     int flag=1;
  89.     int i, j;
  90.     for(i=0;i<4;i++)
  91.         for(j=0;j<4;j++)
  92.             (arrayPtr[i][j]==(4*i+j+1))?:(flag=0);
  93.     return flag;
  94. }
  95.  
  96. void get_current_pos(int (*arrayPtr)[4], int * m, int * n){
  97.     int i, j;
  98.     for(i=0;i<4;i++)
  99.         for(j=0;j<4;j++)
  100.             if(arrayPtr[i][j]==16){
  101.                 *m=i;
  102.                 *n=j;
  103.             }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement