21xrx.com
2024-12-22 17:36:45 Sunday
登录
文章检索 我的文章 写文章
2048小游戏C++代码
2023-07-14 14:03:53 深夜i     --     --
2048 小游戏 C++ 代码

2048 是一款非常流行的数字益智游戏。这个游戏最初在网页上出现,后来也有了移动端的应用。如果你喜欢游戏编程,那么也许你可以尝试下面这个基于 C++ 的 2048 小游戏代码。


#include <iostream>

#include <algorithm>

#include <vector>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <ctime>

using namespace std;

int arr[6][6];

int dup[6][6];

void init() //初始化

{

  memset(arr, 0, sizeof(arr)); // 将arr数组全部设为0

  memset(dup, 0, sizeof(dup)); // 将dup数组全部设为0

}

int add_value()

{

  srand((unsigned int)time(NULL));

  int x = rand() % 4 + 1;

  int y = rand() % 4 + 1;

  while (arr[x][y] != 0)

  {

    x = rand() % 4 + 1;

    y = rand() % 4 + 1;

  }

  int num = rand() % 10;

  if (num == 9)

    arr[x][y] = 4;

  else

    arr[x][y] = 2;

  return 0;

}

void copyarr() //复制arr数组到dup数组

{

  memcpy(dup, arr, sizeof(arr));

}

bool check_arr() //判断游戏是否已经结束

{

  for (int i = 1; i <= 4; i++)

  {

    for (int j = 1; j <= 4; j++)

    {

      if (arr[i][j] == 2048)

        return 1;

      if (arr[i][j] == 0)

        return 0;

      if (j != 4 && arr[i][j] == arr[i][j + 1])

        return 0;

      if (i != 4 && arr[i][j] == arr[i + 1][j])

        return 0;

    }

  }

  return 1;

}

bool check_move(int x)

{

  for (int i = 1; i <= 4; i++)

  {

    for (int j = 1; j <= 4; j++)

    {

      if (arr[i][j] == 0)

        continue;

      if (x == 1 && i != 1 && arr[i][j] != 0)

      {

        if (arr[i - 1][j] == 0 || arr[i - 1][j] == arr[i][j])

          return 1;

      }

      if (x == 2 && i != 4 && arr[i][j] != 0)

      {

        if (arr[i + 1][j] == 0 || arr[i + 1][j] == arr[i][j])

          return 1;

      }

      if (x == 3 && j != 1 && arr[i][j] != 0)

      {

        if (arr[i][j - 1] == 0 || arr[i][j - 1] == arr[i][j])

          return 1;

      }

      if (x == 4 && j != 4 && arr[i][j] != 0)

      {

        if (arr[i][j + 1] == 0 || arr[i][j + 1] == arr[i][j])

          return 1;

      }

    }

  }

  return 0;

}

void up_move() // 向上移动

{

  copyarr();

  for (int j = 1; j <= 4; j++)

  {

    vector<int>tmp;

    for (int i = 1; i <= 4; i++)

    {

      if (arr[i][j] != 0)

        tmp.push_back(arr[i][j]);

    }

    if (tmp.size() == 0)

      continue;   

    for (int k = 0; k<tmp.size() - 1; k++)

    {

      if (tmp[k] == tmp[k + 1])

      {

        tmp[k] = tmp[k] * 2;

        tmp.erase(tmp.begin() + k + 1);

      }

    }

    for (int i = 1; i <= 4; i++)

      arr[i][j] = 0;

    for (int i = 0; i<tmp.size(); i++)

      arr[i + 1][j] = tmp[i];

  }

}

void down_move() //向下移动

{

  copyarr();

  for (int j = 1; j <= 4; j++)

  {

    vector<int>tmp;

    for (int i = 4; i >= 1; i--)

    {

      if (arr[i][j] != 0)

        tmp.push_back(arr[i][j]);

    }

    if (tmp.size() == 0)

      continue;

    for (int k = 0; k<tmp.size() - 1; k++)

    {

      if (tmp[k] == tmp[k + 1])

      {

        tmp[k] = tmp[k] * 2;

        tmp.erase(tmp.begin() + k + 1);

      }

    }

    for (int i = 1; i <= 4; i++)

      arr[i][j] = 0;

    for (int i = 0; i<tmp.size(); i++)

      arr[4 - i][j] = tmp[i];

  }

}

