此代码为原创内容,仅供个人学习,禁止转发或用于商业通途。
#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;
}
|