//Dan Mayer danmanwointernet@excite.com
//craps.cxx
//Gillet Ian Bratt
//1:00
//PLAY the dice game craps win fake money. Lose fake money.

#include <iostream.h>
#include <stdlib.h>
#include <winbgim.h>
#include <math.h>
#include <stdio.h>
#include <time.h>

void finddicevalue(int& dice1,int& dice2); //finds dice numbers
int checkscores(int& dice1,int& dice2);   //says win or lose
void drawdice(int dice,int side);    //graphics

int main()
{
    char score[80];
    int dice1,dice2,quit=0,c,money=0,play=1,x=0,y=0,page=1;
    initwindow(600, 400);
    srand ( time (NULL) );         // Initialize random generator
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
    outtextxy(100,75,"Click to begin");
    finddicevalue(dice1,dice2);
    outtextxy(100,350,"WELCOME TO CRAPS THE GAME!");
    
    do
    {
	delay(1);
	if ((ismouseclick(WM_LBUTTONDOWN)) && (play==1)) // Left button start
	{
	    outtextxy(100,75,"                                 "); //covers up pregamestuff
	    outtextxy(100,350,"                                   ");
	    clearmouseclick(WM_LBUTTONDOWN);
	    finddicevalue(dice1,dice2);
	    money=money+(checkscores(dice1,dice2));  //updates scores
	    sprintf(score, "Money: %d", money);
	    outtextxy(450,10,"                     ");
	    outtextxy(450,10,score);
	    setfillstyle(SOLID_FILL, RED);
	    fillellipse(580,355,15,15);             //draws the option buttons
	    setfillstyle(SOLID_FILL, GREEN);
	    fillellipse(545,355,15,15);
	    x=0;
	    y=0;
	    play=0;
	    do          //waits for user to click an option
	    {
		delay(0);
		outtextxy(10,350,"Want to play again:"); //ends game or goes another round
		if (ismouseclick(WM_LBUTTONDOWN))
		{
		    getmouseclick(WM_LBUTTONDOWN, x, y);
		    if((getpixel(x,y))==RED)
			quit=1;
		    else if ((getpixel(x,y))==GREEN)
		    {
		    setfillstyle(SOLID_FILL, BLACK);
		    bar(0,330,600,400);
		    outtextxy(150,50,"       ROLL DICE NOW!           "); //says roll
		    play=1;
		    }
		}
	    }while((((getpixel(x,y))!=RED) && (getpixel(x,y)!=GREEN)) && (play!=1));
	}

    }while(quit!=1);
    
    closegraph();                 //close the graph
    return EXIT_SUCCESS;
}


void finddicevalue(int& dice1,int& dice2)
{
    setfillstyle(SOLID_FILL, BLACK); //covers up old dice rolled
    fillellipse(275,220,200,60);
    dice1 = (rand() % 6)+1;        //gets random numbers
    dice2 = (rand() % 6)+1;
    drawdice(dice1,1);
    drawdice(dice2,2);        //draws dice with the new values
}

int checkscores(int& dice1,int& dice2)
{
    
    int dicetotal,point=0,score1=0,clicked=0;
    dicetotal= dice1+dice2;         
    switch (dicetotal)      //switch to decide how to react to roll
    {
    case 1: point=1; break;
    case 2: score1=-1; break;
    case 3: score1=-1; break;
    case 4: point=4; break;
    case 5: point=5; break;
    case 6: point=6; break;
    case 7: score1=1; break;
    case 8: point=8; break;
    case 9: point=9; break;
    case 10: point=10; break;
    case 11: score1=1; break;
    case 12: score1=-1; break;
    }
   
    if ((score1==0) && (point!=0)) //if not a win or lose has roll again until win
    {
	do
	{
	    delay(0);
	    outtextxy(150,50,"Click to roll again     ");
	    if (ismouseclick(WM_LBUTTONDOWN)) // Left button. 
	    {
		clearmouseclick(WM_LBUTTONDOWN);
		finddicevalue(dice1,dice2);
		clicked=1;
	    }
	    if(clicked==1)
	    {
		clicked=0;
		dicetotal= dice1+dice2;
		if(dicetotal==point)
		{
		    score1=1;
		    dicetotal=101;
		}
		if(dicetotal==7)
		{
		    score1=-1;
		    dicetotal=101;
		}
	    }
	}while(dicetotal<100);  //if dicetotal goes above hundred means win or lose 
    }
    if(score1==1)
	outtextxy(150,50,"       You WON!          ");   /tells them win or lose
    if(score1==-1)
	outtextxy(150,50,"       You LOST!          ");
    return score1;   
}

void drawdice(int dice, int side)    //draws dice
{
    int width=50,x;
    setfillstyle(SOLID_FILL, WHITE);
    if(side==1)
	x = 200;
    if(side==2)
	x = 300;
    rectangle(x,200,(x+width),(200+width));
    if(dice==1)
	fillellipse((x+(width/2)),(200+(width/2)),5,5);   //puts dots where they go
    if(dice==2)
    {
	fillellipse((x+(width/4)),(200+(width/2)),5,5);
	fillellipse((x+((3*width)/4)),(200+(width/2)),5,5);
    }
    if(dice==3)
    {
	fillellipse((x+(width/2)),(200+(width/4)),5,5);
	fillellipse((x+(width/4)),(200+(3*width/4)),5,5);
	fillellipse((x+((3*width)/4)),(200+(3*width/4)),5,5);
    }
    if(dice==4)
    {
	fillellipse((x+(width/4)),(200+(width/4)),5,5);
	fillellipse((x+((3*width)/4)),(200+(width/4)),5,5);
	fillellipse((x+(width/4)),(200+(3*width/4)),5,5);
	fillellipse((x+((3*width)/4)),(200+(3*width/4)),5,5);
    }
    if(dice==5)
    {
	fillellipse((x+(width/4)),(200+(width/6)),5,5);
	fillellipse((x+((3*width)/4)),(200+(width/6)),5,5);
	fillellipse((x+(width/2)),(200+(3*width/6)),5,5);
	fillellipse((x+(width/4)),(200+(5*width/6)),5,5);
	fillellipse((x+((3*width)/4)),(200+(5*width/6)),5,5);
    }

    if(dice==6)
    {
	fillellipse((x+(width/4)),(200+(width/6)),5,5);
	fillellipse((x+((3*width)/4)),(200+(width/6)),5,5);
	fillellipse((x+(width/4)),(200+(3*width/6)),5,5);
	fillellipse((x+((3*width)/4)),(200+(3*width/6)),5,5);
	fillellipse((x+(width/4)),(200+(5*width/6)),5,5);
	fillellipse((x+((3*width)/4)),(200+(5*width/6)),5,5);
    }
}
