程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C說話掌握台版2048小游戲

C說話掌握台版2048小游戲

編輯:關於C++

C說話掌握台版2048小游戲。本站提示廣大學習愛好者:(C說話掌握台版2048小游戲)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話掌握台版2048小游戲正文


後果欠好,見諒,沒事就寫了一個!!!

/**
 * @author Routh
 * @main.c
 * @date 2014, 4, 26
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
// 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", "");
}
/**
 * get a random position: R
 * the x : 0xff & (R >> 4)
 * the y : 0x0f & R
 */
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];
}
/** the clean array.**/
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);
}
/** BOX can move.*/
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;
  /** set the cursor, visible or invisible.*/
  CONSOLE_CURSOR_INFO cci = {1, 0};
  /** get the console output handle.*/
  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;
}

附上別的一個小同伴的代碼

/*
 * Copyright (C) Judge Young
 * E-mail: [email protected]
 * Version: 1.0
 */

#include <stdio.h>
#include <time.h>  /* 包括設定隨機數種子所須要的time()函數 */
#include <conio.h>  /* 包括Windows平台上完成輸出字符不帶回顯和回車確認的getch()函數 */
#include <windows.h> /* 包括Windows平台上完成設定輸入光標地位到達清屏功效的函數 */ 

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();  /* 生成隨機數,本法式中僅生成2或4,幾率之比設為2:1 */
void check_game_over(); /* 檢測能否輸失落游戲,設定游戲停止標記 */
int get_null_count();  /* 獲得游戲面板上空地位數目 */

int board[4][4];   /* 游戲數字面板,籠統為二維數組 */
int score;      /* 游戲的分 */
int best;      /* 游戲最高分 */
int if_need_add_num; /* 能否須要生成隨機數標記,1表現須要,0表現不須要 */
int if_game_over;  /* 能否游戲停止標記,1表現游戲停止,0表現正常 */

/* main函數 函數界說 */
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; /* 先設定不默許須要生成隨機數,須要時再設定為1 */
    
    switch (cmd) /* 敕令解析,w,s,a,d字符代表高低閣下敕令 */
    {
    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;
  
  /* 懂得到游戲初始化時湧現的兩個數必定會有個2,所以先隨機生成一個2,其他均為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);
    }
  }
  
  /* 後面曾經生成了一個2,這裡再生成一個隨機的2或許4,且設定生成2的幾率是4的兩倍 */
  add_rand_num();
  
  /* 在這裡刷新界面並顯示的時刻,界面上曾經默許湧現了兩個數字,其他的都為空(值為0) */
  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);/* 肯定生成何值,設定生成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()
{
  /* 變量i用來遍歷行項的下標,而且在挪動時一切行互相自力,互不影響 */ 
  for (int i = 0; i < 4; i++)
  {
    /* 變量j為列下標,變量k為待比擬(歸並)項的下標,輪回進入時k<j */
    for (int j = 1, k = 0; j < 4; j++)
    {
      if (board[i][j] > 0) /* 找出k前面第一個不為空的項,下標為j,以後分三種情形 */
      {
        if (board[i][k] == board[i][j]) /* 情形1:k項和j項相等,此時歸並方塊並計分 */
        {
          score += board[i][k++] <<= 1;
          board[i][j] = 0;
          if_need_add_num = 1; /* 須要生成隨機數和刷新界面 */ 
        }
        else if (board[i][k] == 0) /* 情形2:k項為空,則把j項賦值給k項,相當於j方塊挪動到k方塊 */
        {
          board[i][k] = board[i][j];
          board[i][j] = 0;
          if_need_add_num = 1;
        }
        else /* 情形3:k項不為空,且和j項不相等,此時把j項賦值給k+1項,相當於挪動到k+1的地位 */
        {
          board[i][++k] = board[i][j];
          if (j != k) /* 斷定j項和k項能否本來就挨在一路,若不是則把j項賦值為空(值為0) */
          {
            board[i][j] = 0;
            if_need_add_num = 1;
          }
        }
      }
    }
  }
}

/* 右移 函數界說 */
void move_right()
{
  /* 模仿左移操作,差別僅僅是j和k都反向遍歷 */
  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()
{
  /* 模仿左移操作,差別僅僅是行列交換後遍歷,且j和k都反向遍歷 */
  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()
{
  /* 重設光標輸入地位方法清屏可以削減閃耀,system("cls")為備用清屏敕令,均為Windows平台相干*/
  COORD pos = {0, 0};
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  
  printf("\n\n\n\n");
  printf("        GAME: 2048   SCORE: %06d  BEST: %06d\n", score, best);
  printf("       --------------------------------------------------\n\n");
  
  /* 繪制表格和數字 */
  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); /* 跨越四位的數字用2的冪情勢表現,如2^13情勢 */
              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]");
    }
  }
}

以上所述就是本文的全體內容了,願望年夜家可以或許愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved