IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言示例,三个骰子 -> 正文阅读

[C++知识库]C语言示例,三个骰子

此代码为原创内容,仅供个人学习,禁止转发或用于商业通途。

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

#define _bet_type 4
typedef unsigned char dice;
typedef unsigned int bet;

void msleep(long);
void print_at(int,int,char*);
void draw_dice(int,int,int);
void roll_dices(dice*);
void draw_dices(dice*);
void roll(dice*);
void list_bets(dice*,bet*);
int check_bets(dice*,bet*);

void msleep(long ms)
{
	struct timespec ts;

	ts.tv_sec = ms / 1000;
	ts.tv_nsec = (ms % 1000) * 1000000;
	nanosleep(&ts, &ts);
}
void print_at(int x,int y,char* s)
{
	printf("\e[?25l");
	printf("\033[%d;%dH%s\n",y,x,s);
	printf("\e[?25h");
}
void draw_dice(int x,int y,int point)
{
	switch(point)
	{
		case 1:
			print_at(x,y,"     ");y++;
			print_at(x,y,"     ");y++;
			print_at(x,y,"  *  ");y++;
			print_at(x,y,"     ");y++;
			print_at(x,y,"     ");
			break;
		case 2:
			print_at(x,y,"     ");y++;
			print_at(x,y,"  *  ");y++;
			print_at(x,y,"     ");y++;
			print_at(x,y,"  *  ");y++;
			print_at(x,y,"     ");
			break;
		case 3:
			print_at(x,y,"     ");y++;
			print_at(x,y,"   * ");y++;
			print_at(x,y,"  *  ");y++;
			print_at(x,y," *   ");y++;
			print_at(x,y,"     ");
			break;
		case 4:
			print_at(x,y,"     ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y,"     ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y,"     ");
			break;
		case 5:
			print_at(x,y,"     ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y,"  *  ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y,"     ");
			break;
		case 6:
			print_at(x,y,"     ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y," * * ");y++;
			print_at(x,y,"     ");
			break;
	}
}
void roll_dices(dice* dices)
{
	dices[0]=rand()%6+1;
	dices[1]=rand()%6+1;
	dices[2]=rand()%6+1;
}
void draw_dices(dice* dices)
{
	draw_dice(0,1,dices[0]);
	draw_dice(8,1,dices[1]);
	draw_dice(16,1,dices[2]);
}
void roll(dice* dices)
{
	for(int i=0;i<20;i++)
	{
		roll_dices(dices);
		draw_dices(dices);
		msleep(50);
	}
}
void list_bets(dice* dices,bet *bets)
{
	char str[64];

	int point=dices[0]+dices[1]+dices[2];


	sprintf(str,"(s) small(4-10) rate:2 bet:%d",bets[0]);
	if(point>=4&&point<=10)
		printf("\033[0;93m");
	print_at(0,6,str);
	printf("\033[0m");

	sprintf(str,"(b) big(11-17) rate:2 bet:%d",bets[1]);
	if(point>=11&&point<=17)
		printf("\033[0;93m");
	print_at(0,7,str);
	printf("\033[0m");

	sprintf(str,"(T) triple 1 or 6 rate:60 bet:%d",bets[2]);
	if(dices[0]==dices[1]&&dices[1]==dices[2]&&(dices[0]==1||dices[0]==6))
		printf("\033[0;93m");
	print_at(0,8,str);
	printf("\033[0m");

	sprintf(str,"(t) other triple  rate:30 bet:%d",bets[3]);
	if(dices[0]==dices[1]&&dices[1]==dices[2]&&dices[0]!=1&&dices[0]!=6)
		printf("\033[0;93m");
	print_at(0,9,str);
	printf("\033[0m");
}
int check_bets(dice *dices,bet* bets)
{
	int n=0;
	int point=dices[0]+dices[1]+dices[2];
	if(point>=4&&point<=10)
		n+=bets[0]*2;
	if(point>=11&&point<=17)
		n+=bets[1]*2;
	if(dices[0]==dices[1]&&dices[1]==dices[2])
	{
		if(dices[0]==1||dices[1]==6)
			n+=bets[2]*60;
		else
			n+=bets[3]*30;
	}
	memset(bets,0,sizeof(int)*_bet_type);
	return n;
}
int main(int count,char** args)
{
	srand(time(NULL));
	int coin=10000;
	if(count>=2)
	{
		coin=atoi(args[1]);
		if(coin<=0)coin=10000;
	}
	dice dices[3];
	bet bets[_bet_type];
	memset(bets,0,sizeof(int)*_bet_type);
	roll_dices(dices);
	char b;
	int n=10;
	do{
		system("clear");
		draw_dices(dices);
		list_bets(dices,bets);
		char str[30];
		sprintf(str,"coins = %d",coin);
		print_at(0,11,str);
		print_at(0,12,"input \"TYPE[,AMOUNT]\" to bet, \"e\" to roll, \"q\" to quit.");
		if(n<5||n>500)
		{
			printf("\033[0;31m");
			print_at(0,13,"bets must be greater than 5 and less than 500!");
			printf("\033[0m");
		}
		scanf("%c,%d",&b,&n);

		if(b=='e')
		{
			roll(dices);
			coin+=check_bets(dices,bets);			
			continue;
		}
		if(b=='q')break;

		if(n<5||n>500)continue;

		switch(b)
		{
			case 's':
				coin+=bets[0];
				bets[0]=n;
				coin-=n;
				break;
			case 'b':
				coin+=bets[1];
				bets[1]=n;
				coin-=n;
				break;
			case 'T':
				coin+=bets[2];
				bets[2]=n;
				coin-=n;
				break;
			case 't':
				coin+=bets[3];
				bets[3]=n;
				coin-=n;
				break;
		}
	}while(1);

	printf("\nbye!\n");
	return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-29 10:03:43  更:2021-09-29 10:05:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:45:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码