c语言编程,掷骰子游戏,掷三个骰子,我与电脑轮流坐庄,输了换庄,游戏结束询问是否结束,如果结束?

如果结束要统计我的游戏局数,赢的局数,输的局数,胜率,多谢大家了,希望大家能给我好的答案

掷骰子比大小,用随机数就可以,但是庄家的规则是咋样的没交代清楚(这里假定庄家的优势在于点数一样大时,庄家赢):

#include "stdafx.h"

#include <iostream>

#include <time.h>

using namespace std;


int main()

{

int Me, Computer,n=1,MC=1;

int meWin = 0, nGames = 0;

srand((unsigned int)time(NULL));

while (n)

{

cout << "开始掷骰子(输入1开始,输入0结束):" << endl;

cin >> n;

Me = rand() % 6 + 1;

Computer= rand() % 6 + 1;

nGames++;

if (MC)

{

cout << "你做庄:" << endl;

cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;

if (Me >= Computer)

{

meWin++;

cout << "恭喜你赢了!" << endl;

}

else

{

MC = 0;

cout << "哦,你输了!" << endl;

}

}

else

{

cout << "电脑做庄:" << endl;

cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;

if (Computer >= Me)

cout << "哦,你输了!" << endl;

else

{

meWin++;

MC = 1;

cout << "恭喜你赢了!" << endl;

}

}

}


cout << "你赢的次数:" << meWin << " 输的次数:" << nGames - meWin << " 胜率:";

float p = (float)meWin / nGames;

int q = p * 100;

printf("%2d%% \n", q);

system("pause");

    return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-28
掷骰子就是随机数1~6,每个骰子都独立随机数。用srand函数设置随机种子,用rand()%6+1;来获得一个1~6的随机数。
至于统计局数,输赢数,单纯变量累计。
而游戏规则需要你说明 ,掷骰子的规则不止一种,更何况有不玩骰子的人。需求不明确,代码写不了。
相似回答