溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言實(shí)現(xiàn)2048游戲代碼

發(fā)布時(shí)間:2020-09-27 14:09:16 來(lái)源:腳本之家 閱讀:191 作者:fu_01 欄目:編程語(yǔ)言

本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)2048游戲具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

C語(yǔ)言實(shí)現(xiàn)2048游戲代碼

使用文本界面的屏幕繪圖庫(kù) ncurses.

設(shè)計(jì)思路:

  • 在滿足條件情況下消除方塊
  • 允許在游戲主界面(16 宮格)中任意一格輸出數(shù)據(jù)

實(shí)現(xiàn)代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <curses.h> 
#include <unistd.h> 
#include <signal.h> 
#include <time.h> 
 
void draw(); // 用于繪制游戲界面 
void play(); // 游戲運(yùn)行的邏輯主體 
void init(); // 初始化函數(shù),用于完成一些必要的初始化操作 
void draw_one(int y, int x); // 繪制單個(gè)數(shù)字 
void cnt_value(int *new_y, int *new_x); //統(tǒng)計(jì)(y, x)對(duì)應(yīng)的格子周圍一圈的空格的個(gè)數(shù) 
int game_over(); // 結(jié)束游戲 
int cnt_one(int y, int x); //統(tǒng)計(jì)(y, x)對(duì)應(yīng)的格子周圍一圈的空格的個(gè)數(shù) 
 
// 游戲主界面是一個(gè) 4*4 的 16 宮格,使用二維數(shù)組進(jìn)行表示,用 0 表示空格 
int a[4][4] = { 0 }; 
// 16 宮格中空格的個(gè)數(shù) 
 
int empty; 
int old_y, old_x; 
 
int main() 
{ 
  init(); 
  play(); 
  endwin(); 
 
  return 0; 
} 
 
void init() 
{ 
  int x, y; 
 
  initscr(); //開(kāi)啟curses模式 
  cbreak(); //開(kāi)啟cbreak模式,除 DELETE 或 CTRL 等仍被視為特殊控制字元外一切輸入的字元將立刻被一一讀取 
  noecho(); //echo() and noecho(): 此函式用來(lái)控制從鍵盤輸入字元時(shí)是否將字元顯示在終端機(jī)上 
  curs_set(0); //設(shè)置光標(biāo)模式 
 
  empty = 15; 
  srand(time(0)); 
  x = rand() % 4; 
  y = rand() % 4; 
  a[y][x] = 2; 
  draw(); 
} 
 
void draw() 
{ 
  int n, m, x, y; 
  char c[4] = {'0', '0', '0', '0'}; 
  clear(); //清除終端屏幕 
 
  for(n = 0; n < 9; n += 2) 
  { 
    for(m = 0; m < 21; m++) 
    { 
      move(n, m);//將游標(biāo)移動(dòng)至 x,y 的位置 
      addch('-');//在當(dāng)前位置畫字符'-' 
      refresh();//將做清除螢?zāi)坏墓ぷ?
    } 
  } 
 
  for(m = 0; m < 22; m += 5) 
  { 
    for(n = 1; n < 8; n++) 
    { 
      move(n, m); 
      addch('|'); 
      refresh(); 
    } 
  } 
 
  for(y = 0; y < 4; y++) 
  { 
    for(x = 0; x < 4; x++) 
    { 
      draw_one(y, x); 
    } 
  } 
} 
 
void draw_one(int y, int x) 
{ 
  int i, m, k, j; 
  char c[5] = {0x00}; 
  i = a[y][x]; 
  m = 0; 
 
  while(i > 0) 
  { 
    j = i % 10; 
    c[m++] = j + '0'; 
    i = i / 10; 
  } 
 
  m = 0; 
  k = (x + 1) * 5 - 1; 
 
  while(c[m] != 0x00) 
  { 
    move(2 * y + 1, k); 
    addch(c[m++]); 
    k--; 
  } 
} 
 
