This 2048 program can work,but often there are catons happening(it runs to slow,need to wait for some seconds to get on), and i sincerely hope to receive some advice on how to promote the speed of my program. I am the beginner of C language, and this 2048 game is my first program. I appreciate it if you can give me some help.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
int a[4][4] = { 0 };
int a[4][4] = { 0 };
int* p = &a[0][0];
int gameover = 0;
void input(int a[4][4], int* score);
void init(int a[4][4]);
void game_over(void);
void operate(int a[4][4], int* empty, int* score);
int main(void) {
int empty = 2;
int score = 0;
init(a);
input(a, &score);
while (gameover != 1) {
operate(a, &empty, &score);
input(a, &score);
}
fflush(stdin);
getchar();
return 0;
}
void input(int a[4][4], int* score)
{
int i = 0, j = 0;
for (j = 3; j >= 0; j--) {
printf("-------------------------\n");
printf("|");
fflush(stdout);
fflush(stdin);
for (i = 0; i < 4; i++) {
if (a[j][i] == 0)
printf(" |");
else
printf(" %d |", a[j][i]);
fflush(stdout);
}
putchar('\n');
}
printf("-------------------------\n");
printf("up↑ down↓ left← right→\n");
fflush(stdout);
fflush(stdin);
printf("-----score:%d -----\n", *score);
}
void init(int a[4][4]) {
int x, y;
srand((unsigned)time(NULL));
x = rand() % 4;
y = rand() % 4;
a[y][x] = 2;
while (a[y][x] != 0) {
x = rand() % 4;
y = rand() % 4;
}
a[y][x] = 2;
}
void operate(int a[4][4], int* empty, int* score) {
int in = 0;
int x = 0, y = 0, check = 0;
getch();
in = getch();
fflush(stdin);
switch (in)
{
case 72:
for (x = 0;x < 4; x++)
{
for (y = 3; y >= 0; y--)
{
if (a[y][x] != 0 && y != 3)
{
check = y + 1;
for (; check < 4; check++)
{
if (a[check][x] != 0)
{
if (a[y][x] == a[check][x])
{
a[check][x] = 2 * a[check][x];
a[y][x] = 0;
*empty -= 1;
*score += a[check][x];
break;
}
else
{
if (check - 1 != y)
{
a[check - 1][x] = a[y][x];
a[y][x] = 0;
}
break;
}
}
if (check == 3 && a[check][x] == 0)
{
a[3][x] = a[y][x];
a[y][x] = 0;
break;
}
}
}
}
}
break;
case 80:
for (x = 0;x < 4; x++)
{
for (y = 0; y < 4; y++)
{
if (a[y][x] != 0 && y != 0)
{
check = y - 1;
for (; check >= 0; check--)
{
if (a[check][x] != 0)
{
if (a[y][x] == a[check][x])
{
a[check][x] = 2 * a[check][x];
a[y][x] = 0;
*empty -= 1;
*score += a[check][x];
break;
}
else
{
if (check + 1 != y)
{
a[check + 1][x] = a[y][x];
a[y][x] = 0;
}
break;
}
}
if (check == 0 && a[check][x] == 0)
{
a[0][x] = a[y][x];
a[y][x] = 0;
break;
}
}
}
}
}
break;
case 75:
for (y = 0;y < 4; y++)
{
for (x = 0; x < 4; x++)
{
if (a[y][x] != 0 && x != 0)
{
check = x - 1;
for (; check >= 0; check--)
{
if (a[y][check] != 0)
{
if (a[y][x] == a[y][check])
{
a[y][check] = 2 * a[y][check];
a[y][x] = 0;
*empty -= 1;
*score += a[y][check];
break;
}
else
{
if (check + 1 != x)
{
a[y][check + 1] = a[y][x];
a[y][x] = 0;
}
break;
}
}
if (check == 0 && a[y][check] == 0)
{
a[y][0] = a[y][x];
a[y][x] = 0;
break;
}
}
}
}
}
break;
case 77:
for (y = 0;y < 4; y++)
{
for (x = 3; x >= 0; x--)
{
if (a[y][x] != 0 && x != 3)
{
check = x + 1;
for (; check < 4; check++)
{
if (a[y][check] != 0)
{
if (a[y][x] == a[y][check])
{
a[y][check] = 2 * a[y][check];
a[y][x] = 0;
*empty -= 1;
*score += a[y][check];
break;
}
else
{
if (check - 1 != x)
{
a[y][check - 1] = a[y][x];
a[y][x] = 0;
}
break;
}
}
if (check == 3 && a[y][check] == 0)
{
a[y][3] = a[y][x];
a[y][x] = 0;
break;
}
}
}
}
}
break;
}
*empty += 1;
if (*empty <= 16)
{
do
{
srand((unsigned)time(NULL));
x = rand() % 4;
y = rand() % 4;
} while (a[y][x] != 0);
a[y][x] = 2;
system("cls");
fflush(stdout);
fflush(stdin);
}
else
{
game_over();
}
}
i hope there will be no caton or less. And sorry for so much code. Because so much catons , i added many fflush() hoping that the situation can getting better, but it is totoally invalid.