linux下实现的2048游戏示例分享
本文导语: my2048.c 代码如下:#include"my_getch.h"#include"math.h"#include"time.h" #define SPACE() printf(" ")#define RED_NUM(n) printf("33[;31m%4d",(n))#define GREEN_NUM(n) printf("33[;32m%4d",(n))#define BLUE_NUM(n) printf("33[;34m%4d",(n))#define YELLOW_NUM(n) printf("33[;33m%4d",(n))#define PUR...
my2048.c
#include"my_getch.h"
#include"math.h"
#include"time.h"
#define SPACE() printf(" ")
#define RED_NUM(n) printf("33[;31m%4d",(n))
#define GREEN_NUM(n) printf("33[;32m%4d",(n))
#define BLUE_NUM(n) printf("33[;34m%4d",(n))
#define YELLOW_NUM(n) printf("33[;33m%4d",(n))
#define PURPLE_NUM(n) printf("33[;35m%4d",(n))
#define DEEPGREEN_NUM(n) printf("33[;36m%4d",(n))
/*根据不同的number进行不同的宏替换,输出不同颜色的数字*/
void printNum(const int num)
{
if(num==0)
SPACE();
else if(num==1024 || num==32)
RED_NUM(num);
else if(num==2 || num==64 )
BLUE_NUM(num);
else if(num==4 || num==128)
GREEN_NUM(num);
else if(num==8 || num==256)
YELLOW_NUM(num);
else if(num==16 || num==512)
PURPLE_NUM(num);
else
DEEPGREEN_NUM(num);
}
enum game_stat{PLAYING,FAILED,EXITED,DONE};
enum cmd{UP,DOWN,LEFT,RIGHT,QUIT,INVALID};
enum cmd direction;
short empty[16];
struct Game
{
int box[16];
enum game_stat stat;
int step;
unsigned long int point;
}game;
void init_game()
{
int i;
for(i=0;i