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

int game();
void calendar(int year, int month, int date);
int leapyear(int year);
void print_Calendar(int firstDay, int days);
void lucky();
void lottery();

int main(){
	int choice;
	int year,month,date;
	if ( game() == 0 ){
		return 0;
	}
    
	printf("now you can choice three present for reward that you won the game\n");
	printf("1 生日當月的日曆\n");
	printf("2 星座當日運勢\n");
	printf("3 一張有五組號碼的樂透彩券\n");
	printf("please selecet you choice (1~3):");
	scanf("%d",&choice);
	
	switch(choice){
		case 1:
			printf("你的生日月份??  ");
			scanf("%d",&month);
			printf("你的生日日期??  ");
			scanf("%d",&date);
			printf("今年是西元幾年??  ");
			scanf("%d",&year);
			printf("\n\n");
			calendar(year,month,date);
			break;
		
		case 2:
			printf("你的生日月份??  ");
			scanf("%d",&month);
			printf("你的生日日期??  ");
			scanf("%d",&date);
			printf("\n\n");
			lucky();
			break;

		case 3:
			printf("\n\n");
			lottery();
			break;
	}
	
	system("pause");
	return 0;
}

//game function是一個猜數字遊戲，從1~14中選出一個不為7的數字，讓遊戲者猜選中的數字比七大或比七小，
//如果連續三次猜中就可以有獎品，如果在20次機會中都沒有連續三次猜中，就gameover了

int game(){

	int play_count = 0;
	int win_count = 0;
	int answer = 7;
	int choice;
    
	
	while (play_count <= 20 ){

		printf("guess the answer is larger or smaller than 7 ?\n");
		printf("2 for larger than 7 and 1 for smaller than 7, and 0 is quit this game:");
		scanf("%d",&choice);
		
		if( choice != 2 && choice !=1){
			printf("BYEBYE!!");
			return 0;
		}
		
		answer = 7;
		while (answer == 7){
			srand( time(NULL) );
			answer = rand() % 14;
		}
		
		if( (answer > 7 && choice == 2) || (answer < 7  && choice == 1) ){
			win_count++;
			if (win_count == 3){
				printf("\n");
				return 1;
			}
		}
		else {
			win_count = 0;
		}

		play_count++;
		printf("you have played %d times, and won %d times continuously\n",play_count,win_count);

	}
	
	if (play_count > 20 )
		printf("I haven't seen a such stupid person like you so far. Bye bye....");
		return 0;

}

//這個function是用來計算傳入的日期是這一年的第幾天，還有傳入month的第一天是星期幾，並決定這個月有幾天
//得到這兩個資訊便可得到這個月的月曆了

void calendar(int year, int month, int date){
	
	int i,y,days,first_Day_of_Year,firstDay;
	int month_Days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

	if ( leapyear(year) )
		month_Days[2] = 29;

	for ( i = 1 ; i < month ; i++)
		days = days + month_Days[i];
	
	days = days + date;

	y = year - 1;
	first_Day_of_Year = (36+y+(y/4)-(y-100)+(y/400))%7+1;
	
	firstDay = days % 7;
	firstDay = firstDay + first_Day_of_Year - 1;
	firstDay--;//因為print_Calendar這function的firstDay參數星期日為0，星期一為1..... 星期六為6
	firstDay = firstDay % 7;	
	print_Calendar(firstDay,month_Days[month]);

}

int leapyear(int year){
	
	if( (year % 4 == 0 && year % 100 !=0) || (year % 400 == 0 ) )
		return 1;
	
	else return 0;
}
		
//這個function是用來劃當月月曆的，firstDay是這個月的一號是星期幾，我們把星期日當作0，
//days則是這個月總共有幾天

void print_Calendar(int firstDay, int days){
	
	int count = 1;
	int curPrint = firstDay; //curPrint這變數的用意是在畫calendar能知道現在作到那一天以決定是否要換行
	int i = 0;
	
	printf("Sun Mon Tue Wed Thu Fri Sat\n\n");
	for (i=0 ; i<firstDay ; i++)
		printf("    ");
		

	while(count <= days){
		if( count < 10 )
			printf("  %d ",count);
		else
			printf(" %d ",count);
		
		count++;
		curPrint++;
		curPrint = curPrint % 7;
		if(curPrint == 0)
			printf("\n\n");
	}		
	printf("\n\n\n\n");
}

void lucky(){
	printf("你今天會很幸運喔!!!\n\n\n\n");
}

void lottery(){
	int pick,i;
	int count = 0;
	int number[42] = {0};
	
	while (count < 5 ){
		srand( time(NULL) );
		pick = rand() % 42 +1;
		
		if( number[pick] == 0){
			number[pick] = 1;
			count++;
		}
		else continue;
	}
	
	printf("The five number pick by computer is:\n");

	for( i=1 ; i<=42 ; i++){
		if (number[i] == 1)
			printf("  %d  ",i);
	}
	printf("\n\n\n\n\n");

}