void left_move() //向左移动

{

  copyarr();

  for (int i = 1; i <= 4; i++)

  {

    vector<int>tmp;

    for (int j = 1; j <= 4; j++)

    {

      if (arr[i][j] != 0)

        tmp.push_back(arr[i][j]);

    }

    if (tmp.size() == 0)

      continue;

    for (int k = 0; k<tmp.size() - 1; k++)

    {

      if (tmp[k] == tmp[k + 1])

      {

        tmp[k] = tmp[k] * 2;

        tmp.erase(tmp.begin() + k + 1);

      }

    }

    for (int j = 1; j <= 4; j++)

      arr[i][j] = 0;

    for (int j = 0; j<tmp.size(); j++)

      arr[i][j + 1] = tmp[j];

  }

}

void right_move() //向右移动

{

  copyarr();

  for (int i = 1; i <= 4; i++)

  {

    vector<int>tmp;

    for (int j = 4; j >= 1; j--)

    {

      if (arr[i][j] != 0)

        tmp.push_back(arr[i][j]);

    }

    if (tmp.size() == 0)

      continue;

    for (int k = 0; k<tmp.size() - 1; k++)

    {

      if (tmp[k] == tmp[k + 1])

      {

        tmp[k] = tmp[k] * 2;

        tmp.erase(tmp.begin() + k + 1);

      }

    }

    for (int j = 1; j <= 4; j++)

      arr[i][j] = 0;

    for (int j = 0; j<tmp.size(); j++)

      arr[i][4 - j] = tmp[j];

  }

}

void print_arr() //打印数组

{

  for (int i = 1; i <= 4; i++)

  {

    for (int j = 1; j <= 4; j++)

    {

      if (arr[i][j] == 0)

        printf("  .");

      else

        printf("%5d", arr[i][j]);

    }

    printf("\n");

  }

  printf("\n");

}

int main()

{

  init();

  add_value();

  add_value();

  print_arr();

  while (1)

  {

    char s[10];

    scanf("%s", s);

    if (strcmp(s, "up") == 0)

    {

      if (check_move(1) == 1)

      {

        up_move();

        if (memcmp(dup, arr, sizeof(arr)) != 0)

        {

          add_value();

          if (check_arr() == 1)

          {

            printf("you win!\n");

            break;

          }

        }

        else

          continue;

        print_arr();

      }

      else

      {

        printf("can not move!\n");

        continue;

      }

    }

    else if (strcmp(s, "down") == 0)

    {

      if (check_move(2) == 1)

      {

        down_move();

        if (memcmp(dup, arr, sizeof(arr)) != 0)

        {

          add_value();

          if (check_arr() == 1)

          {

            printf("you win!\n");

            break;

          }

        }

        else

          continue;

        print_arr();

      }

      else

      {

        printf("can not move!\n");

        continue;

      }

    }

    else if (strcmp(s, "left") == 0)

    {

      if (check_move(3) == 1)

      {

        left_move();

        if (memcmp(dup, arr, sizeof(arr)) != 0)

        {

          add_value();

          if (check_arr() == 1)

          {

            printf("you win!\n");

            break;

          }

        }

        else

          continue;

        print_arr();

      }

      else

      {

        printf("can not move!\n");

        continue;

      }

    }

    else if (strcmp(s, "right") == 0)

    {

      if (check_move(4) == 1)

      {

        right_move();

        if (memcmp(dup, arr, sizeof(arr)) != 0)

        {

          add_value();

          if (check_arr() == 1)

          {

            printf("you win!\n");

            break;

          }

        }

        else

          continue;

        print_arr();

      }

      else

      {

        printf("can not move!\n");

        continue;

      }

    }

    else if (strcmp(s, "quit") == 0)

    {

      printf("bye bye~\n");

      break;

    }

    else

    {

      printf("wrong input!\n");

      continue;

    }

  }

  return 0;

}

这段代码实现了基础的 2048 游戏逻辑,包括生成随机数、判断游戏是否结束、判断是否可以移动数字等等。如果你对这个游戏感兴趣,也可以在此基础上添加更多的功能,比如计分、排行榜等等,为玩家提供更好的游戏体验。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复