黑马程序员技术交流社区
标题:
c语言游戏,猜拳
[打印本页]
作者:
Q曲
时间:
2015-11-16 19:58
标题:
c语言游戏,猜拳
/*
使用随机数产生一个1-1000的数,给用户10次竞猜的机会
*/
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
void test1(){
//定义两个变量
int computer=-1,player=-1;
//系统产生一个随机数
srand(time(0));
computer=rand()%3;
printf("%d\n",computer); //2
//玩家输入一个数字
printf("请输入:0石头 1剪刀 2布\n");
scanf("%d",&player);
//比较大小 玩家 系统 赢得情况
// 0 1
// 1 2
// 2 0
if((player==0 && computer==1)||
(player==1 && computer==2)||
(player==2 && computer==0)){
printf("玩家赢\n");
}
//玩家输
else if((computer==0 && player==1)||
(computer==1 && player==2)||
(computer==2 && player==0)){
printf("玩家输了\n");
}
//平局
else{
printf("平局\n");
}
}
int main(){
//定义用户猜的数
//系统随机的数
//竞猜的总次数
//现在已经竞猜的次数
//数字的范围
int userNum=0;
int comNum=0;
int total=10;
int nowTotal=0;
int m,n;
//系统产生随机数
srand(time(0));
comNum=rand()%1000+1;
printf("%d\n",comNum);
//用户输入一个数
printf("请输入1-1000之间的数字:\n");
scanf("%d",&userNum);
//判断竞猜的次数
while(1){
//可以竞猜,判断大小,
if(userNum<comNum){
printf("你输入的数字小了,请重输!\n这是第%d次输入",nowTotal);
}else if(userNum>comNum){
printf("你输入的数字大了,请重输!\n这是第%d次输入",nowTotal);
}else(userNum=comNum){
printf("恭喜你,猜对了\n这是第%d次输入",nowTotal);
nowTotal++;
}
//不可以竞猜,提示退出
printf("你的次数已经用完,再见~!\n");
return 0;
}
复制代码
我是拿visualC++6.0 写的代码,可能和Xcode有些不同。
作者:
liuchuan_alex
时间:
2015-11-16 21:41
666666666666666
作者:
arhui
时间:
2015-11-16 21:59
好牛逼 666666
作者:
石油小硕
时间:
2015-11-16 22:04
赞一个,程序媛同学,加油
作者:
江流石不动
时间:
2015-11-16 22:05
{:2_30:}{:2_30:}{:2_30:}{:2_30:}
作者:
陌忆
时间:
2015-11-17 22:55
加油加油
作者:
junjunzhang
时间:
2015-11-18 14:47
/*
剪刀石头布游戏规则:项目:1⃣️剪刀 0;2⃣️石头 1;3⃣️布 2.
① 剪刀 --> 布
② 石头 --> 剪刀
③ 布 --> 石头
④ 平局
计算机computer:随机出拳 剪刀、石头、布
玩家player :随机出拳 剪刀、石头、布
附:随机数函数
arc4random_uniform();
使用方法:
首先导入头文件:
#include <stdlib.h>
arc4random_uniform(3); //随机产生 0 1 2
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
// 定义变量,保存计算机、用户出的随机的项目
int computer,player;
// 计算机随机输出项目
computer = arc4random_uniform(3);
// printf("%d\n",computer);
// 提示用户输出项目
printf("请出拳:0.剪刀 1.石头 2.布\n");
// 保存用户输出项目
scanf("%d\n",&player);
// 校验用户输入是否合法
if (player < 0 || player > 2) {
printf("请按套路出拳!\n");
return 0;
}
// 根据游戏规则,判断输赢
// 输出用户输出项目的输赢结果
if ((player == 0 && computer == 2) ||
(player == 1 && computer == 0) ||
(player == 2 && computer == 1)){
printf("我靠!你赢啦!!\n");
} else
if ((computer == 0 && player == 2) ||
(computer == 1 && player == 0) ||
(computer == 2 && player == 1)){
printf("你输了\n");
} else {
printf("平局\n");
}
return 0;
}
复制代码
这样写,运行程序,每次都要出拳两次才有结果,什么原因?
作者:
能不能行
时间:
2015-11-18 21:22
看上去很牛逼的样子
作者:
hei军
时间:
2015-11-18 21:40
恩恩 就是 感觉代码有点多
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2