<1>电脑随机出拳,使用rand函数,srand()函数进行随机播种
<2>用户出拳通过键盘输入(0:石头,1:剪刀2:布)
<3>三局两胜,最终判定输赢结果打印格式如下:
电脑赢3盘,用户赢2盘,电脑胜
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int player=0;
int count=0;
int pcwin=0;
int playerwin=0;
srand(time(NULL));
printf("0代表石头 1代表剪刀 2代表布\n");
while(1){
int pc = rand()%3;
printf("请玩家输入:");
scanf("%d",&player);
if(player==0){
if(pc==1){
count++;
playerwin++;
}
else if(pc==2){
count++;
pcwin++;
}
}else if(player==1){
if(pc==0){
count++;
pcwin++;
}
else if(pc==2){
count++;
playerwin++;
}
}else if(player==2){
if(pc==0){
count++;
playerwin++;
}
else if(pc==1){
count++;
pcwin++;
}
}
//printf("count=%d\n",count);
if(count == 5){
break;
}
}
if(pcwin > playerwin){
printf("电脑赢%d盘 玩家赢%d盘\n",pcwin,playerwin);
printf("电脑胜\n");
}else if(pcwin < playerwin){
printf("电脑赢%d盘 玩家赢%d盘\n",pcwin,playerwin);
printf("玩家胜\n");
}else{
printf("平局\n");
}
return 0;
}