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

简陋的c语言猜拳游戏

发布时间: 2022-04-23 18:41:35

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);

2. C语言猜拳小游戏程序求助

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_int(void); //规范输入的函数
int game(int y,int x); //游戏函数
int result(int m,int n); //比较最终结果的函数
int main()
{
int x,y,m,n;
int k = 0;
char q;
printf("1代表石头;2代表剪刀;3代表布;\n");
printf("请输入您的选择.\n");
while(k<3)
{
scanf("%d", &y);
k++;
game(y,x);
}
result(m,n);
system("pause");
return 0;
}
int game(int y,int x)
{
int m = 0; //玩家赢的次数
int n = 0; //电脑赢的次数
srand(time(NULL));
x = rand()%3+1; //取随机数1~3
if(y==1&&x==3)
{
printf("你出石头\n");
printf("电脑出布\n");
printf("你输了\n");
++n; //电脑赢的次数
}
else if(y==1&&x==1)
{
printf("大家都出石头,平局\n");
++m;
++n;
}
else if(y==1&&x==2)
{
printf("你出石头\n");
printf("电脑出剪刀\n");
printf("你赢了\n");
++m; //玩家赢的次数
}
if(y==2&&x==1)
{
printf("你出剪刀\n");
printf("电脑出石头\n");
printf("你输了\n");
++n;
}
else if(y==2&&x==2)
{
printf("大家都出剪刀,平局\n");
++m;
++n;
}
else if(y==2&&x==3)
{
printf("你出剪刀\n");
printf("电脑出布\n");
printf("你赢了\n");
++m;
}
if(y==3&&x==1)
{
printf("你出布\n");
printf("电脑石头\n");
printf("你赢了\n");
++m;
}
else if(y==3&&x==2)
{
printf("你出石头\n");
printf("电脑出剪刀\n");
printf("你输了\n");
++n;
}
else if(y==3&&x==3)
{
printf("大家都出布,平局\n");
++m;
++n;
}
return m,n; //返回m,n的值
}
int result(int m,int n) //比较最终结果
{
if(m<n)
printf("3局%d胜,你输了.\n",m);
else if(m>n)
printf("3局%d胜,你赢了.\n",m);
else if(m==n)
printf("一胜一负一平局,旗鼓相当。\n");
return 0;
}//改好了,直接比较三次出结果就行了呀!最后暂停查看下system("pause");

3. C语言 怎么用C语言设计一个猜拳游戏 剪刀石头布用1.2.3代替 要玩5局3胜,

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

int main()
{
int a,b,i,m=0,n=0;
srand(time(NULL));
for(i=0;i<10;i++)
{
a=rand()%3+1;
printf("%d",a);
b=rand()%3+1;
printf("%d\n",b);
if(a>b) m++;
else if(a<b) n++;
if(m>3)
{
printf("a is the winner");
break;
}
if(n>3)
{
printf("b is the winner");
break;
}

}
return 0;
}

4. c语言猜拳游戏问题,求大神帮忙看一下

getchar()放在scanf输入之前,其他小改动。

#include "stdafx.h"

#include <iostream>

#include <time.h>

using namespace std;


int main()

{

int times,x,user,computer,result,Y=0,T=0,C=0;

char userInput;

cout << "请输入局数:" << endl;

cin >> times;

for (int i = 0; i < times; i++)

{

x = i + 1;

printf(" Match %d:Enter R for Rock,P for paper,or S for scissors:", x);

getchar();

scanf_s("%c", &userInput, 1);

switch (userInput)

{

case 82:

user = 2;

break;

case 83:

user = 1;

break;

case 80:

user = 3;

break;

}

srand((unsigned)time(NULL));

computer = rand() % 3 + 1;

if (computer == 1)

{

printf(" The computer chose scissors.");

}

if (computer == 2)

{

printf(" The computer chose rock.");

}

if (computer == 3)

{

printf(" The computer chose paper.");

}

result = computer - user;

if (result == -1 || result == 2)

{

Y += 1;

printf("You win. ");

printf(" Scores: ");

}

else if (result == 1 || result == -2)

{

C += 1;

printf("You lose. ");

printf(" Scores: ");

}

else if (result == 0)

{

T += 1;

printf("You tied. ");

printf(" Score: ");

}

if (T != 0)

{

printf(" Ties-%d", T);

}

if (Y != 0)

{

printf(" You-%d", Y);

}

if (C != 0)

{

printf(" Computer-%d", C);

}

}

printf(" ");

system("pause");

return 0;

}

5. c语言问题 猜拳游戏

整体还是很好的。

问题在这里:

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

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

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

如图:

希望帮上忙。

6. C语言 猜拳游戏编程

#include
using
namespace
std;
main
()
{
int
a,b,c,d;
char
e;
cout<<"1表示石头
2表示剪刀
3表示布."<
>a;
b=rand
()%3+1;
cout<<"电脑出的是"<
>e;
}
cout<
评论
0
0
0
加载更多

7. 求帮忙写一个C语言的猜拳小游戏

import java.util.Scanner;

public class aaa{
public static void main(String[] args){

int count1 = 0;
int count2 = 0;
String dnc = "";
String nic = "";

while(true){

int dn = (int)(Math.random()*3+1);
int ni = 0;
while(true){
System.out.println("请输入1-3的数字");
Scanner s1 = new Scanner(System.in);
ni = s1.nextInt();
if(ni>=1 && ni<=3){
break;
}
}

if(ni==1){
nic = "石头";
}else if(ni==2){
nic = "剪刀";
}else{
nic = "布";
}
if(dn==1){
dnc = "石头";
}else if(dn==2){
dnc = "剪刀";
}else{
dnc = "布";
}

if(dn==1 && ni==2 || dn==2 && ni==3 || dn==3 && ni==1){
System.out.println("电脑赢了1次!电脑出:"+dnc+",你出:"+nic+"");
count1++;
}else if(dn==ni){
System.out.println("平局!电脑出:"+dnc+",你出:"+nic+"");
}else{
System.out.println("你赢了1次!电脑出:"+dnc+",你出:"+nic+"");
count2++;
}

if(count1==2){
System.out.println("三局两胜,电脑赢了!");
break;
}else if(count2==2){
System.out.println("三局两胜,你赢了!");
break;
}
}
}
}

这个是个Java源代码 是Java文件 在DOS命令框运行 至于C语言不了解 但是C语言和Java语言基本上相似,所以写了这段代码 希望可以帮到你

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

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

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

9. 怎样用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";
}