void play() 
{ 
  int x, y, i, new_x, new_y, temp; 
  int old_empty, move; 
  char ch; 
 
  while(1) 
  { 
    move = 0; 
    old_empty = empty; 
    ch = getch(); 
 
    switch(ch) 
    { 
      case 97: //左移 a 
      case 104: // h 
      case 68: // 左移方向鍵 
        for(y = 0; y < 4; y++) 
          for(x = 0; x < 4; ) 
          { 
            if(a[y][x] == 0) 
            { 
              x++; 
              continue; 
            } 
            else 
            { 
              for(i = x + 1; i < 4; i++) 
              { 
                if(a[y][i] == 0) 
                { 
                  continue; 
                } 
                else 
                { 
                  if(a[y][x] == a[y][i]) 
                  { 
                    a[y][x] += a[y][i]; 
                    a[y][i] = 0; 
                    empty++; 
                    break; 
                  } 
                  else 
                  { 
                    break; 
                  } 
                } 
              } 
              x = i; 
            } 
          } 
 
        for(y = 0; y < 4; y++) 
          for(x = 0; x < 4; x++) 
          { 
            if(a[y][x] == 0) 
            { 
              continue; 
            } 
            else 
            { 
              for(i = x; (i > 0) && (a[y][i - 1] == 0); i--) 
              { 
                a[y][i - 1] = a[y][i]; 
                a[y][i] = 0; 
                move = 1; 
              } 
            } 
          } 
        break; 
      case 100: //右移 d  
      case 108: // l 
      case 67:  //右移方向鍵 
        for(y = 0; y < 4; y++) 
          for(x = 3; x >= 0; ) 
          { 
            if(a[y][x] == 0) 
            { 
              x--; 
              continue; 
            } 
            else 
            { 
              for(i = x - 1; i >= 0; i--) 
              { 
                if(a[y][i] == 0) 
                { 
                  continue; 
                } 
                else if(a[y][x] == a[y][i]) 
                { 
                  a[y][x] += a[y][i]; 
                  a[y][i] = 0; 
                  empty++; 
                  break; 
                } 
                else 
                { 
                  break; 
                } 
              } 
              x = i; 
            } 
          } 
 
        for(y = 0; y < 4; y++) 
          for(x = 3; x >= 0; x--) 
          { 
            if(a[y][x] == 0) 
            { 
              continue; 
            } 
            else 
            { 
              for(i = x; (i < 3) && (a[y][i + 1] == 0); i++) 
              { 
                a[y][i + 1] = a[y][i]; 
                a[y][i] = 0; 
                move = 1; 
              } 
            } 
          } 
        break; 
      case 119: //上移 w 
      case 107: //k 
      case 65:  //上移方向鍵 
        for(x = 0; x < 4; x++) 
          for(y = 0; y < 4; ) 
          { 
            if(a[y][x] == 0) 
            { 
              y++; 
              continue; 
            } 
            else 
            { 
              for(i = y + 1; i < 4; i++) 
              { 
                if(a[i][x] == 0) 
                { 
                  continue; 
                } 
                else if(a[y][x] == a[i][x]) 
                { 
                  a[y][x] += a[i][x]; 
                  a[i][x] = 0; 
                  empty++; 
                  break; 
                } 
                else 
                { 
                  break; 
                } 
              } 
              y = i; 
            } 
          } 
 
        for(x = 0; x < 4; x++) 
          for(y = 0; y < 4; y++) 
          { 
            if(a[y][x] == 0) 
            { 
              continue; 
            } 
            else 
            { 
              for(i = y; (i > 0) && (a[i - 1][x] == 0); i--) 
              { 
                a[i - 1][x] = a[i][x]; 
                a[i][x] = 0; 
                move = 1; 
              } 
            } 
          } 
        break; 
      case 115: //下移 s  
      case 106: //j 
      case 66:  //下移方向鍵 
        for(x = 0; x < 4; x++) 
          for(y = 3; y >= 0; ) 
          { 
            if(a[y][x] == 0) 
            { 
              y--; 
              continue; 
            } 
            else 
            { 
              for(i = y - 1; i >= 0; i--) 
              { 
                if(a[i][x] == 0) 
                { 
                  continue; 
                } 
                else if(a[y][x] == a[i][x]) 
                { 
                  a[y][x] += a[i][x]; 
                  a[i][x] = 0; 
                  empty++; 
                  break; 
                } 
                else 
                { 
                  break; 
                } 
              } 
              y = i; 
            } 
          } 
 
        for(x = 0; x < 4; x++) 
          for(y = 3; y >= 0; y--) 
          { 
            if(a[y][x] == 0) 
            { 
              continue; 
            } 
            else 
            { 
              for(i = y; (i < 3) && (a[i + 1][x] == 0); i++) 
              { 
                a[i + 1][x] = a[i][x]; 
                a[i][x] = 0; 
                move = 1; 
              } 
            } 
          } 
        break; 
      case 'Q': 
      case 'q': 
        game_over(); 
        break; 
      default: 
        continue; 
        break; 
 
    } 
 
    if(empty <= 0) 
      game_over(); 
 
    if((empty != old_empty) || (move == 1)) 
    { 
      do 
      { 
        new_x = rand() % 4; 
        new_y = rand() % 4; 
      }while(a[new_y][new_x] != 0); 
 
      cnt_value(&new_y, &new_x); 
 
      do 
      { 
        temp = rand() % 4; 
      }while(temp == 0 || temp == 2); 
 
      a[new_y][new_x] = temp + 1; 
      empty--; 
    } 
    draw(); 
  } 
} 
 
int cnt_one(int y, int x) 
{ 
  int value = 1; 
 
  if(y - 1 > 0) 
    a[y - 1][x] ? 0 : value++; 
  if(y + 1 < 4) 
    a[y + 1][x] ? 0 : value++; 
  if(x - 1 >= 0) 
    a[y][x - 1] ? 0 : value++; 
  if(x + 1 < 4) 
    a[y][x + 1] ? 0 : value++; 
  if(y - 1 >= 0 && x - 1 >= 0) 
    a[y - 1][x - 1] ? 0 : value++; 
  if(y - 1 >= 0 && x + 1 < 4) 
    a[y - 1][x + 1] ? 0 : value++; 
  if(y + 1 < 4 && x - 1 >= 0) 
    a[y + 1][x - 1] ? 0 : value++; 
  if(y + 1 < 4 && x + 1 < 4) 
    a[y + 1][x + 1] ? 0 : value++; 
 
  return value; 
} 
 
void cnt_value(int *new_y, int *new_x) 
{ 
  int max_x, max_y, x, y, value; 
  int max = 0; 
 
  max = cnt_one(*new_y, *new_x); 
  for(y = 0; y < 4; y++) 
    for(x = 0; x < 4; x++) 
    { 
      if(!a[y][x]) 
      { 
        value = cnt_one(y, x); 
        if(value > max && old_y != y && old_x != x) 
        { 
          *new_y = y; 
          *new_x = x; 
          old_x = x; 
          old_y = y; 
          break; 
        } 
      } 
    } 
} 
 
int game_over() 
{ 
  sleep(1); //暫停1秒 
  endwin(); //關(guān)閉curses并重置tty(結(jié)束curses編程時(shí),最后調(diào)用的一個(gè)函數(shù)) 
  exit(0); 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI