/* 
 * Author:     Wil Schrader
 * Date:       9/13/2009
 * Version:    1.0
 * Class:      CIT 131 / Fall 2009 
 *
 * Assignment: 2) Slot Machine
 *                A basic slot machine game.
 */
#include <stdio.h>
#include <stdlib.h>

/* Function Prototype */
int random( int low, int high );

/* Main Function */
int main( void )
{
   int running = 1;        /* Controls the continious execution of the game */
   int chips = 1000;       /* The starting amount of chips */
   int selection, bet;     /* Menu Selection and Bet Amount */
   int num1, num2, num3;   /* The three Slot Machine numbers */

   /* While The Game Is Running */
   while (running) {

      /* Display the number of Chips and Menu */
      printf( "Player's chips: $%d\n", chips );
      printf( "1) Play slot. 2) Exit. " );
      scanf( "%d", &selection );

      if ( selection == 1 ) {

         /* Ask for a bet and then loop until a valid bet is entered */
         do {
            printf( "Enter your bet: " );
            scanf( "%d", &bet );

            if (bet > chips)
               printf( "You did not enter a valid bet.\n" );
         } while ( bet > chips );

         /* Remove the bet amount from their chips */
         chips -= bet;

         /* Get the random numbers and print them to the screen */
         num1 = random( 2, 7 ); num2 = random( 2, 7 ); num3 = random( 2, 7 );
         printf( "%d %d %d\n", num1, num2, num3 );

         /* 7 7 7 */
         /* Win Bet x 10 */
         if ( num1 == 7 && num2 == 7 && num3 == 7 ) {
            chips += bet * 10;
            printf( "You win!\n" );
         }
         /* All Three Numbers The Same */
         /* Win Bet x 5 */
         else if ( num1 == num2 && num2 == num3 ) {
            chips += bet * 5;
            printf( "You win!\n" );
         }
         /* Any Two The Same */
         /* Win Bet x 3 */
         else if ( num1 == num2 || num1 == num3 || num2 == num3 ) {
            chips += bet * 3;
            printf( "You win!\n" );
         }
         /* Otherwise, they've lost */
         else {
            
            /* If they have no more chips, exit the game */
            if ( chips <= 0 ) {
               printf( "You've busted.\n" );
               printf( "Exiting..." );
               running = 0;
            }
            /* Otherwise, they've just lost this round */
            else {
               printf( "You lose.\n" );
            } /* End If */

         } /* End If */

      }
      /* Exit the Game */
      else if ( selection == 2 ) {
         printf( "Exiting..." );
         running = 0;
      } /* End If */

   } /* End While Loop */

   return 0; /* Program ended successfully */

} /* End Main Function */

/* Random Function */
int random( int low, int high )
{

   /* Return the random number */
   return (low + rand() % high);

} /* End Random Function */