下面是一个简陋的猜字游戏,玩了一会儿,发现自己打不过自己写的游戏,除非赢了就跑,最高分没有过1000。

说明:srand(time(NULL))和rand(),srand,time和rand都是函数,其中srand和rand配对使用,srand是start random,也就是随机数的初始化,time函数中的NULL表示获取系统时间,所以整个意思是:以系统时间开始初始化,然后获取随机数。随机数结果是一个整数。

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int cash = 100; void play(int bet) { char *C = (char *) malloc(sizeof(char) * 100);
C[0] = 'J';
C[1] = 'Q';
C[2] = 'K';
printf("Shuffling... \n");
srand(time(NULL)); int i;
for (i = 0; i < 5; i++) {
int x = rand() % 3;
int y = rand() % 3;
char temp = C[x];
C[x] = C[y];
C[y] = temp;
} int playersGuess;
printf("What's your guess for the Queen's position? 1, 2 or 3: ");
scanf("%d", &playersGuess); if (C[playersGuess - 1] == 'Q') {
cash = cash + bet * 3;
printf("You win!, the result is: %c %c %c\n", C[0], C[1], C[2]);
printf("Your money left is $ %d.\n", cash);
} else {
cash = cash - bet * 3;
printf("You lose the bet, the result is: %c %c %c\n", C[0], C[1], C[2]);
printf("Your money left is $ %d.\n", cash);
} free(C);
} void main(void) {
int bet;
printf("Welcome to the virtual Casino!\n");
printf("The total cash you have from the start is $%d.\n", cash); while (cash > 0) {
printf("Please enter your bet: $ ");
scanf("%d", &bet); if (bet <= 0) break;
else if (bet > cash) {
printf("Error. The bet is bigger than your cash.");
break;
} play(bet); }
printf("You lost the game. \n"); }

有时间加入一个重新开始的功能,可玩性会高一些。

C - 一个C语言猜字游戏的更多相关文章

  1. 猜字游戏java

    一.实践目的 1.掌握基本输入输出. 2.掌握方法定义与调用,理解参数传递方式. 3.掌握数组的声明.定义与初始化,数组的处理. 4.掌握数组作为方法参数和返回值. 二.实践要求 利用方法.数组.基本 ...

  2. Leetcode 299.猜字游戏

    猜字游戏 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls&q ...

  3. 044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏

    044 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 06 使用do-while循环实现猜字游戏 本文知识点:do-while循环深入运用 案例练习 案例 ...

  4. 用MFC完成一个简单的猜数字游戏: 输入的四位数中,位置和数字都正确为A,数字相同而位置不同的为B。

    最近学习了MFC一些比较基础的知识,所以打算通过做一个简单的数字游戏来理解MFC的流程并进一步熟悉其操作. 在这里,我做了一个猜数字的小游戏.第一步当然是设计主界面,先给大家展示一下游戏界面: 主界面 ...

  5. C语言猜数字游戏

    猜数字游戏,各式各样的实现方式,我这边提供一个实现方式,希望可以帮到新手. 老程序猿就不要看了,黑呵呵 源代码1 include stdio.h include stdlib.h include ti ...

  6. 51 Nod 不一样的猜字游戏

    1536 不一样的猜数游戏  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 瓦斯亚和皮台亚在玩一个简单的游戏.瓦 ...

  7. c语言-猜数字游戏

    #include <stdio.h> #include <stdlib.h> int top(); int input(); void main() { ; int numbe ...

  8. C语言 猜数游戏--产生一个随机数

    #include <stdio.h> #include <time.h> #include <stdlib.h> int main(int argc, const ...

  9. 用while循环写一个简单的猜数字游戏

    import random #练习:模拟猜数字的游戏 """ 计算机出一个1~100之间的随机数由人来猜 计算机根据人猜的数字分别给出 大一点/小一点/猜中了 的提示 & ...

随机推荐

  1. OSCP Learning Notes - Overview

    Prerequisites: Knowledge of scripting languages(Bash/Pyhon) Understanding of basic networking concep ...

  2. Ethical Hacking - NETWORK PENETRATION TESTING(2)

     ALFA  AWUS 1900 RTL8814AU https://www.alfa.com.tw/products_detail/2.htm Follow the guide on aircrac ...

  3. 3.TCP协议

    一.TCP协议特点和报文段格式 面向连接的传输层协议 每一条TCP连接只能有两个端点 TCP提供可靠交付的服务,无差错,不丢失,不重复,按序到达 全双工通信 -> 发送缓冲:准备发送的数据&am ...

  4. https://blog.csdn.net/yongchaocsdn/article/details/53355296

    https://blog.csdn.net/yongchaocsdn/article/details/53355296

  5. JSONObject遍历

    导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...

  6. Mac Sourcetree克隆项目提示无效的url

    之前用SoucreTree拉去过另一个账号的git项目,今天创建了一个新的码云账号,克隆里面的项目是一直报错误 > 错误如下: > 原因以及解决方案:

  7. springboot 使用 dev tool 导致 CastException

    1.背景 项目使用了 Spring + shiro 实现 权限控制, 使用AOP 对 每个 Controller 进行 log 记录时,需要从 shiro 中 获取 username字段, 问题就这样 ...

  8. Get与Post的区别?(面试官最想听到的答案)

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  9. Python break语句

    Python break语句:当运行到 break 语句时,终止包含 break 的循环语句. 注:无论判断条件是否达到 False 或 序列是否遍历完都会停止执行循环语句和该 break 下的所有语 ...

  10. PHP array_pop() 函数

    实例 删除数组中的最后一个元素: <?php$a=array("red","green","blue");array_pop($a); ...