当前位置:首页 » 编程语言 » c语言猜拳游戏怎么设计
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言猜拳游戏怎么设计

发布时间: 2022-07-02 18:53:16

㈠ 猜拳游戏c语言编程

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define ORIGINGOLD 100 typedef struct { char name[100]; unsigned long score; } User; void Menu() { system("cls"); printf("========================made by 小恩 =====================\n"); printf("1:start game\n"); printf("2:show order\n"); printf("3:quit game\n"); printf("==========================================================\n"); } int choise() { int ch; printf("Input your choice(1~3): "); while(ch=getchar()) { if('\n'==ch) continue; while('\n'!=getchar()); if(ch>'3'||ch<'1') { printf("Error input,Try again(1~3): "); continue; } else break; } return ch; } int agree(const char *p) { char yes; printf("%s",p); while(scanf("%c",&yes),yes!='Y'&& yes!='N'&& yes!='y'&& yes!='n') { while('\n'!=getchar()); printf("Error input! Try again(Y/N): "); } while('\n'!=getchar()); if(yes=='Y'|| yes=='y') return 1; else return 0; } User getUser(const unsigned long score) { User gamer; int ch,i=0; memset(&gamer,0,sizeof(User)); printf("Please input the User's name:"); while(!strcmp(gamer.name,"")) { while((i<99) && ('\n'!=(ch=getchar()))) gamer.name[i++]=(char)ch; gamer.name[i]='\0'; } gamer.score=score; return gamer; } int getInt(int minR,int maxR) { int showhand; while(!scanf_s("%d",&showhand) || showhand<minR|| showhand>maxR) { while('\n'!=getchar()); printf("Invide input!Try again:(%d~%d) ",minR,maxR); } while('\n'!=getchar()); return showhand; } void save(const unsigned long score) { FILE *fp; int num=0; User gamer={"",0L},temp={"",0L},gm; if(fopen_s(&fp,"F:\\fist.gm","r+")) { if(fopen_s(&fp,"F:\\fist.gm","w")) { printf("Error,Can't open file F:\\fist.gm for save!\n"); exit(1);

希望采纳

㈡ 用C语言编写一个有趣的猜拳游戏

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

#define maxn 10001

char p1[]="rock";
char p2[]="scissors";
char p3[]="cloth";
char *h[4];

void init(void)
{
h[1]=p1;
h[2]=p2;
h[3]=p3;
}

void begin(void)
{
char your[maxn];
long com;
char p[maxn];
srand(time(NULL));
for(;;)
{
printf("input your order ?( 1:rock 2:scissors 3:cloth )\n");

scanf("%s",your);
com=strlen(your);
if( your[0]>'3' || your[0]<'1' || com!=1)
{
printf("wrong order! please choose in(1~3);\n");
getchar();
continue;
}
printf("your order is %s!\n",h[your[0]-'0']);

getchar();
getchar();
com=rand()%3+1;
printf("The computer's order is %s!\n",h[com]);

getchar();
switch(com-your[0]+'0')
{
case 0:printf("draw game!\n");break;
case 1:
case -2:
printf("You wins!\n");break;
case -1:
case 2:
printf("The computer wins!\n");break;
}
com=1;
for(;com==1;)
{
printf("continue?(Y/N)\n");
scanf("%s",p);
com=0;
switch(p[0])
{

case 'Y':break;
case 'N':
printf("press any key to continue.\n");getchar();getchar();return ;
default:
printf("wrong order!\n");com=1;break;
}
}
}
}

main(void)
{
init();
begin();
return 0;
}

㈢ 用C语言编写三局两胜的猜拳游戏,怎么编写

随机种子产生pc的随机出拳
srand(time(0));

int pc = rand()%3; //0, 1, 2 石头剪子布
屏幕输入自己的结果。
scanf(" %d", &var);
一个负责比较的代码块,很简单的逻辑处理。
一个最多执行三次的循环。
两个负责记录胜负次数的变量,针对单一角色,两胜或者两负,都会结束游戏。

㈣ 用1,2,3分别代表石头剪刀布,输入甲乙的猜 拳选择输出甲乙猜拳结果。(分支结构)。C语言程序设计

void func(int a,int b)
{
int num=0;
if((a==1)&&(b==1))
printf("打平\n");
else if((a==1)&&(b==2))
printf("甲获胜\n");
else if((a==1)&&(b==3))
printf("乙获胜\n");
else if((a==2)&&(b==1))
printf("乙获胜\n");
else if((a==2)&&(b==2))
printf("打平\n");
else if((a==2)&&(b==3))
printf("甲获胜\n");
else if((a==3)&&(b==1))
printf("甲获胜\n");
else if((a==3)&&(b==2))
printf("乙获胜\n");
else if((a==3)&&(b==3))
printf("打平\n");
}

㈤ 猜拳游戏c代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 电脑出拳
int result; // 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while (1){
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;

default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}

srand((unsigned)time(NULL)); // 随机数种子
computer=rand()%3; // 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石头\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石头\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你赢了!");
else if (result==5||result==9||result==10) printf("电脑赢了!");
else printf("平手");
system("pause>nul&&cls"); // 暂停并清屏
}
return 0;
}

