当前位置:主页 > 软件编程 > C代码 >

C语言实现2048游戏

时间:2021-03-17 09:39:14 | 栏目:C代码 | 点击:

本文实例为大家分享了C语言实现2048小游戏的具体代码,供大家参考,具体内容如下

具有以下特点:

1.linux下完成

2.非堵塞键盘读取

3.随机生成2和4

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
 
#define TTY_PATH "/dev/tty" 
#define STTY_ON "stty raw -echo -F" 
#define STTY_OFF "stty -raw echo -F" 
 
int map[4][4]; 
 
typedef struct node 
{ 
 int x; 
 int y; 
 int num; 
}Node; 
Node node; 
 
void init_map()//初始化全部方格 
{ 
 int i, j; 
 for(i=0; i<4; i++) 
  for(j=0; j<4; j++) 
   map[i][j] = 0; 
} 
 
void new_node()//新增方格,避免重复。 
{ 
 int x = 0, y = 0 ,num = 0; 
 do{ 
 int fals = 1; 
 int i,j; 
 for(i=0; i<4; i++) 
  for(j=0; j<4; j++) 
   if(map[i][j] == 0) 
    fals =0; 
 if(fals)//新方格无法被填入,游戏失败 
 { 
  system("clear"); 
  printf("game over!!!!!!!!!!!\n"); 
  exit(0); 
 }  
 x = rand() % 4; 
 y = rand() % 4; 
 num = rand() % 2; 
 
 if(num == 0) 
  num = 3; 
 else if(num == 1) 
  num = 5;  
 }while(map[x][y] > 0); 
  node.x = x; 
  node.y = y; 
  node.num = num; 
  map[x][y] = node.num; 
} 
 
void show()//彩色打印数字 
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 { 
  for(j=0; j<4; j++) 
  { 
   if(map[i][j]%2 == 1) 
   { 
    map[i][j] -= 1; 
    printf("\33[31m%d\33[0m\t",map[i][j]); 
   } 
   else if(map[i][j] == 0) 
   { 
    printf("%d\t",map[i][j]); 
   } 
   else if(map[i][j] == 2) 
   { 
    printf("\33[32m%d\33[0m\t",map[i][j]); 
   } 
   else if(map[i][j] == 4) 
   { 
    printf("\33[33m%d\33[0m\t",map[i][j]); 
   } 
   else if(map[i][j] == 8) 
   { 
    printf("\33[34m%d\33[0m\t",map[i][j]); 
   } 
   else if(map[i][j] == 16) 
   { 
    printf("\33[35m%d\33[0m\t",map[i][j]); 
   } 
   else if(map[i][j] == 32) 
   { 
    printf("\33[36m%d\33[0m\t",map[i][j]); 
   } 
   else 
   { 
    printf("\33[44m%d\33[0m\t",map[i][j]); 
   } 
  } 
  printf("\n"); 
 } 
} 
void left() 
{ 
 int i, j, z, tmp; 
 for(i=0; i<4; i++)//全体方格左移 
  for(j=0; j<4; j++) 
   if(map[i][j] == 0 ) 
    for(z = j + 1; z<4; z++) 
     if(map[i][z] > 0) 
     { 
      tmp = map[i][j]; 
      map[i][j] = map[i][z]; 
      map[i][z] = tmp; 
      break; 
     } 
 for(i=0; i<4; i++)//如果方格数字相同则,相加,通过主函数多次调用,排列好 
  for(j=0; j<4; j++) 
   if(map[i][j] > 0 ) 
    for(z = j + 1; z<4; z++) 
     if(map[i][z] > 0) 
      if(map[i][z] == map[i][j]) 
      { 
       map[i][j] *= 2; 
       map[i][z] = 0; 
      }else 
       break; 
     else 
      break; 
   else 
    break; 
} 
 
void right() 
{ 
 int i, j, z, tmp; 
 for(i=0; i<4; i++) 
  for(j=3; j>=0; j--) 
   if(map[i][j] == 0 ) 
    for(z = j-1; z>=0; z--) 
     if(map[i][z] > 0) 
     { 
      tmp = map[i][j]; 
      map[i][j] = map[i][z]; 
      map[i][z] = tmp; 
      break; 
     } 
 for(i=0; i<4; i++) 
  for(j=3; j>=0; j--) 
   if(map[i][j] > 0 ) 
    for(z = j-1; z>=0; z--) 
     if(map[i][z] > 0) 
      if(map[i][z] == map[i][j]) 
      { 
       map[i][j] *= 2; 
       map[i][z] = 0; 
      }else 
       break; 
     else 
      break; 
   else 
    break; 
} 
void up() 
{ 
 int i, j, z, tmp; 
 for(i=0; i<4; i++) 
  for(j=0; j<4; j++) 
   if(map[j][i] == 0 ) 
    for(z = j+1; z<4; z++) 
     if(map[z][i] > 0) 
     { 
      tmp = map[j][i]; 
      map[j][i] = map[z][i]; 
      map[z][i] = tmp; 
      break; 
     } 
 for(i=0; i<4; i++) 
  for(j=0; j<4; j++) 
   if(map[j][i] > 0 ) 
    for(z = j+1; z<4; z++) 
     if(map[z][i] > 0) 
      if(map[z][i] == map[j][i]) 
      { 
       map[j][i] *= 2; 
       map[z][i] = 0; 
      }else 
       break; 
     else 
      break; 
   else 
    break; 
} 
void down() 
{ 
 int i, j, z, tmp; 
 for(i=0; i<4; i++) 
  for(j=3; j>=0; j--) 
   if(map[j][i] == 0 ) 
    for(z = j-1; z>=0; z--) 
     if(map[z][i] > 0) 
     { 
      tmp = map[j][i]; 
      map[j][i] = map[z][i]; 
      map[z][i] = tmp; 
      break; 
     } 
 for(i=0; i<4; i++) 
  for(j=3; j>=0; j--) 
   if(map[j][i] > 0 ) 
    for(z = j-1; z>=0; z--) 
     if(map[z][i] > 0) 
      if(map[z][i] == map[j][i]) 
      { 
       map[j][i] *= 2; 
       map[z][i] = 0; 
      }else 
       break; 
     else 
      break; 
   else 
    break; 
} 
void move(char ch) 
{ 
 switch(ch) 
 { 
  case 'a': 
   left(); 
   break; 
  case 'd': 
   right(); 
   break; 
  case 'w': 
   up(); 
   break; 
  case 's': 
   down(); 
   break; 
 } 
} 
 
char in_direct()//非堵塞输入 
{ 
 fd_set fd; 
 struct timeval tv; 
 char ch; 
 FD_ZERO(&fd); 
 FD_SET(0, &fd); 
 tv.tv_sec = 0; 
 tv.tv_usec = 10; 
 if(select(1, &fd ,NULL, NULL, &tv) > 0) 
 {  
   ch = getchar(); 
 }  
 return ch; 
} 
int main() 
{ 
 srand(time(NULL)); 
 init_map(); 
 new_node(); 
 show(); 
 char ch; 
 int i=0; 
 while(1) 
 { 
  system(STTY_ON TTY_PATH); 
  ch = in_direct(); 
  system(STTY_OFF TTY_PATH); 
  if(ch=='a'||ch=='d'||ch=='s'||ch=='w') 
  { 
   system("clear"); 
   for(i=0;i<3;i++)//重复多次才能排序好 
    move(ch); 
   new_node(); 
   show(); 
    
  } 
  if(ch=='q')//退出游戏 
  { 
   system("clear"); 
   printf("game over!!!!!!!!\n"); 
   break; 
  } 
  usleep(500000); 
 } 
 return 0; 
} 

您可能感兴趣的文章:

相关文章