Fork Copy #include "stdio.h" #include "string.h" #include "stdlib.h" #include "time.h" #include #include "windows.h" //#include #include "conio.h" void gotoxy(int x,int y) { HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ; COORD position = {x,y} ; SetConsoleCursorPosition(hStdout,position ) ; } void swap(int &a,int &b) { int tg=a; a=b; b=tg; } void xuat(int a[4][4]) { gotoxy(0,14); for (int i=0;i<4;i++) { printf(" %c ",186); for (int j=0;j<4;j++) if (a[i][j]) printf("%4d %c ",a[i][j],186); else printf("%4s %c "," ",186); printf("\n\n\n\n\n"); } } void themso(int a[4][4]) { int d=0,rd; for (int i=0;i<4;i++) for (int j=0;j<4;j++) if (a[i][j]==0) d++; //tim so o trong' srand(time(NULL)); rd=1+rand()%d; //tao ngau nhieu 1 so tu 1-> so o trong int k=0; for (int i=0;i<4;i++) for (int j=0;j<4;j++) if (a[i][j]==0) { k++; if (k==rd) { a[i][j]=4; //den o trong thu' rd thi tao so 4 return; } } } int kiemtra(int a[4][4]) //kiem tra xem con o trong khong { int d=0; for (int i=0;i<4;i++) for (int j=0;j<4;j++) if (a[i][j]==0) d++; return d; } void reset(int a[4][4],int &move,long &diem,int &check) //tao lai ma tran 4x4 va bien move, diem { move=0; diem=0; for (int i=0;i<4;i++) for (int j=0;j<4;j++) a[i][j]=0; a[0][0]=8; check=1; } void kekhung() { printf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",201,205,205,205,205,205,205,203,205,205,205,205,205,205,203,205,205,205,205,205,205,203,205,205,205,205,205,205,187); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",204,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,185); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",204,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,185); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",204,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,206,205,205,205,205,205,205,185); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c %c %c %c %c\n",186,186,186,186,186); printf(" %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",200,205,205,205,205,205,205,202,205,205,205,205,205,205,202,205,205,205,205,205,205,202,205,205,205,205,205,205,188); } void huongdan() { printf(" *********************************\n *Bam cac phim mui ten de choi *\n *Bam TAB de luu va thoat game *\n *Bam ESC de thoat game *\n *********************************\n\n"); gotoxy(0,11); kekhung(); } void xuatdiem(long diem,int move,long best=0) { gotoxy(0,6); printf(" 2048 Best: %6ld\n\n Moved:%4d Score:%6ld\n\n\n",best,move,diem); //kekhung(); } void inlogo() //in so 2048 { system("cls"); printf("\n *** *** * *** \n * * * * ** * *\n * * * * ** * *\n * * * * * *** \n * * * * * * *\n * * * * * * *\n * * * ***** * *\n * * * * * *\n ***** *** * ***"); } int main() { SetConsoleTitle(TEXT("2048 v2.1 - Created by Nguyen Dang Dung")); int move=0,check=1,win=1,dc; long best=0,diem=0; int a[4][4]={0},i,j; a[0][0]=8; char x[50]; FILE *f; goto START; SAVE: f=fopen("C:\\windows\\save_2048.txt","w"); fprintf(f,"%d %ld\n",move,diem); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) fprintf(f,"%4d ",a[i][j]); fprintf(f,"\n"); } fclose(f); inlogo(); printf("\n\n\n\n Saved!"); Sleep(1000); goto START; LOAD: f=fopen("C:\\windows\\save_2048.txt","r"); if (!f) { fclose(f); inlogo(); printf("\n\n\n\n Khong co game da luu"); Sleep(1000); goto START; } else { fscanf(f,"%d%ld",&move,&diem); for (int i=0;i<4;i++) for (int j=0;j<4;j++) fscanf(f,"%d ",&a[i][j]); fclose(f); check=2; //check=2 de BEGIN khong them so va khong giam luot move goto BEGIN; } END: inlogo(); printf("\n\n\n\n Bye Bye!\n\n Created by Nguyen Dang Dung"); Sleep(1000); return 0; START: f=fopen("C:\\windows\\best.txt","r"); if (f) { fscanf(f,"%ld",&best); } fclose(f); inlogo(); printf("\n\n\t 1. New game\n\n\t 2. Load game\n\n\t 3. Quit game\n\n\t"); dc=getch(); if(dc==49) goto RS; if (dc==50) goto LOAD; if (dc==51) goto END; else goto START; RS: system("cls"); //Vao game moi thi phai in huong dan (vi trong khi choi game khong xoa man hinh, chi in de len) huongdan(); reset(a,move,diem,check); //Khoi tao lai gia tri BEGIN: win=kiemtra(a); //kiem tra xem con o trong nao khong if (check==1) //kiem tra xem di chuyen hay cong 2 o chua themso(a); //them ngau nhien 1 so sau khi da duy chuyen hoac cong 2 o else //neu LOAD game thi check=2 va khong them so va giam luot di chuyen if (check==0) move--; //neu khong cong 2 o hoac di chuyen thi luot di chuyen dc giam di else //neu LOAD game thi phai xoa man hinh luc dau (do trong qua trinh choi khong co xoa man hinh ma chi ghi de len thoi) { system("cls"); huongdan(); } check=0; //tao lai bien' de bat dau luot moi NHAP: if (diem>=best) { best=diem; f=fopen("C:\\windows\\best.txt","w"); fprintf(f,"%ld",best); fclose(f); } xuatdiem(diem,move,best); if (win) xuat(a); else { system("cls"); huongdan(); printf("\n\n\tYou lose!"); //khong con o trong thi in ra thua } dc= getch(); //lay gia tri phim if (dc==75) goto A; //75: mui ten trai if (dc==77) goto D; //77: mui ten phai if (dc==72) goto W; //72: mui ten len if (dc==80) goto S; //80: mui ten xuong if (dc==9) goto SAVE; //9: TAB if (dc==27) goto START; //27:ESC else if (win) goto NHAP; else goto START; //khi da in ra thua thi nhap gi cung quay lai tu dau W: move++; // chon 1 trong 4 W, A, S, D deu tang luot di chuyen for (j=0;j<4;j++) for (i=0;i<3;i++) if (a[i][j]==0) for (int u=i+1;u<4;u++) if (a[u][j]) { swap(a[i][j],a[u][j]); //don` het o khac 0 len tren cung check=1; //danh dau la da di chuyen de them so ngau nhien break; } for (j=0;j<4;j++) for (i=0;i<3;i++) { if (a[i][j]==a[i+1][j]&&a[i][j]) //neu 2 o lien tiep bang nhau va khac 0 thi cong vao o tren, o duoi bang 0 { a[i][j]+=a[i+1][j]; diem+=a[i][j]; a[i+1][j]=0; i++; check=1; //danh dau la da cong 2 o de lat them 1 so ngau nhien } } for (j=0;j<4;j++) for (i=0;i<3;i++) if (a[i][j]==0) for (int u=i+1;u<4;u++) if (a[u][j]) { swap(a[i][j],a[u][j]); //don` het o khac 0 len tren cung check=1; //danh dau la da di chuyen de them so ngau nhieu break; } goto BEGIN; S: move++; for (j=0;j<4;j++) for (i=3;i>0;i--) if (a[i][j]==0) for (int u=i-1;u>=0;u--) if (a[u][j]) { swap(a[i][j],a[u][j]); check=1; break; } for (j=0;j<4;j++) for (i=3;i>0;i--) { if (a[i][j]==a[i-1][j]&&a[i][j]) { a[i][j]+=a[i-1][j]; diem+=a[i][j]; a[i-1][j]=0; i--; check=1; } } for (j=0;j<4;j++) for (i=3;i>0;i--) if (a[i][j]==0) for (int u=i-1;u>=0;u--) if (a[u][j]) { swap(a[i][j],a[u][j]); check=1; break; } goto BEGIN; A: move++; for (j=0;j<4;j++) for (i=0;i<3;i++) if (a[j][i]==0) for (int u=i+1;u<4;u++) if (a[j][u]) { swap(a[j][i],a[j][u]); check=1; break; } for (j=0;j<4;j++) for (i=0;i<3;i++) { if (a[j][i]==a[j][i+1]&&a[j][i]) { a[j][i]+=a[j][i+1]; diem+=a[j][i]; a[j][i+1]=0; i++; check=1; } } for (j=0;j<4;j++) for (i=0;i<3;i++) if (a[j][i]==0) for (int u=i+1;u<4;u++) if (a[j][u]) { swap(a[j][i],a[j][u]); check=1; break; } goto BEGIN; D: move++; for (j=0;j<4;j++) for (i=3;i>0;i--) if (a[j][i]==0) for (int u=i-1;u>=0;u--) if (a[j][u]) { swap(a[j][i],a[j][u]); check=1; break; } for (j=0;j<4;j++) for (i=3;i>0;i--) { if (a[j][i]==a[j][i-1]&&a[j][i]) { a[j][i]+=a[j][i-1]; diem+=a[j][i]; a[j][i-1]=0; i--; check=1; } } for (j=0;j<4;j++) for (i=3;i>0;i--) if (a[j][i]==0) for (int u=i-1;u>=0;u--) if (a[j][u]) { swap(a[j][i],a[j][u]); check=1; break; } goto BEGIN; }