㈥ 用c语言怎么写猜拳游戏啊!我刚刚学一个月

#include<cstdio>
#include<cstdlib>
#include<ctime>
intmain()
{
inta=0,b=0,i,c,h,wr;
printf("飞行棋,先输入格子数,之后每次输入1-3的数(上限100) ");
printf("输入格子数:");
scanf("%d",&h);
a=a+h;b=b+h;
while(1)
{
printf("1=石头2=剪刀3=布 ");
printf("****** ******** ****** ");
printf("输入1~3 ");
printf("---------------------------------- ");
scanf("%d",&i);
srand(time(0));
c=rand()%3;
if(i==1&&c==2||i==2&&c==3||i==3&&c==1)
{a=a-1;
printf("你赢了,前进一步 你还剩%d电脑剩%d ",a,b);
printf("*--------------------------------* ");}
elseif(c==1&&i==2||c==2&&i==3||c==3&&i==1)
{b=b-1;
printf("你输了,电脑前进 你还剩%d电脑剩%d ",a,b);
printf("/--------------------------------/ ");}
else{printf("平局 你还剩%d电脑剩%d ",a,b);
printf("?--------------------------------? ");}
if(a==0||b==0)break;}

if(a==0)printf("youwin ");
elseprintf("youlose ");

system("pause");
}

㈦ 怎样用C语言编写一个猜拳游戏

*帮助做程序主体(开始游戏部分)*/
#include
<iostream.h>
#include
<stdlib.h>
void
main()
{
int
computer,
user,
money,
bet;
money=100;
for(
;
money==0;
)
{
cout
<<
"your
money:
"
<<
money
<<
endl;
cout
<<
"bet:
";
cin
>>
bet;
money=money-bet
if(bet>money)
break;
cout
<<
"set:
";
cin
>>
user;
computer=round(sin(rand())*2+1);
switch(user,computer)
{
.../*这地方你自己填一填*/
}
}
cout
<<
"game
over";
}

㈧ c语言问题 猜拳游戏

整体还是很好的。

问题在这里:

  1. “scanf("%c,%C",&player1,&player2); ”这一句,第二个%c大写了,改一下。

  2. scanf的实质是将键盘输入字符存入声明字符变量时开辟的一段存储区域。第一局运行正常,而第二局往后你只是单纯的想覆盖输入,这是不可以的——因为你在上一次使用scanf后没有清空输入缓存, 这样再次使用scanf的时候函数就会认为你已经输入过了。

  3. 改进只需要在scanf之前加上fflush(stdin)清空输入缓存。

如图:

希望帮上忙。

㈨ 用c语言设计人和电脑的猜拳游戏,越简单越好,谢谢

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intisWin(int,int);//返回:0:平均。1:玩家胜。2:玩家败
intmain()
{
charp,name[3][5]={"石头","剪刀","布"};
intn;
srand(time(NULL));
while(1)
{
printf("请输入你要出拳对应的数字:0、石头。1、剪刀。2、布。 ");
scanf("%[0-2]",&p);
getchar();
n=rand()%3;
printf("电脑:%s! 玩家:%s! ",name[n],name[(p-'0')]);
switch(isWin(n,p-'0'))
{
case0:printf("玩家败! ");break;
case1:printf("玩家胜! ");break;
case2:printf("平局! ");break;
default:printf("error ");
}
}
return0;
}

intisWin(intn1,intn2)//n1:电脑出拳。n2:玩家出拳。返回:2:平均。1:玩家胜。2:玩家败
{
if(n1==n2)
return2;
if(n2-n1==-1||n2-n1==2)
return1;
if(n2-n1==1||n2-n1==-2)
return0;
return-1;
}

㈩ 就C语言中 猜拳游戏的代码

这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
下面的代码会实现一个猜拳游戏,让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。
启动程序后,让用户出拳,截图:

用户出拳,显示对决结果:截图:

代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 电脑出拳
int result; // 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while (1){
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;

default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}

srand((unsigned)time(NULL)); // 随机数种子
computer=rand()%3; // 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石头\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石头\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你赢了!");
else if (result==5||result==9||result==10) printf("电脑赢了!");
else printf("平手");
system("pause>nul&&cls"); // 暂停并清屏
}
return 0;
}
代码分析
1) 首先,我们需要定义3个变量来储存玩家出的拳头(gamer)、电脑出的拳头(computer)和最后的结果(result),然后给出文字提示,让玩家出拳。
接下来接收玩家输入:
scanf("%c%*c",&gamer);