21xrx.com
2025-06-08 00:23:01 Sunday
文章检索 我的文章 写文章
"探秘C++:有趣好玩的编程代码"
2023-07-09 10:40:14 深夜i     17     0
C++ 探秘 编程 代码 有趣

C++是一种面向对象的编程语言,它拥有广泛的应用,包括操作系统、游戏、网络、金融等领域。它的代码结构清晰、语法规范、功能强大,然而,C++总是让人们感觉难以入门。其实,在掌握了C++的基础语法后,你可以写出一些有趣好玩的代码。

首先介绍几个简单的代码示例。

1、判断素数

判断一个数n是否为素数,只需从2到n-1判断n是否能被整除即可。

bool isPrime(int n) {
  if (n <= 1) return false;
  for (int i = 2; i < n; ++i) {
    if (n % i == 0) return false;
  }
  return true;
}

2、斐波那契数列

斐波那契数列是指从1开始的数列,它的第一和第二个数都是1,第三个数是前两个数的和,以此类推。数列中的任意两个相邻数字构成的比例,接近于黄金分割常数1.618。

int fib(int n) {
  if (n <= 0) return 0;
  if (n == 1) return 1;
  return fib(n-1) + fib(n-2);
}

现在让我们来看看更有趣的代码。

3、创建一个迷宫

一个迷宫可以通过随机算法生成。在C++中,我们可以使用栈实现一个深度优先搜索算法来生成一个迷宫。我们首先定义一个节点结构体来表示每一个格子。

struct Node {
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
};

然后我们实现一个迷宫类,并在其中定义一个生成迷宫的算法。

class Maze {
private:
  int row, col; // 迷宫的大小
  vector<vector<int>> maze; // 迷宫
public:
  Maze(int _row, int _col) : row(_row), col(_col) {
    maze = vector<vector<int>> (row, vector<int> (col, 0));
  }
  void generate() {
    stack<Node> s;
    s.push(Node(0, 0));
    int dx[] = 1;
    int dy[] = 0;
    while (!s.empty()) {
      Node cur = s.top(); s.pop();
      for (int i = 0; i < 4; ++i) {
        int nx = cur.x + dx[i], ny = cur.y + dy[i];
        if (nx < 0 || nx >= row || ny < 0 || ny >= col || maze[nx][ny]) continue;
        s.push(Node(nx, ny));
        maze[cur.x][cur.y] |= (1 << i);
        maze[nx][ny] |= (1 << (i ^ 1));
      }
    }
  }
};

在主函数中我们可以创建一个迷宫对象,并计算出迷宫中每个格子的墙壁信息。在控制台中输出时,我们可以将“0”表示为墙壁,将“1”表示为路。这样就可以在控制台中打印出一个迷宫了。

int main() {
  Maze maze(10, 10);
  maze.generate();
  for (int i = 0; i < maze.row; ++i) {
    for (int j = 0; j < maze.col; ++j) {
      cout << (maze.maze[i][j] & 1 ? " " : "0");
      cout << (maze.maze[i][j] & 2 ? " " : "0");
    }
    cout << endl;
    for (int j = 0; j < maze.col; ++j) {
      cout << (maze.maze[i][j] & 4 ? "0" : " ");
      cout << (maze.maze[i][j] & 8 ? "0" : " ");
    }
    cout << endl;
  }
  return 0;
}

4、制作一个随机迷宫游戏

我们可以将上面的迷宫生成算法应用到一个游戏中。在游戏中,我们可以定义一个可以在迷宫中移动的角色,并绘制出迷宫和角色的位置。角色的移动速度和迷宫的大小都可以调整,从而制作出不同难度的游戏。我们使用了Windows.h头文件中的光标控制函数(SetConsoleCursorPosition和GetAsyncKeyState),从而实现了一个Windows控制台中的小游戏。

void gotoxy(int x, int y) {
  static HANDLE h = NULL;
  if (!h) h = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD c = y ;
  SetConsoleCursorPosition(h,c);
}
class Character {
private:
  int x, y;
public:
  Character()
    x = y = 0;
  
  void move(int dx, int dy) {
    x += dx;
    y += dy;
  }
  void draw() const {
    gotoxy(x, y);
    cout << "@";
  }
};
class Game {
private:
  Maze maze;
  Character player;
  int speed;
public:
  Game(int row, int col, int _speed) : maze(row, col), speed(_speed) {}
  bool init() {
    maze.generate();
    while (maze.maze[0][0] & 1 || maze.maze[maze.row-1][maze.col-1] & 4) {
      maze.generate();
    }
    player.draw();
    return true;
  }
  void run() {
    while (true) {
      if (GetAsyncKeyState(VK_UP)) {
        if ((maze.maze[player.x-1][player.y] & 4) == 0) {
          player.move(-1, 0);
        } 
      }
      if (GetAsyncKeyState(VK_DOWN)) {
        if ((maze.maze[player.x][player.y] & 4) == 0) {
          player.move(1, 0);
        }
      }
      if (GetAsyncKeyState(VK_LEFT)) {
        if ((maze.maze[player.x][player.y-1] & 2) == 0) {
          player.move(0, -1);
        }
      }
      if (GetAsyncKeyState(VK_RIGHT)) {
        if ((maze.maze[player.x][player.y] & 2) == 0) {
          player.move(0, 1);
        }
      }
      if (player.x == maze.row-1 && player.y == maze.col-1) {
        gotoxy(0, maze.row+1);
        cout << "You Won!" << endl;
        break;
      }
      player.draw();
      Sleep(speed);
    }
  }
};
int main() {
  Game game(20, 30, 100);
  game.init();
  game.run();
  return 0;
}

用以上代码可以制作出一个有趣的小游戏,体验C++的乐趣。

综上所述,C++有着广泛的应用和强大的编程功能。我们可以通过学习C++的基础语法,并结合一些有趣的实例进行实践,从而快速掌握C++的编程技巧。无论是判断素数、创建迷宫,还是制造一个随机迷宫游戏,这些代码示例都可以让你感受到C++的乐趣。

  
  

评论区