【C++】石头剪刀布的实现

题目:剪刀石头布

知识储备

  • 字符串
  • 头文件#include <cstdlib>
  • switch语句
  • while(true)如何使用
    • true表示为真,因此while(true)是一个无限循环,因为表达式的值一直为真,为了跳出循环,循环内部要用break语句来跳出。用exit也可以跳出,此时表示了函数直接返回。
    • 跳出循环可以使用break语句或者exit
while(true)
{
	...
}  // 不断重复执行大括号内部的语句
#include<iostream> 
#include<string> 
using namespace std; 
main () 
{ 
string str; 
getline(cin,str); 
cout<<str<<endl; 
}

输入:jkljkljkl 
输出:jkljkljkl
输入:jkl jfksldfj jklsjfl 
输出:jkl jfksldfj jklsjfl

和cin.getline()类似,但是cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数

程序一:

#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int main()
{	cout << "****** 欢迎来到石头剪刀布游戏 ****** \n" << endl;
	cout << "游戏规则如下:石头>剪刀>布>石头\n" << endl;
	cout << "其中,R代表石头,S代表剪刀,C代表布。当您输入您选择出的手势,电脑将自动生成一个手势,按照大小规则判断输赢。\n" << endl;
	enum Chioce {rock, shear, cloth};  // 枚举
	enum Result {you, me, tie};
		Chioce yours, mine; // 定义枚举变量
		Result winner;
		string s1;  // 定义字符串变量
		cout << "你出的是:";
		cin >> s1;
		cout << s1 << endl;

		switch (s1[0])
		{
			case 'R': case 'r': yours = rock; break;
			case 'S': case 's': yours = shear; break;
			case 'C': case 'c': yours = cloth; break;
			default: break;
		}
		
		// 出错:产生的随机数并不是真的随机数。每次都是布。原因还不知道为啥
		int n = rand();  // 产生随机数
		cout << n << endl;
		
		switch(n%3)
		{
			case 0:mine = shear; break;
			case 1:mine = rock; break;
			case 2:mine = cloth; break;
			default: break;
		}

		cout << mine <<endl;

		if(yours == mine) winner = tie;
		if(mine == rock && yours == shear || mine == shear && yours == cloth || mine == cloth && yours == rock)
		{
			winner = me;
		}
		if(yours == rock && mine == shear || yours == shear && mine == cloth || yours == cloth && mine == rock)
		{
			winner = you;
		}

		switch(winner)
		{
			case you: cout << "You win!\n"; break;
			case me: cout << "I win!\n"; break;
			case tie: cout << "It's tie!\n"; break;
			default: break;
		}

	return 0;
}

问题:

  1. 电脑产生的随机数并不是真的随机数,而是伪随机数;
  2. 电脑只能运行一次游戏,无法多次运行

程序二

Q:头文件#include <ctime>是什么?
【C++】关于#include<ctime>与srand(time(NULL))

源程序

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

#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;
}
  • 1
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值