/**
* @main.c
*/
#include
#include
#include
#include
// console width
#define CONSOLE_WIDTH 80
#define BOX_WIDTH 10
int BOX[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
// the console output handle
HANDLE c_handle;
void setCursorPosition(short x, short y) {
static COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(c_handle, c);
}
void drawTheGameBox() {
printf("%15s■■■■■■■■■■■■■■■■■■■■■\\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\\n", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■%8s■%8s■%8s■%8s■\\n", "", "", "", "", "");
printf("%15s■■■■■■■■■■■■■■■■■■■■■\\n", "");
}
/**
* get a random position: R
* the x : 0xff & (R >> 4)
* the y : 0x0f & R
*/
int random() {
int i = 0, j = 0, _index = 0, rrr = 0;
int rand_arr[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
srand((unsigned)time(NULL));
// rand()
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) {
rand_arr[_index ++] = (0xff & i << 4) | (0xf & j);
}
}
}
if(_index == 0) {
return -1;
}
return rand_arr[rand() % _index];
}
/** the clean array.**/
int* alogs(int item[]) {
int i = 0, j = 0;
int tep[4] = {0, 0, 0, 0}, tmp[4] = {0, 0, 0, 0};
for(i = 0; i < 4; i ++) {
if(item[i] != 0) {
tep[j ++] = item[i];
}
}
for(i = 0; i < 3; i ++) {
if(tep[0] == 0) break;
if(tep[i] == tep[i + 1]) {
tep[i] *= 2;
tep[i + 1] = 0;
}
}
j = 0;
for(i = 0; i < 4; i ++) {
if(tep[i] != 0) {
tmp[j ++] = tep[i];
}
}
return (int *)(&tmp);
}
/** BOX can move.*/
int validate(int item[]) {
int i = 0;
for(i = 0; i < 3; i ++) {
if(item[i] != 0 && item[i] == item[i + 1]) return 1;
if(item[i] == 0 && item[i + 1] != 0) return 1;
}
return 0;
}
int keydownControlx(int key) {
int i = 0, j = 0;
int *p;
int tp[4] = {0, 0, 0, 0};
switch(key) {
case 72: // up
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[0][i];
tp[1] = BOX[1][i];
tp[2] = BOX[2][i];
tp[3] = BOX[3][i];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[0][i] = p[0];
BOX[1][i] = p[1];
BOX[2][i] = p[2];
BOX[3][i] = p[3];
}
return j != 4;
case 80: // down
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[3][i];
tp[1] = BOX[2][i];
tp[2] = BOX[1][i];
tp[3] = BOX[0][i];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[3][i] = p[0];
BOX[2][i] = p[1];
BOX[1][i] = p[2];
BOX[0][i] = p[3];
}
return j != 4;
case 75: // left
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[i][0];
tp[1] = BOX[i][1];
tp[2] = BOX[i][2];
tp[3] = BOX[i][3];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[i][0] = p[0];
BOX[i][1] = p[1];
BOX[i][2] = p[2];
BOX[i][3] = p[3];
}
return j != 4;
case 77: // right
j = 0;
for(i = 0; i < 4; i ++) {
tp[0] = BOX[i][3];
tp[1] = BOX[i][2];
tp[2] = BOX[i][1];
tp[3] = BOX[i][0];
p = alogs(tp);
if(!validate(tp)) j ++;
BOX[i][3] = p[0];
BOX[i][2] = p[1];
BOX[i][1] = p[2];
BOX[i][0] = p[3];
}
return j != 4;
default: return 0;
}
return 0;
}
int main() {
int i = 0, j = 0, r = 0;
/** set the cursor, visible or invisible.*/
CONSOLE_CURSOR_INFO cci = {1, 0};
/** get the console output handle.*/
c_handle = GetStdHandle(STD_OUTPUT_HANDLE);
// hide the cursor.
SetConsoleCursorInfo(c_handle, &cci);
//
SetConsoleTextAttribute(c_handle, 0x3);
//system("color 30");
drawTheGameBox();
r = random();
if(rand() % 2 == 0) {
BOX[0xff & ( r >> 4)][0xf & r] = 2;
} else {
BOX[0xff & ( r >> 4)][0xf & r] = 4;
}
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) continue;
setCursorPosition(17 + j * 8 + 2 + (j * 2), i * 4 + (i + 2));
//SetConsoleTextAttribute(c_handle, BOX[i][j]);
printf("%d", BOX[i][j]);
}
}
// begin
while(1) {
Sleep(100);
// key down.
while (_kbhit())
{
// the key down fun.
if(!keydownControlx(_getch())) continue;
// clear the console and redraw.
system("cls");
SetConsoleTextAttribute(c_handle, 0x3);
drawTheGameBox();
for(i = 0; i < 4; i ++) {
for(j = 0; j < 4; j ++) {
if(BOX[i][j] == 0) continue;
setCursorPosition(17 + j * 8 + 2 + (j * 2), i * 4 + (i + 2));
//SetConsoleTextAttribute(c_handle, BOX[i][j]);
printf("%d", BOX[i][j]);
}
}
r = random();
if( r == -1 ) { // game over
//SetConsoleTextAttribute(c_handle, 0xff0000);
setCursorPosition(27, 10);
printf("GAME ORVER!!!!!!!");
}
if(rand() % 2 == 0) {
BOX[0xff & ( r >> 4)][0xf & r] = 2;
} else {
BOX[0xff & ( r >> 4)][0xf & r] = 4;
}
Sleep(100);
setCursorPosition(17 + (0xf & r) * 8 + 2 + ((0xf & r) * 2), (0xff & ( r >> 4)) * 4 + ((0xff & ( r >> 4)) + 2));
//SetConsoleTextAttribute(c_handle, BOX[0xff & ( r >> 4)][0xf & r]);
printf("%d", BOX[0xff & ( r >> 4)][0xf & r]);
}
}
return 0;
}