Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- int main()
- {
- srand(time(NULL));
- int x = 0, y = 0, temp = 0;
- int end = 0;
- char action = 'l';
- int field[4][4];
- for(int i=0;i<16;i++) {
- field[0][i] = i+1;
- }
- field[3][3] = 0;
- for(int i=0;i<8;i++) {
- int x2 = rand() % 4;
- int y2 = rand() % 4;
- temp = field[x2][y2];
- x = rand() % 4;
- y = rand() % 4;
- field[x2][y2] = field[x][y];
- field[x][y] = temp;
- }
- printf("__\n|Example command: 1 1 u\n--\n");
- while(!end) {
- for(int i=0;i<4;i++) {
- for(int j=0;j<4;j++)
- printf("%2d ", field[i][j]);
- printf("\n");
- }
- printf("action> ");
- if(scanf("%d %d %c", &x, &y, &action) != 3) {
- printf("Invalid command\n");
- continue;
- }
- if(x > 3 || x < 0 || y > 3 || y < 0) {
- printf("Invalid command\n");
- continue;
- }
- switch(action) {
- case 'u':
- if(x > 0) {
- temp = field[x][y];
- field[x][y] = field[x-1][y];
- field[x-1][y] = temp;
- }
- break;
- case 'd':
- if(x < 3) {
- temp = field[x][y];
- field[x][y] = field[x+1][y];
- field[x+1][y] = temp;
- }
- break;
- case 'l':
- if(y > 0) {
- temp = field[x][y];
- field[x][y] = field[x][y-1];
- field[x][y-1] = temp;
- }
- break;
- case 'r':
- if(y < 3) {
- temp = field[x][y];
- field[x][y] = field[x][y+1];
- field[x][y+1] = temp;
- }
- break;
- default:
- printf("Invalid command\n");
- }
- for(int i=0;i<16;i++) {
- if(i == 15) {
- end = 1;
- break;
- }
- if(field[0][i] != i+1)
- break;
- }
- }
- printf("You won!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement