Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. int main()
  7. {
  8.     srand(time(NULL));
  9.  
  10.     int x = 0, y = 0, temp = 0;
  11.     int end = 0;
  12.     char action = 'l';
  13.  
  14.     int field[4][4];
  15.     for(int i=0;i<16;i++) {
  16.         field[0][i] = i+1;
  17.     }
  18.     field[3][3] = 0;
  19.     for(int i=0;i<8;i++) {
  20.         int x2 = rand() % 4;
  21.         int y2 = rand() % 4;
  22.         temp = field[x2][y2];
  23.         x = rand() % 4;
  24.         y = rand() % 4;
  25.         field[x2][y2] = field[x][y];
  26.         field[x][y] = temp;
  27.     }
  28.  
  29.     for(int i=0;i<16;i++) {
  30.         if(field[0][i] == 0) {
  31.             x = i / 4;
  32.             y = i % 4;
  33.             break;
  34.         }
  35.     }
  36.  
  37.     printf("Commands: u - up, d - down, l - left, r - right\n\n\n");
  38.  
  39.     while(!end) {
  40.         for(int i=0;i<4;i++) {
  41.             for(int j=0;j<4;j++)
  42.                 printf("%2d ", field[i][j]);
  43.             printf("\n");
  44.         }
  45.         printf("action> ");
  46.         if(scanf("%c", &action) != 1) {
  47.             printf("Invalid: not enough or too many commands\n");
  48.             continue;
  49.         }
  50.         fflush(stdin);
  51.         switch(action) {
  52.             case 'u':
  53.                 if(x > 0) {
  54.                     temp = field[x][y];
  55.                     field[x][y] = field[x-1][y];
  56.                     field[x-1][y] = temp;
  57.                     x = x - 1;
  58.                 }
  59.                 break;
  60.             case 'd':
  61.                 if(x < 3) {
  62.                     temp = field[x][y];
  63.                     field[x][y] = field[x+1][y];
  64.                     field[x+1][y] = temp;
  65.                     x = x+1;
  66.                 }
  67.                 break;
  68.             case 'l':
  69.                 if(y > 0) {
  70.                     temp = field[x][y];
  71.                     field[x][y] = field[x][y-1];
  72.                     field[x][y-1] = temp;
  73.                     y = y - 1;
  74.                 }
  75.                 break;
  76.             case 'r':
  77.                 if(y < 3) {
  78.                     temp = field[x][y];
  79.                     field[x][y] = field[x][y+1];
  80.                     field[x][y+1] = temp;
  81.                     y = y + 1;
  82.                 }
  83.                 break;
  84.             default:
  85.                 printf("Invalid command\n");
  86.         }
  87.  
  88.         for(int i=0;i<16;i++) {
  89.             if(i == 15) {
  90.                 end = 1;
  91.                 break;
  92.             }
  93.             if(field[0][i] != i+1)
  94.                 break;
  95.         }
  96.  
  97.     }
  98.  
  99.     printf("You won!\n");
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement