栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言控制台版2048小游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言控制台版2048小游戏

效果不好,见谅,没事就写了一个!!!


 
#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", "");
}

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];
}

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);
}

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;
  
  CONSOLE_CURSOR_INFO cci = {1, 0};
  
  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;
}

附上另外一个小伙伴的代码



#include 
#include   
#include   
#include   

void start_game(); 
void reset_game(); 


void move_left(); 
void move_right();
void move_up();
void move_down();

void refresh_show();  
void add_rand_num();  
void check_game_over(); 
int get_null_count();  

int board[4][4];   
int score;      
int best;      
int if_need_add_num; 
int if_game_over;  


int main()
{
  start_game();
} 


void start_game()
{
  reset_game();
  char cmd;
  while (1)
  {
    cmd = getch(); 
    
    if (if_game_over) 
    {
      if (cmd == 'y' || cmd == 'Y') 
      {
 reset_game();
 continue;
      }
      else if (cmd == 'n' || cmd == 'N') 
      {
 return;
      }
      else
      {
 continue;
      }
    }
    
    if_need_add_num = 0; 
    
    switch (cmd) 
    {
    case 'a':
    case 'A':
    case 75 :
      move_left();
      break;
    case 's':
    case 'S':
    case 80 :
      move_down();
      break;
    case 'w':
    case 'W':
    case 72 :
      move_up();
      break;
    case 'd':
    case 'D':
    case 77 :
      move_right();
      break;
    }
    
    score > best ? best = score : 1; 
    
    if (if_need_add_num) 
    {
      add_rand_num();
      refresh_show();
    }
  }
}


void reset_game()
{
  score = 0;
  if_need_add_num = 1;
  if_game_over = 0;
  
   
  int n = rand() % 16;
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      board[i][j] = (n-- == 0 ? 2 : 0);
    }
  }
  
  
  add_rand_num();
  
  
  system("cls");
  refresh_show();
}


void add_rand_num()
{
  srand(time(0));
  int n = rand() % get_null_count();
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      if (board[i][j] == 0 && n-- == 0) 
      {
 board[i][j] = (rand() % 3 ? 2 : 4);
 return;
      }
    }
  }
}


int get_null_count()
{
  int n = 0;
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      board[i][j] == 0 ? n++ : 1;
    }
  }
  return n;
}


void check_game_over()
{
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      
      if (board[i][j] == board[i][j+1] || board[j][i] == board[j+1][i])
      {
 if_game_over = 0;
 return;
      }
    }
  }
  if_game_over = 1;
}

 


void move_left()
{
   
  for (int i = 0; i < 4; i++)
  {
    
    for (int j = 1, k = 0; j < 4; j++)
    {
      if (board[i][j] > 0) 
      {
 if (board[i][k] == board[i][j]) 
 {
   score += board[i][k++] <<= 1;
   board[i][j] = 0;
   if_need_add_num = 1;  
 }
 else if (board[i][k] == 0) 
 {
   board[i][k] = board[i][j];
   board[i][j] = 0;
   if_need_add_num = 1;
 }
 else 
 {
   board[i][++k] = board[i][j];
   if (j != k) 
   {
     board[i][j] = 0;
     if_need_add_num = 1;
   }
 }
      }
    }
  }
}


void move_right()
{
  
  for (int i = 0; i < 4; i++)
  {
    for (int j = 2, k = 3; j >= 0; j--)
    {
      if (board[i][j] > 0)
      {
 if (board[i][k] == board[i][j])
 {
   score += board[i][k--] <<= 1;
   board[i][j] = 0;
   if_need_add_num = 1;
 }
 else if (board[i][k] == 0)
 {
   board[i][k] = board[i][j];
   board[i][j] = 0;
   if_need_add_num = 1;
 }
 else
 {
   board[i][--k] = board[i][j];
   if (j != k)
   {
     board[i][j] = 0;
     if_need_add_num = 1;
   }
 }
      }
    }
  }
}


void move_up()
{
  
  for (int i = 0; i < 4; i++)
  {
    for (int j = 1, k = 0; j < 4; j++)
    {
      if (board[j][i] > 0)
      {
 if (board[k][i] == board[j][i])
 {
   score += board[k++][i] <<= 1;
   board[j][i] = 0;
   if_need_add_num = 1;
 }
 else if (board[k][i] == 0)
 {
   board[k][i] = board[j][i];
   board[j][i] = 0;
   if_need_add_num = 1;
 }
 else
 {
   board[++k][i] = board[j][i];
   if (j != k)
   {
     board[j][i] = 0;
     if_need_add_num = 1;
   }
 }
      }
    }
  }
}


void move_down()
{
  
  for (int i = 0; i < 4; i++)
  {
    for (int j = 2, k = 3; j >= 0; j--)
    {
      if (board[j][i] > 0)
      {
 if (board[k][i] == board[j][i])
 {
   score += board[k--][i] <<= 1;
   board[j][i] = 0;
   if_need_add_num = 1;
 }
 else if (board[k][i] == 0)
 {
   board[k][i] = board[j][i];
   board[j][i] = 0;
   if_need_add_num = 1;
 }
 else
 {
   board[--k][i] = board[j][i];
   if (j != k)
   {
     board[j][i] = 0;
     if_need_add_num = 1;
   }
 }
      }
    }
  }
}



void refresh_show()
{
  
  COORD pos = {0, 0};
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  
  printf("nnnn");
  printf(" GAME: 2048   SCORE: %06d  BEST: %06dn", score, best);
  printf("--------------------------------------------------nn");
  
  
  printf("     ┌──┬──┬──┬──┐n");
  for (int i = 0; i < 4; i++)
  {
    printf("     │");
    for (int j = 0; j < 4; j++)
    {
      if (board[i][j] != 0)
      {
 if (board[i][j] < 10)
 {
   printf(" %d │", board[i][j]);   
 }
 else if (board[i][j] < 100)
 {
   printf(" %d │", board[i][j]);
 }
 else if (board[i][j] < 1000)
 {
   printf(" %d│", board[i][j]);
 }
 else if (board[i][j] < 10000)
 {
   printf("%4d│", board[i][j]);
 }
 else
 {
   int n = board[i][j];
   for (int k = 1; k < 20; k++)
   {
     n >>= 1;
     if (n == 1)
     {
printf("2^%02d│", k); 
break;
     }
   }
 }
      }
      else printf("  │");
    }
    
    if (i < 3)
    {
      printf("n     ├──┼──┼──┼──┤n");
    }
    else
    {
      printf("n     └──┴──┴──┴──┘n");
    }
  }
  
  printf("n");
  printf("--------------------------------------------------n");
  printf("W↑ A← →D ↓S");
  
  if (get_null_count() == 0)
  {
    check_game_over();
    if (if_game_over) 
    {
      printf("r   GAME OVER! TRY THE GAME AGAIN? [Y/N]");
    }
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号