21xrx.com
2024-11-22 09:42:34 Friday
登录
文章检索 我的文章 写文章
C++实现石头剪刀布游戏程序
2023-07-09 03:12:16 深夜i     --     --
C++ 石头剪刀布 游戏程序 实现 编程语言

石头剪刀布这款简单却经典的游戏,可以用来锻炼我们的判断力和心理素质。在这篇文章中,我将介绍如何使用C++实现这个游戏的程序。

首先,我们需要定义三个变量来代表石头、剪刀和布。我们可以使用枚举类型来定义这些变量,如下所示:


enum Choice

  PAPER

;

然后,我们需要让用户输入他们的选择。我们可以使用cin语句来获取用户的输入,如下所示:


cout << "Please choose rock (0), scissors (1), or paper (2): ";

int userChoice;

cin >> userChoice;

接下来,我们需要生成计算机的随机选择。我们可以使用rand函数来生成一个0到2之间的随机数。同时,我们需要使用srand函数来设置随机数生成器的种子,以便它在每次运行时都生成不同的随机数。代码示例如下:


srand(time(0)); // 设置随机数生成器的种子

int computerChoice = rand() % 3; // 生成0到2之间的随机数

现在,我们需要根据用户的选择和计算机的选择来决定谁赢了这局游戏。我们可以使用一个switch语句来实现这个功能,如下所示:


switch (userChoice)

{

  case ROCK:

    switch (computerChoice)

    {

      case ROCK:

        cout << "It's a tie!\n";

        break;

      case SCISSORS:

        cout << "You win!\n";

        break;

      case PAPER:

        cout << "Computer wins!\n";

        break;

    }

    break;

  case SCISSORS:

    switch (computerChoice)

    {

      case ROCK:

        cout << "Computer wins!\n";

        break;

      case SCISSORS:

        cout << "It's a tie!\n";

        break;

      case PAPER:

        cout << "You win!\n";

        break;

    }

    break;

  case PAPER:

    switch (computerChoice)

    {

      case ROCK:

        cout << "You win!\n";

        break;

      case SCISSORS:

        cout << "Computer wins!\n";

        break;

      case PAPER:

        cout << "It's a tie!\n";

        break;

    }

    break;

}

最后,我们需要让用户决定是否继续游戏。我们可以使用一个while循环来重复进行游戏,直到用户选择退出。代码示例如下:


bool playAgain = true;

while (playAgain)

{

  // 游戏代码

  cout << "Do you want to play again? (y/n): ";

  char choice;

  cin >> choice;

  if (choice != 'y')

  

    playAgain = false;

  

}

综合起来,完整的C++程序如下所示:


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

enum Choice

  ROCK;

int main()

{

  srand(time(0));

  bool playAgain = true;

  while (playAgain)

  {

    cout << "Please choose rock (0), scissors (1), or paper (2): ";

    int userChoice;

    cin >> userChoice;

    int computerChoice = rand() % 3;

    switch (userChoice)

    {

      case ROCK:

        switch (computerChoice)

        {

          case ROCK:

            cout << "It's a tie!\n";

            break;

          case SCISSORS:

            cout << "You win!\n";

            break;

          case PAPER:

            cout << "Computer wins!\n";

            break;

        }

        break;

      case SCISSORS:

        switch (computerChoice)

        {

          case ROCK:

            cout << "Computer wins!\n";

            break;

          case SCISSORS:

            cout << "It's a tie!\n";

            break;

          case PAPER:

            cout << "You win!\n";

            break;

        }

        break;

      case PAPER:

        switch (computerChoice)

        {

          case ROCK:

            cout << "You win!\n";

            break;

          case SCISSORS:

            cout << "Computer wins!\n";

            break;

          case PAPER:

            cout << "It's a tie!\n";

            break;

        }

        break;

    }

    cout << "Do you want to play again? (y/n): ";

    char choice;

    cin >> choice;

    if (choice != 'y')

    

      playAgain = false;

    

  }

  return 0;

}

通过学习这个程序,我们可以更好地理解如何使用C++编写简单的游戏程序。同时,我们也可以通过修改代码来增加程序的功能,比如增加记分系统或者实现多人对战等。

  
  

评论区

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