C语言 石头剪子布
用1、2、3来表示剪刀、石头、布,然后先用scanf函数读取用户的输入,再用rand函数产生一个随机数,最后进行比较就可以了。
#include
#include
#include
void main()
{
int com,user;
char c;
srand((unsigned)time(NULL)); //初始化随机数,如果没有这条语句,每次运行电脑产生的随机数序列都是一样的
printf("进入游戏
");
do{
printf("请出拳(1.剪刀、2.石头、3.布):");
scanf("%d",&user);
com=rand()%3+1; //产生1-3范围内的随机数
printf("电脑出%s
",com==1?"剪刀":com==2?"石头":"布");
switch(com-user) //用电脑产生的数和用户输入的数的差值来判断胜负
{
case 0:
printf("平手
");
break;
case 1:
case -2:
printf("电脑赢了
");
break;
case -1:
case 2:
printf("你赢了
");
}
printf("继续游戏按Y键、其它键退出");
getchar();
c=getchar();
}while(c=='y' || c=='Y');
printf("已经退出游戏");
}
#include
#include
#include
void main()
{
int com,user;
char c;
srand((unsigned)time(NULL)); //初始化随机数,如果没有这条语句,每次运行电脑产生的随机数序列都是一样的
printf("进入游戏
");
do{
printf("请出拳(1.剪刀、2.石头、3.布):");
scanf("%d",&user);
com=rand()%3+1; //产生1-3范围内的随机数
printf("电脑出%s
",com==1?"剪刀":com==2?"石头":"布");
switch(com-user) //用电脑产生的数和用户输入的数的差值来判断胜负
{
case 0:
printf("平手
");
break;
case 1:
case -2:
printf("电脑赢了
");
break;
case -1:
case 2:
printf("你赢了
");
}
printf("继续游戏按Y键、其它键退出");
getchar();
c=getchar();
}while(c=='y' || c=='Y');
printf("已经退出游戏");
}
扩展资料
C++编程: 石头剪子布
#include
#include
using namespace std;
int main()
{
int n;
char a[10],b[10];
cin>>n;
int c[n+3];
for(int i=1;i<=n;i++)
{
cin>>a>>b;
if(a[0]=='R')
{
if(b[0]=='R')
{
c[i]=0;
}
if(b[0]=='S')
{
c[i]=1;
}
if(b[0]=='P')
{
c[i]=2;
}
}
else if(a[0]=='S')
{
if(b[0]=='R')
{
c[i]=2;
}
if(b[0]=='S')
{
c[i]=0;
}
if(b[0]=='P')
{
c[i]=1;
}
}
else if(a[0]=='P')
{
if(b[0]=='R')
{
c[i]=1;
}
if(b[0]=='S')
{
c[i]=2;
}
if(b[0]=='P')
{
c[i]=0;
}
}
}
for(int i=1;i<=n;i++)
switch(c[i])
{
case 0:
{
printf("Tie
");break;
}
case 1:
{
printf("Player1
");break;
}
case 2:
{
printf("Player2
");break;
}
}
return 0;
}
随便写的,可能有bug,自己看看吧。文字使用英语这个太难了,太难了……
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int n, i, ch1, ch2;
int pie, lose, win;
char ch[4][10] = {"", "rock", "paper", "scissors"};
srand((unsigned)time(NULL));
printf("Rock-paper-scissors
");
printf("Please input the times that you want to play:");
scanf("%d", &n);
pie = lose = win = 0;
for (i = 1; i <= n; ++i) {
printf("Round %d
Please input your choice.(1 for rock, 2 for paper, 3 for scissors)
", i);
scanf("%d", &ch1); // people choice
while (ch1 > 3 || ch1 <= 0) {
printf("Illegal input, please input again.
");
scanf("%d", &ch1);
}
ch2 = rand() % 3 + 1; // computer chioce
if (ch1 == 1 && ch2 == 3 || ch1 == 2 && ch2 == 1 || ch1 == 3 && ch2 == 2) {
++win;
printf("Your choice is %s, the computer's choice is %s, you win.
", ch[ch1], ch[ch2]);
} else if (ch2 == 1 && ch1 == 3 || ch2 == 2 && ch1 == 1 || ch2 == 3 && ch1 == 2) {
++lose;
printf("Your choice is %s, the computer's choice is %s, you lose.
", ch[ch1], ch[ch2]);
} else {
++pie;
printf("You and computer's choices are also %s, pie.
", ch[ch1]);
}
}
printf("Game over. you lose %d times, win %d times, pie %d times.
", lose, win, pie);
if (win >= lose) {
printf("congratulations~you win.");
} else {
printf("Sorry, you lose.");
}
return 0;
}
这个要图形界面的话比较麻烦
这个如果用C的话写起来挺麻烦的,无偿不好吧~
咸垄金银: 用1、2、3来表示剪刀、石头、布,然后先用scanf函数读取用户的输入,再用rand函数产生一个随机数,最后进行比较就可以了. #include #include #include void main() { int com,user; char c; srand((unsigned)time(NULL)); //初始化随机数,如...
中山区17717274066: 用C语言做一个简单的石头剪子布游戏 - ?
咸垄金银: #include <stdio.h> #include <stdlib.h> enum games{cloth,hammer,scissors,game,quit}; enum games select(); enum games mach(); int won(enum games player, enum games machine); void result(int win, int lose, int tie);void main() {enum games ...
中山区17717274066: 用c语言编写剪刀石头布程序.求算法!! - ?
咸垄金银: 这个太简单了吧 石头、剪子、布 实际上不就是 a b c嘛 a>b b>c c>a//scanf()接收2个字母 ,放在变量 no1 和 no2里面if(no1=='a'&&no2==b)//第一个是a输出第一个大if(no1=='a'&&no2=='a')//一样大if(no1=='a'&&no2=='c')//第二个大....把条件都列出来 OK
中山区17717274066: C语言编写简单游戏:剪刀、石头、布?
咸垄金银: 呵呵,这是我去年初学C语言时写的,当时水平低,高手就不要指责了~~~ #include<time.h> #include<stdio.h> #include<conio.h> #include <stdlib.h> char x,response; int y,draw,win,loss; void main() { c: draw = 0,win = 0,loss = 0; d: system("cls"); ...
中山区17717274066: c语言初学者,要做一个石头剪刀布的程序,不知道怎么做,求大神看看 - ?
咸垄金银: 更加合理的答案: #include #include #include int main(void){ char gesture[3][10] = {"scissor", "stone", "cloth"}; int man, computer, result, ret; srand(time(NULL)); while(1){ computer = rand() % 3; printf("Input your gesture(0-scissor 1-stone ...
中山区17717274066: 利用c语言编写石头剪子布游戏!!!!!一定要帮帮忙!!! - ?
咸垄金银: 大概思路:你可以做个服务端运行起来, 软后做客户端, 服务端客户端可以用TCP链接. 然后服务端负责记录联入的玩家以向客户端推送消息及游戏结果. 客户端负责玩家信息维护及游戏交互.
中山区17717274066: 用c语言编写一个剪刀石头布的游戏,简单点最好,不要用太复杂的c语言知识,c语言刚学. - ?
咸垄金银: #include<stdio.h> #include<stdlib.h> #include<time.h>/*************\ * 剪刀 石头 布 * * 最简单小游戏 * \*************/int main(void){char gesture[3][10] = {"scissor","stone","cloth"};int man, computer, result, ret;/*随机数初始化函数*/...
中山区17717274066: 用C语言编写一个石头剪子布游戏,具体要求如下,高分急求 - ?
咸垄金银: 刚刚的有点问题,现在改好了: #include<stdio.h> #include<time.h> #include<stdlib.h> #include<string.h> char name[100],way[3][10]={"石头","剪子","布"},mode_name[2][20]={"三局两胜","五局三胜"}; void new_game(int ...
中山区17717274066: 利用c语言编写石头剪子布游戏!!!!!一定要帮帮忙!!!?
咸垄金银: #define ST 'M' #define JZ 'J' #define BU 'P' int arr[3] = { ST, JZ, BU }; int win(int a, int b) { if ( a== ST && b== JZ ) return 1; if ( a== JZ && b== BU ) return 1; if ( a== BU && b== ST ) return 1; return 0; } int main() { int ...
中山区17717274066: C语言编程:剪刀石头布的小游戏 - ?
咸垄金银: 原创: TC2.0以及gcc 编译通过/*=======================================================*Author :wacs5*Date :20100601(YYYYMMDD)*Function :剪刀石头布*===================================================...