/**
 * Game of Craps, implemeted with wager
 * @author Fazli Can & Dilek Demirel
 * @version Game of craps (March 31, 2002).
 */
 
import cs1.Keyboard;

public class GameOfCraps
{
    public static void main(String args[ ])
    {
        int moneyAmount;
        char userResponse;
        int wagerAmount;
        boolean moreGame = false;
        
        //asks for the initial amount of money
        moneyAmount= getMoney( );

        do {
            System.out.println("You have this much money: " + moneyAmount);
            System.out.println("Do you want to play craps (y, n)?");
            userResponse= Keyboard.readChar( );
            if (userResponse == 'y' || userResponse == 'Y')
                moreGame = true;
            else 
            if (userResponse == 'n' || userResponse == 'N')
                moreGame = false;
            else{
                System.out.println("Illegal input!");
                moreGame = false;
            }
                
            if (moreGame==true) {
                //get wager amount
                wagerAmount= getWagerAmount(moneyAmount);
                if(playGame( ) == true)//if the player wins
                    moneyAmount = moneyAmount + wagerAmount;
                else
                    moneyAmount = moneyAmount - wagerAmount;
            }
        } while(moreGame && moneyAmount > 0); 
        // the game continues until the user enters Y or y and the s/he has money left
        
        if(moneyAmount == 0)
            System.out.println("You have no money left. Please leave the play grounds.");        
    }

//======================================================================
// the method to roll a die
    public static int rollDie( )
    {
        int faceValue= (int) (Math.random( )*6) + 1;
        return(faceValue);
    }

//======================================================================
// the method simulating one run of the game
    public static boolean playGame( )
    {
        int score, die1, die2, myNo= 0;
        boolean firstTry, done, wins= false;
        firstTry= true;
        done= false;

        while(!done) {
            die1= rollDie( );
            die2= rollDie( );
            score= die1 + die2;
            System.out.println("score: " + score);
            if(firstTry){
                firstTry = false;
                switch(score) {
                    case 7: 
                        wins= true;
                        done= true;
                        break;
                    case 11:
                        wins= true;
                        done= true;
                        break;
                    case 2: 
                        wins= false;
                        done= true;
                        break;
                    case 3: 
                        wins= false;
                        done= true;
                        break;
                    case 12:
                        wins= false;
                        done= true;
                        break;
                    default:  // 4, 5, 6, 8, 9, 10
                        myNo= score;
                        break;
                 }
            }
            else {
                    if(score == myNo) {
                        wins= true;
                        done= true;
                    }
                    else 
                    if(score == 7){
                        wins= false;
                        done= true;
                    }
            }
        }  // end of while
        if(wins)
            System.out.println("You win!");
        else
            System.out.println("You lose");
        return(wins);
    }   
                                
//=======================================================================
// the method to prompt for the initial amount of money
    public static int getMoney( )
    {
        int inputMoney;
        boolean error=false;
        do {
            System.out.println("Enter the amount of your gambling money:");
            inputMoney= Keyboard.readInt( );
            if (inputMoney <= 0)
            {
                error = true;
                System.out.println("Error: please enter a positive int value.");
            }
        } while(error);
        return(inputMoney);
    }
//=======================================================================
// the method to prompt for the wager amount
    public static int getWagerAmount(int maxMoney)
    {
        boolean done= false;
        int wagerMoney;
        do {
            System.out.println("Enter the wager amount, max can be: " + maxMoney +"\nEnter zero to quit");
            wagerMoney= Keyboard.readInt( );
            if (wagerMoney == 0)
                System.exit(0);
            //wager amount must be psotive integer and less that the max money of the player
            if(wagerMoney <= maxMoney && wagerMoney > 0)
                done= true;
            else
                System.out.println("Error: Illegal amount");
        } while(!done);
        return(wagerMoney);
    }
 }

