关闭

一个简单的剪刀石头布游戏(C++实现)

标签: C++剪刀石头布随机数
2589人阅读 评论(0) 收藏 举报
分类:

首先先看下面这个C++代码,它实现了剪刀石头布的游戏,并且在退出游戏时会打印游戏结果。其设计思路是将rock,paper和scissors分别初始化为0,1,2后(由C++11的强类型枚举enum实现,而且三者出现的先后顺序会影响后面的判断条件),程序则利用rand()srand()随机地做出响应。程序还引入了名为Choicenamespace,这不仅是为了之后使用方便,而且也能预防之后程序扩充时引起名字冲突

#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

// 用户和电脑的选择

Choice player_choice; 
Choice computer_choice; 

// 记录用户和电脑的游戏结果

int player_wins = 0;  
int computer_wins = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); // 设置随机种子
    string input_str;
    int c;

    while (true) 
    {
        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        // 对输入进行校验

        if (input_str.size() < 1) 
        {
            cout << "Invalid input." << endl;
            continue;
        }
        c = input_str[0];
        if (c == 'R' || c == 'r')
            player_choice = Choice::rock;
        else if (c == 'P' || c == 'p')
            player_choice = Choice::paper;
        else if (c == 'S' || c == 's')
            player_choice = Choice::scissors;
        else if (c == 'E' || c == 'e')
            break;
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        // 电脑做出随机选择,并判断游戏结果

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    // 退出游戏后,显示用户和电脑各自的得分

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

// 电脑等概率地做出选择

Choice get_computer_choice()
{
    int n = rand0toN1(3);
    if (n == 0)
        return Choice::rock;
    if (n == 1)
        return Choice::paper;
    return Choice::scissors;
}

// 判断谁是胜利者

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

// 打印相应的提示信息

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

// 随机地返回0~n-1中的任一整数

int rand0toN1(int n)
{
    return rand() % n;
}

在上面的代码中,电脑每次都是等可能的从rock,paper和scissors中做出选择,为了让程序更好玩,下面又对get_computer_choice()函数进行修改,使得程序在每次运行时都会载入一种偏好,而每种偏好出现的概率又不相同。

#include <ctime>
#include <iostream>
#include <string>
#include <stdlib.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

// 用户和电脑的选择

Choice player_choice;
Choice computer_choice;

// 设置电脑的偏好

Choice comp_first_choice = Choice::rock;
Choice comp_second_choice = Choice::scissors;
Choice comp_third_choice = Choice::paper;

// 记录用户和电脑的游戏结果

int player_wins = 0;
int computer_wins = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void assign_computer_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); // 设置随机种子
    assign_computer_prefs(); // 设置电脑的偏好
    string input_str;
    int c;
    while (true) 
    {
        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        // 对输入进行校验

        if (input_str.size() < 1) 
        {
            cout << "Invalid input." << endl;
            continue;
        }
        c = input_str[0];
        if (c == 'R' || c == 'r')
            player_choice = Choice::rock;
        else if (c == 'P' || c == 'p')
            player_choice = Choice::paper;
        else if (c == 'S' || c == 's')
            player_choice = Choice::scissors;
        else if (c == 'E' || c == 'e')
            break;
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        // 电脑做出随机选择,并判断游戏结果

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    // 退出游戏后,显示用户和电脑各自的得分

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

// 设置电脑相应的偏好

void assign_computer_prefs()
{
    int n = rand0toN1(3);
    if (n == 0) 
    {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    } 
    else if (n == 1) 
    {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } 
    else 
    {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    }
}


Choice get_computer_choice()
{
    int n = rand0toN1(10);
    if (n <= 4) // 50%的几率选择comp_first_choice
        return comp_first_choice;
    else if (n <= 7) // 30%的几率选择comp_second_choice
        return comp_second_choice;
    return comp_third_choice; //20%的几率选择comp_third_choice
}

// 判断谁是胜利者

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl
             << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

// 打印相应的提示信息

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

// 随机地返回0~n-1中的任一整数

int rand0toN1(int n)
{
    return rand() % n;
}

再上面的基础上再更进一步,让程序在每次运行时都记录用户的选择,在若干回合后,程序根据统计得到用户的偏好后,重新调整自己的偏好,这样会让玩家觉得更有挑战性。

#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

// 用户和电脑的选择

Choice player_choice;   
Choice computer_choice; 

// 设置电脑的偏好

Choice comp_first_choice = Choice::rock;   
Choice comp_second_choice = Choice::scissors; 
Choice comp_third_choice = Choice::paper;

// 记录用户和电脑的游戏结果

int player_wins = 0;   
int computer_wins = 0; 

//  记录用户输入的选择

int player_n_rock = 0; 
int player_n_scissors = 0;
int player_n_paper = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void assign_computer_prefs();
void reset_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); // 设置随机种子
    assign_computer_prefs(); // 设置电脑的偏好
    string input_str;
    int c;
    while (true) 
    {
        int m = rand0toN1(10); 

        // 10个回合后,电脑重置偏好
        if (m == 0) 
            reset_prefs();

        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        // 对输入进行校验

        if (input_str.size() < 1)
        {
            cout << "Invalid input." << endl;
            continue;
        }

        // 统计用户的选择

        c = input_str[0];
        if (c == 'R' || c == 'r') 
        {
            player_choice = Choice::rock;
            player_n_rock++;
        } 
        else if (c == 'P' || c == 'p') 
        {
            player_choice = Choice::paper;
            player_n_paper++;
        } 
        else if (c == 'S' || c == 's') 
        {
            player_choice = Choice::scissors;
            player_n_scissors++;
        } 
        else if (c == 'E' || c == 'e') 
        {
            break;
        } 
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        // 电脑做出随机选择,并判断游戏结果

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    // 退出游戏后,显示用户和电脑各自的得分

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

// 设置电脑相应的偏好

void assign_computer_prefs()
{
    int n = rand0toN1(3);
    if (n == 0) {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    } else if (n == 1) {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } else {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    }
}

// 重置电脑相应的偏好

void reset_prefs()
{
    if (player_n_rock > player_n_paper && player_n_rock > player_n_scissors) 
    {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    } 
    else if (player_n_paper > player_n_scissors)
     {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } 
    else 
    {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    }
}


Choice get_computer_choice()
{
    int n = rand0toN1(10);
    if (n <= 4) // 50%的几率选择comp_first_choice
        return comp_first_choice;
    else if (n <= 7) // 30%的几率选择comp_second_choice
        return comp_second_choice;
    return comp_third_choice; //20%的几率选择comp_third_choice
}

// 判断谁是胜利者

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl
             << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

// 打印相应的提示信息

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

// 随机地返回0~n-1中的任一整数

int rand0toN1(int n)
{
    return rand() % n;
}

参考资料:《C++ Without Fear 2nd Edtion》,中文译名为《好学的C++》

1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Java基本功练习二(显示日历,石头剪子布游戏,找完全数等)

这是基于Eclipse编程基本功的第二篇,作者是菜鸟,才开始学习不久,希望和大家共同进步!
  • xyb890826
  • xyb890826
  • 2014-12-03 15:02
  • 1590

第5周 经典小程序2题 C语言实现的猜拳游戏(剪子锤子布)

下面的代码会实现一个猜拳游戏,让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。 问题及代码: #include #include #include int main() ...
  • Emperor_21AD_ShiDi
  • Emperor_21AD_ShiDi
  • 2016-04-25 22:07
  • 2846

学习JavaScript(1)-制作一个简单的石头剪刀布游戏

JS 初学小项目
  • sugarf
  • sugarf
  • 2016-09-07 07:12
  • 194

剪刀石头布游戏程序的简单实现

  • 2012-08-05 14:51
  • 6KB
  • 下载

一个很简单的石头剪刀布C语言程序··

  • 2009-11-09 19:21
  • 1KB
  • 下载

一个简单的VB石头剪刀布

  • 2010-08-14 10:43
  • 63KB
  • 下载

一个C语言的剪刀石头布小游戏

/****************************************************** 石头剪刀布的程序 源程序是从开源中国看到的, 地址为http://www.osch...
  • geek_monkey
  • geek_monkey
  • 2015-03-04 09:15
  • 738

一个自己写的剪刀石头布小游戏

  • 2010-03-30 09:43
  • 532KB
  • 下载

用户与计算机的几个简单交互游戏(猜数字、猜单词、剪刀石头布...)

1)计算机产生一1~100的随机数,用户猜测,会有提示是较大还是较小。用户可以多玩几次(需要输入玩的次数),退出时打印他猜测的总次数及猜中一个数字的平均次数。 package userGuessIn...
  • u013805796
  • u013805796
  • 2014-03-21 12:37
  • 586

一个简单的石头剪刀布java源码

  • 2011-12-06 23:29
  • 7KB
  • 下载
    个人资料
    • 访问:71312次
    • 积分:1327
    • 等级:
    • 排名:千里之外
    • 原创:65篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论