#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ESC 27
int num[4][4] = { 0, };
int is_empty[4][4] = { 0, };
int pre[4][4] = { 0, };
int is_pre, score, pre_score;
void printd(int num) {
if (num == 0)
printf("%4c", num);
else {
if (num == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
else if (num == 4)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
else if (num == 8)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
else if (num == 16)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
else if (num == 32)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
else if (num == 64)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
else if (num == 128)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
else if (num == 256)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
else if (num == 512)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
else if (num == 1024)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
else if (num == 2048)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
else if (num == 4096)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
printf("%4d", num);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
}
void print() {
printf("\n\n\n\n\n");
printf("\t\t+---------+---------+---------+---------+\n");
printf("\t\t| | | | |\n");
printf("\t\t| ");
printd(num[0][0]);
printf(" | ");
printd(num[0][1]);
printf(" | ");
printd(num[0][2]);
printf(" | ");
printd(num[0][3]);
printf(" |\n");
printf("\t\t| | | | |\n");
printf("\t\t+---------+---------+---------+---------+\n");
printf("\t\t| | | | |\n");
printf("\t\t| ");
printd(num[1][0]);
printf(" | ");
printd(num[1][1]);
printf(" | ");
printd(num[1][2]);
printf(" | ");
printd(num[1][3]);
printf(" |\n");
printf("\t\t| | | | |\n");
printf("\t\t+---------+---------+---------+---------+\n");
printf("\t\t| | | | |\n");
printf("\t\t| ");
printd(num[2][0]);
printf(" | ");
printd(num[2][1]);
printf(" | ");
printd(num[2][2]);
printf(" | ");
printd(num[2][3]);
printf(" |\n");
printf("\t\t| | | | |\n");
printf("\t\t+---------+---------+---------+---------+\n");
printf("\t\t| | | | |\n");
printf("\t\t| ");
printd(num[3][0]);
printf(" | ");
printd(num[3][1]);
printf(" | ");
printd(num[3][2]);
printf(" | ");
printd(num[3][3]);
printf(" |\n");
printf("\t\t| | | | |\n");
printf("\t\t+---------+---------+---------+---------+\n");
printf("\n\t\tscore : %d", score);
printf("\n\t\t esc : stop\t\tb : before\n");
}
int rand_init() {
int a = rand() % 10;
if (a == 0 || a == 7 || a == 9)return 4;
else return 2;
}
void game_start() {
int i, j, a, b, c, d;
score = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
num[i][j] = 0;
is_empty[i][j] = 0;
}
}
a = rand() % 4;
b = rand() % 4;
num[a][b] = rand_init();
while (1) {
c = rand() % 4;
d = rand() % 4;
if (!(a == c&&b == d)) {
if (num[a][b] == 4) {
num[c][d] = 2;
break;
}
num[c][d] = rand_init();
break;
}
}
is_pre = 1;
}
void search_empty() {
int i, j,a, b;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (num[i][j] == 0)
is_empty[i][j] = 1;
else
is_empty[i][j] = 0;
}
}
while (1) {
a = rand() % 4;
b = rand() % 4;
if (is_empty[a][b] == 1) {
num[a][b] = rand_init();
return;
}
}
}
void move(int z) {
int copy[4][4];
int i, j,k;
if (is_pre == 0)is_pre = 1;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
copy[i][j] = num[i][j];
pre[i][j] = num[i][j];
}
}
pre_score = score;
for (k = 0; k < 4; k++) {
int arra[4] = { 0, };
int arrb[4] = { 0, };
int arrc[4] = { 0, };
for (i = 0; i < 4; i++) {
if (z == 1)arra[i] = num[k][i];
else if (z == 2)arra[i] = num[k][3 - i];
else if (z == 3)arra[i] = num[i][k];
else if (z == 4)arra[i] = num[3 - i][k];
}
for (i = 0, j = 0; i < 4; i++) {
if (arra[i] != 0) {
arrb[j] = arra[i];
j++;
}
}
for (i = 0; i < 3; i++) {
if (arrb[i] == arrb[i + 1]) {
arrb[i] = arrb[i] * 2;
score += arrb[i];
arrb[i + 1] = 0;
}
}
for (i = 0, j = 0; i < 4; i++) {
if (arrb[i] != 0) {
arrc[j] = arrb[i];
j++;
}
}
for (i = 0; i < 4; i++) {
if (z == 1)num[k][i] = arrc[i];
else if (z == 2)num[k][3 - i] = arrc[i];
else if (z == 3)num[i][k] = arrc[i];
else if (z == 4)num[3 - i][k] = arrc[i];
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (copy[i][j] != num[i][j]) {
search_empty();
return;
}
}
}
}
int esc() {
char input;
system("cls");
printf("\n\n\n\n\n\n\n\n\n\t\t\tAre you sure you want to quit?\n\n");
printf("\t\t\t1. yes(y) 2. no(n)\n");
input = getch();
if (input == 'y' || input == 'Y') return 1;
else return 0;
}
void previous() {
int i, j;
if (is_pre) {
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
num[i][j] = pre[i][j];
score = pre_score;
is_pre = 0;
}
}
void game() {
while (1) {
char input;
system("cls");
print();
input = getch();
switch (input) {
case LEFT:
move(1);
break;
case RIGHT:
move(2);
break;
case UP:
move(3);
break;
case DOWN:
move(4);
break;
case 'b':
previous();
break;
case ESC:
if (esc())
return;
}
}
}
int main(int argc, char *argv[]) {
srand((unsigned)time(NULL));
game_start();
game();
return 0;
}