Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 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.     printf("__\n|Example command: 1 1 u\n--\n");
  30.  
  31.     while(!end) {
  32.         for(int i=0;i<4;i++) {
  33.             for(int j=0;j<4;j++)
  34.                 printf("%2d ", field[i][j]);
  35.             printf("\n");
  36.         }
  37.         printf("action> ");
  38.         if(scanf("%d %d %c", &x, &y, &action) != 3) {
  39.             printf("Invalid command\n");
  40.             continue;
  41.         }
  42.         if(x > 3 || x < 0 || y > 3 || y < 0) {
  43.             printf("Invalid command\n");
  44.             continue;
  45.         }
  46.         switch(action) {
  47.             case 'u':
  48.                 if(x > 0) {
  49.                     temp = field[x][y];
  50.                     field[x][y] = field[x-1][y];
  51.                     field[x-1][y] = temp;
  52.                 }
  53.                 break;
  54.             case 'd':
  55.                 if(x < 3) {
  56.                     temp = field[x][y];
  57.                     field[x][y] = field[x+1][y];
  58.                     field[x+1][y] = temp;
  59.                 }
  60.                 break;
  61.             case 'l':
  62.                 if(y > 0) {
  63.                     temp = field[x][y];
  64.                     field[x][y] = field[x][y-1];
  65.                     field[x][y-1] = temp;
  66.                 }
  67.                 break;
  68.             case 'r':
  69.                 if(y < 3) {
  70.                     temp = field[x][y];
  71.                     field[x][y] = field[x][y+1];
  72.                     field[x][y+1] = temp;
  73.                 }
  74.                 break;
  75.             default:
  76.                 printf("Invalid command\n");
  77.         }
  78.  
  79.         for(int i=0;i<16;i++) {
  80.             if(i == 15) {
  81.                 end = 1;
  82.                 break;
  83.             }
  84.             if(field[0][i] != i+1)
  85.                 break;
  86.         }
  87.  
  88.     }
  89.  
  90.     printf("You won!\n");
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement