#include <stdlib.h>
//用戶信息
struct User
{
int win,lose;
int counters,countersInUse;
}User;
//初始化用戶信息
structUser initUser()
{
structUser me;
me.win=0;
me.lose=0;
me.counters=300;
return me;
};
//顯示用戶信息
void displayUser(structUser me)
{
printf("You have win:%d\n",me.win);
printf("You have lose:%d\n",me.lose);
printf("You still have counters:%d\n",me.counters);
}
//擲色子的信息
struct Game
{
int one,two,three;
int sum;
}Game;
//初始化擲色子的信息
structGame initGame()
{
structGame game;
game.one=0;
game.two=0;
game.three=0;
game.sum=0;
return game;
}
//顯示所擲色子的信息
void displayGame(structGame game)
{
printf("The first toss of dice is %d\n",game.one);
printf("The second toss of dice is %d\n",game.two);
printf("The third toss of dice is %d\n",game.three);
printf("The sum toss of dice is %d\n",game.sum);
}
//如果賭博成功,並且出現三個數字相同的情況,出現Congratulation信息
void displayCongratulations(structGame game)
{
printf("**************************************\n");
printf("Congratulations For Your Good Luck!\n");
printf("You have toss the number:%d%d%d\n",game.one,game.two,game.three);
printf("You have the ten times reward!\n");
printf("**************************************\n");
}
//輸入用戶的賭博籌碼,該數據X50為真正的籌碼
//註意,該籌碼不能小與等於0,並且不能大於妳所擁有的籌碼
structUser inputYourCounters(structUser me)
{
int theCounters;
printf("Please input your counters(the value * 50):");
scanf("%d",&theCounters);
while (theCounters<=0||theCounters*50>me.counters) {
printf("The counters you input is illegal!");
printf("Please input your counters again:");
scanf("%d",&theCounters);
}
me.countersInUse=theCounters*50;
me.counters=me.counters-me.countersInUse;
return me;
}
//輸入妳對色子的期望值:大或小
int inputYourExpected()
{
int expected;
printf("Please input 0 or 1 to stand for your expected value(0:small 1:big):");
scanf("%d",&expected);
while (expected!=0 && expected!=1) {
printf("expected value is illegal!\n");
printf("Please input 0 or 1 to stand for your expected value again(0:small 1:big):");
scanf("%d",&expected);
}
return expected;
}
//擲色子
int tossOneDice()
{
returnrandom()%6+1;
}
//處理三次擲色子的結果
structUser HandleTossingDice(structUser me,structGame game,int expected)
{
game.one=tossOneDice();
game.two=tossOneDice();
game.three=tossOneDice();
game.sum=game.one+game.two+game.three;
displayGame(game);
if (expected==0) {
if (game.sum<10) {
if (game.one==game.two&&game.one==game.three) {
me.counters=me.counters+10*me.countersInUse;
displayCongratulations(game);
}
else
{
me.counters=me.counters+2*me.countersInUse;
}
me.win++;
return me;
}
else{
me.countersInUse=0;
me.lose++;
return me;
}
}
else
{
if (game.sum>9) {
if (game.one==game.two&&game.one==game.three) {
me.counters=me.counters+10*me.countersInUse;
displayCongratulations(game);
}
else
{
me.counters=me.counters+2*me.countersInUse;
}
me.win++;
return me;
}
else{
me.countersInUse=0;
me.lose++;
return me;
}
}
}
//本局結束,是否還要再來壹局;小賭怡情,大賭養家......
int isGoOn()
{
char goOn;
while(1)
{
printf("go on(yes:y, no:n):");
scanf("%c",&goOn);
if (goOn=='y') {
return1;
}
elseif(goOn=='n')
{
return0;
}
}
}
//程序入口
int main(int argc, constchar * argv[])
{
structUser me=initUser();
displayUser(me);
structGame game=initGame();
displayGame(game);
while (1) {
me=inputYourCounters(me);
int expected=inputYourExpected();
me=HandleTossingDice(me, game, expected);
displayUser(me);
if (isGoOn()==0) {
return0;
}
}
return0;
}