21xrx.com
2024-11-22 05:47:29 Friday
登录
文章检索 我的文章 写文章
C++小游戏简单代码
2023-07-08 06:43:39 深夜i     --     --
C++ 小游戏 简单代码 开发 编程

C++是一种流行的编程语言,用于开发各种计算机应用程序,包括小游戏。如果你想尝试创建自己的小游戏,下面是一些简单的C++代码示例,可供参考。

猜数字游戏

这个游戏要求玩家猜一个随机生成的数字。如果猜错了,程序会告诉玩家数字比猜的数大或小。这里是一些基本代码:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

  int num, guess, tries = 0;

  srand(time(0));

  num = rand() % 100 + 1;

  cout << "Guess My Number Game\n\n";

  do

  {

    cout << "Enter a guess between 1 and 100 : ";

    cin >> guess;

    tries++;

    if (guess > num)

      cout << "Too high!\n\n";

    else if (guess < num)

      cout << "Too low!\n\n";

    else

      cout << "\nCorrect! You got it in " << tries << " guesses!\n";

  } while (guess != num);

  return 0;

}

井字棋游戏

这是一个简单的单人井字棋游戏,让玩家(X)对抗计算机(O)。程序会随机选择谁先走。这里是一些基本代码:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

char board[3][3] = {'1','5','7'};

char current_marker;

int current_player;

void draw_board()

{

  cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;

  cout << "-----------\n";

  cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;

  cout << "-----------\n";

  cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;

}

bool is_winner()

{

  // horizontal

  for (int i = 0; i < 3; i++)

    if (board[i][0] == board[i][1] && board[i][1] == board[i][2])

      return true;

  // vertical

  for (int i = 0; i < 3; i++)

    if (board[0][i] == board[1][i] && board[1][i] == board[2][i])

      return true;

  // diagonal

  if (board[0][0] == board[1][1] && board[1][1] == board[2][2])

    return true;

  if (board[0][2] == board[1][1] && board[1][1] == board[2][0])

    return true;

  return false;

}

bool is_board_full()

{

  for (int i = 0; i < 3; i++)

    for (int j = 0; j < 3; j++)

      if (board[i][j] != 'X' && board[i][j] != 'O')

        return false;

  return true;

}

void player_choice()

{

  int choice;

  cout << "Enter a number between 1 and 9: ";

  cin >> choice;

  int row = (choice - 1) / 3;

  int col = (choice - 1) % 3;

  if (board[row][col] == 'X' || board[row][col] == 'O')

  {

    cout << "Invalid move! Try again.\n\n";

    player_choice();

  }

  else

    board[row][col] = current_marker;

}

void swap_player_and_marker()

{

  if (current_marker == 'X')

    current_marker = 'O';

  else

    current_marker = 'X';

  if (current_player == 1)

    current_player = 2;

  else

    current_player = 1;

}

void game()

{

  cout << "Player 1, enter your name: ";

  string player1;

  cin >> player1;

  cout << "Player 2, enter your name: ";

  string player2;

  cin >> player2;

  srand(time(0));

  current_player = rand() % 2 + 1;

  current_marker = 'X';

  do

  {

    draw_board();

    player_choice();

    swap_player_and_marker();

  } while (!is_winner() && !is_board_full());

  draw_board();

  if (is_winner())

  {

    if (current_player == 1)

      cout << player2 << " wins!\n";

    else

      cout << player1 << " wins!\n";

  }

  else

    cout << "Tie game!\n";

}

int main()

{

  game();

  return 0;

}

这些是简单的C++代码示例,可用于构建各种小游戏。如果您想深入了解C++和游戏编程,请参考其他资源。

  
  

评论区

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