欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年05月05日 09:18:17   作者:fu_01  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)2048游戲代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

效果圖:

使用文本界面的屏幕繪圖庫(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)的格子周?chē)蝗Φ目崭竦膫€(gè)數(shù) 
int game_over(); // 結(jié)束游戲 
int cnt_one(int y, int x); //統(tǒng)計(jì)(y, x)對(duì)應(yīng)的格子周?chē)蝗Φ目崭竦膫€(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)控制從鍵盤(pán)輸入字元時(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)前位置畫(huà)字符'-' 
      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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++調(diào)用Python基礎(chǔ)功能實(shí)例詳解

    C++調(diào)用Python基礎(chǔ)功能實(shí)例詳解

    c++調(diào)用Python首先安裝Python,本文以win7為例,給大家詳細(xì)介紹C++調(diào)用Python基礎(chǔ)功能,需要的朋友參考下吧
    2017-04-04
  • C++代碼實(shí)現(xiàn)逆波蘭式

    C++代碼實(shí)現(xiàn)逆波蘭式

    這篇文章主要為大家詳細(xì)介紹了C++代碼實(shí)現(xiàn)逆波蘭式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例

    C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例

    這篇文章主要介紹了C++中靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 深入探索C++ string的底層實(shí)現(xiàn)

    深入探索C++ string的底層實(shí)現(xiàn)

    C語(yǔ)言中的字符串是以字符數(shù)組的形式存儲(chǔ)的,每個(gè)字符占用一個(gè)字節(jié)的內(nèi)存空間,本文我們將和大家一起深入探討一下string的底層實(shí)現(xiàn),感興趣的小伙伴快來(lái)和小編一起吧
    2023-08-08
  • c++ 面向?qū)ο笤O(shè)計(jì)五大原則

    c++ 面向?qū)ο笤O(shè)計(jì)五大原則

    這篇文章主要介紹了c++ 面向?qū)ο笤O(shè)計(jì)五大原則,幫助大家更好的理解和學(xué)習(xí)c++面向?qū)ο笤O(shè)計(jì),感興趣的朋友可以了解下
    2020-08-08
  • VC++創(chuàng)建msi文件的方法

    VC++創(chuàng)建msi文件的方法

    這篇文章主要介紹了VC++創(chuàng)建msi文件的方法,對(duì)于應(yīng)用程序的開(kāi)發(fā)有一定的借鑒價(jià)值,需要的朋友可以參考下
    2014-07-07
  • C++超集C++/CLI模塊的基本用法

    C++超集C++/CLI模塊的基本用法

    這篇文章介紹了C++超集C++/CLI模塊的基本用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 深入解析C++中的虛函數(shù)與多態(tài)

    深入解析C++中的虛函數(shù)與多態(tài)

    對(duì)C++ 了解的人都應(yīng)該知道虛函數(shù)(Virtual Function)是通過(guò)一張?zhí)摵瘮?shù)表(Virtual Table)和一個(gè)指向虛函數(shù)表的指針(vptr)來(lái)實(shí)現(xiàn)的
    2013-09-09
  • C++基礎(chǔ)入門(mén)教程(八):函數(shù)指針

    C++基礎(chǔ)入門(mén)教程(八):函數(shù)指針

    這篇文章主要介紹了C++基礎(chǔ)入門(mén)教程(八):函數(shù)指針,本文講解了函數(shù)原型和函數(shù)定義、const限定符與指針、函數(shù)的指針參數(shù)、為什么要使用指針參數(shù)等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • C語(yǔ)言實(shí)現(xiàn)萬(wàn)年歷效果

    C語(yǔ)言實(shí)現(xiàn)萬(wàn)年歷效果

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)萬(wàn)年歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評(píng)論