Пользователь
Начинающий
Сообщений: 48
Дата: Понедельник, 02.12.2013, 16:52:17 | Сообщение # 1 |
Offline
|
#include 'stdio.h' #include 'stdlib.h' #include 'time.h' #include 'conio.h' #include'locale.h'
//#define MENTION printf("Enter 'j', 'i', 'l' or 'k' to move the empty cell left, up, right or down correspondingly, or 'q' to exit: \n\n");
int** field; int emptyCell_x, emptyCell_y;
//------------------ void init() { setlocale(0,""); int x,y, i,j; srand(time(NULL)); for(i=1; i<=15;) { x=rand()%4; y=rand()%4; if(field[x][y] == 0) field[x][y] = i++; }
for(i=0; i<4; i++) //to find the empty cell for(j=0; j<4; j++) if(field[j][i] == 0) { emptyCell_x=j; emptyCell_y = i; return; }
}
//--------------------- void printField() { int i,j; for(i=0; i<4; i++) { for(j=0; j<4; j++) if(field[j][i]) printf("%3d", field[j][i]); else printf(" _"); printf("\n\n"); } printf("\n\n"); }
//----------------------- int notEndYet() { int i,j; for(i=0; i<3; i++) for(j=0; j<4; j++) if(field[j][i] != 4*i+j+1) return 0; //go on play if(field[0][3] != 13) return 0;
return 1; //victory! }
//--------------- allows move the emply cell int move() { char c;
while(1) { c = getch(); switch© { case 'a': if(emptyCell_x==0) { printf("Ход не доступен.\n\n"); break; } else { field[emptyCell_x][emptyCell_y] = field[emptyCell_x-1][emptyCell_y]; field[emptyCell_x-1][emptyCell_y] = 0; emptyCell_x--; return 1; } case 'w': if(emptyCell_y==0) { printf("Ход не доступен.\n\n"); break; } else { field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y-1]; field[emptyCell_x][emptyCell_y-1] = 0; emptyCell_y--; return 2; } case 'd': if(emptyCell_x==3) { printf("Ход не доступен.\n\n"); break; } else { field[emptyCell_x][emptyCell_y] = field[emptyCell_x+1][emptyCell_y]; field[emptyCell_x+1][emptyCell_y] = 0; emptyCell_x++; return 3; } case 's': if(emptyCell_y==3) { printf("Ход не доступен.\n\n"); break; } else { field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y+1]; field[emptyCell_x][emptyCell_y+1] = 0; emptyCell_y++; return 4; } case 'q': exit(1); //default: printf("Вас приветствует ига Пятнашки\n"); printf(" Для передвижения пустой клетки в верх нажмите W\n"); printf(" В низ S\n"); printf ("В лево A\n"); printf(" В право D\n "); } } }
//----------------- main function void main() { field = (int**)calloc(4, sizeof(int*)); for(int i=0; i<4; i++) field[i] = (int*)calloc(4, sizeof(int));
do init(); while(notEndYet()); printf("\n\tВас приветствует ига Пятнашки\n"); printf("\tДля передвижения пустой клетки в верх нажмите W\n"); printf("\tВ низ S\n"); printf ("\tВ лево A\n"); printf("\tВ право D\n\n"); printField();
while(!notEndYet()) { move(); printField(); }
printf("\n Вы выиграли!\n"); getch(); }
Сообщение отредактировал Sashok - Понедельник, 02.12.2013, 16:53:31
Подпись пользователя
|
![Ранг 5](/.s/rnk/3/rank5.gif) |
| |