㈠ 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;
}
㈡ 求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语言编程:编写一个猜数的游戏,系统自动产生一个随机数,你来猜,程序给出提示,直到猜对为止。
import java.util.*;
class Assignment8{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int x=(int)(Math.random()*100);//生成一个0~100的随机数
int y=-1;
System.out.println("已生成0~100的随机整数,请输入您所猜的数:");
while(x!=y)
{
y=sc.nextInt();
if(y>x)
{
System.out.println("输入的数过大");
}
else if(y<x)
{
System.out.println("输入的数过小");
}
}
System.out.println("正确!该随机数是"+x);
sc.close();
}
}
(3)C语言猜拳游戏随机代码扩展阅读:
while循环的格式:while(表达式){语句;}
while循环的执行顺序:当表达式为真,则执行下面的语句,语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句,然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环。
例:
int a=NULL;
while(a<10){
a++;//自加
if(a>5)//不等while退出循环,直接判断循环
{break;//跳出循环}
}
结果:结束后a的值为6。
㈣ C语言编写三局两胜的猜拳游戏编程
/*假设有A和B进行猜拳
'x'表示剪刀,'y'表示石头,'z'表示布
规则如下:
1、 石头 > 剪刀
2、 布 > 石头
3、 剪刀> 布
*/
#include <stdio.h>
#define WIN 1
#define LOSE -1
#define EQUAL 0
/*比赛规则函数*/
int game_rule(char a,char b)
{
switch(a)
case 'x':
{
switch(b)
case 'x':return EQUAL;
case 'y':return LOSE;
case 'z':return WIN;
}
case 'y':
{
switch(b)
case 'x':return WIN;
case 'y':return EQUAL;
case 'z':return LOSE;
}
case 'z':
{
switch(b)
case 'x':return LOSE;
case 'y':return WIN;
case 'z':return EQUAL;
}
}
/* main function*/
void main(void)
{
int ans;
int a = b = 0;
char A;
char B;
do
{
prinf("please input A:\n");
scanf("%c",&A);
getchar();
prinf("please input B:\n");
scanf("%c",&B);
getchar();
//有效性检查请自己加上
ans = game_rule(A,B);
if(ans==WIN)
{
a++;
prinf("A赢了%d局\n",a);
}
else if(ans==LOSE)
{
b++;
prinf("A赢了%d局\n",b);
}
if((a==2)||(b==2))
{
prinf("game over");
break;
}
}while(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);
㈥ C语言 猜拳游戏编程
#include<stdio.h>
#include<math.h>
main()
{char c1;
int i,j;
do{
printf("Please input :");
scanf("%d",&i);
if(i<1||i>3){printf("Input error!\nPlease input again!\n");continue;}
j=rand()%3+1;
printf("%d\n",j);
if(i==j)printf("Ping Ju!\n");else
if((i-j)==1)printf("YOU WIN!\n");else
if(i==1&&j==3)printf("YOU WIN!\n");else
printf("YOU LOSE!\n");
printf("Replay?(Y/N)");
scanf("%c",&c1);
if(c1=='n'||c1=='N')break;
}while(c1=='y'||c1=='Y');
printf("Welcome to play again!\nAny key Quit...");
getch();putchar('\n');
}
㈦ 怎样用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语言的猜拳小游戏
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语言基本上相似,所以写了这段代码 希望可以帮到你
㈨ C语言猜拳游戏代码 帮忙 改改 能运行就行 五局三胜制是关键
#include <stdio.h>
#include <math.h>
#include <time.h>
int main()
{
int a,b,i,s,s1;
i=1;
s=0;
s1=0;
srand((unsigned)time(NULL));
printf("猜拳游戏\n0石头1剪刀2布\n***********\n");
printf("规则:共有5局");
printf("请\n出\n拳\n..\n");
do
{
scanf("%d",&b);
a=rand()%3;
if(a==0&&b==0)
{printf("电脑出的是拳头,您出的是拳头,平\n");s=s+0;}
else if(a==0&&b==1)
printf("电脑出的是拳头,您出的是剪刀,负\n");
else if(a==0&&b==2)
{printf("电脑出的是拳头,您出的是布,赢\n");s=s+1;}
else if(a==1&&b==0)
{printf("电脑出的是剪刀,您出的是石头,赢\n");s=s+1;}
else if(a==1&&b==1)
{printf("电脑出的是剪刀,您出的是剪刀,平\n");s=s+0;}
else if(a==1&&b==2)
printf("电脑出的是剪刀,您出的是布,负\n");
else if(a==2&&b==0)
printf("电脑出的是布,您出的是石头,负\n");
else if(a==2&&b==1)
{printf("电脑出的是布,您出的是剪刀,赢\n");s=s+1;}
else if(a==2&&b==2)
{printf("电脑出的是布,您出的是布,平\n");s=s+0;}
else
{printf("存在错误\n");s=s+0;}
}while(i++>5);
printf("\n您的得分为:%d分\n",s);
if(s>2)
printf("您赢了");
else
printf("您输了");
return 0;
}
估计这样就达到你的目的了
㈩ 用C语言编写三局两胜的猜拳游戏,怎么编写
随机种子产生pc的随机出拳
srand(time(0));
int pc = rand()%3; //0, 1, 2 石头剪子布
屏幕输入自己的结果。
scanf(" %d", &var);
一个负责比较的代码块,很简单的逻辑处理。
一个最多执行三次的循环。
两个负责记录胜负次数的变量,针对单一角色,两胜或者两负,都会结束游戏。