#!/sw/bin/perl
# Written by Sloan Mergler
# Created: 1-7-05 | Last Modified: 6-5-05
# dopewars.pl  v.0.9.3
# DopeWars - game written in perl based on the classic
use warnings;
use strict;
#used for ceil and floor 
use POSIX;
# use for top 10 list
use IO::All;


# SYSTEM VARIABLES #
# YOU
my %person = (
        "name" => "",
        "money" => 2000,
        "savings" => 0,
        "loan" => 5500,
        "location" => "The Bronx",
        "days" => 30,
        "start_days" => 30,
        "empty_pockets" => 100,
        "acid" => 0,
        "cocaine" => 0,
        "ecstasy" => 0,
        "pcp" => 0,
        "heroin" => 0,
        "weed" => 0,
        "shrooms" => 0,
        "speed" => 0,
        "gun" => 0,
);
#PRICES
my %drugs = (
        "acid" => 1000,
        "cocaine" => 16000,
        "ecstasy" => 30,
        "pcp" => 40,
        "heroin" => 25000,
        "weed" => 60,
        "shrooms" => 250,
        "speed" => 130,
);

#used to see if you have any drugs whatsoever
my $total_drugs;

#needed to staul for time 
my $resume;

#used to determine game length
my $length;

#used to test if user input is correct
my $tester = 0;

# keep track of whether officer hard ass is alive
my $cop = 1;

# officer hardass's deputies
my $deputy = int(rand(2)) + 1;

# global variables for high score
my $score_file;
my $total_money;

#choice for buying drugs
my %drug_hash = (
    "price" => 0,
    "drug_poss" => 0,
    "number" => 0,
    "name" => "", 
);
                                

# START GAME #
system("clear");

print "DDDDD       OO    PPPPPP  EEEEEEE                        \n";
sleep(1);
print "D   DDD   OO  OO  PP   PP EE                             \n";
sleep(1);
print "D    DDD OO    OO PPPPPP  EEEEEEE                        \n";
sleep(1);
print "D   DDD   OO  OO  PP      EE                             \n";
sleep(1);
print "DDDDD       OO    PP      EEEEEEE                        \n";
sleep(1);
print "                  W  WW  W    A     RRRRRR   ZZZZZZZZ    \n";
sleep(1);   
print "                  W  WW  W    AA    RR   RR        ZZ    \n";
sleep(1);
print "                  WW WW WW   A  A   RRRRRR      ZZ       \n";
sleep(1);
print "                   WW  WW   AAAAAA  RR   RR  ZZ          \n";
sleep(1);
print "                    W  W   AA    AA RR    RR ZZZZZZZZ    \n";
sleep(1);

print <<EOF;
The object of the game is to make as much 
money as possible within the given limit
by buying and selling drugs

Rules:
1. you must pay back the loanshark by the end of the game
    or his goons will come and break your legs
2. You can choose your amount of days
    (but 30 is the default) 
3. Watch out for Officer Hardass, he's right on your trail
4. When you move to another part of the city, the day changes, 
   the prices of drugs change and your bank roll and loan 
   increase

Enter your name: 
EOF

chomp($person{ 'name' } = <>);

system("clear");
print "Welcome $person{ 'name'} \n";
while (!($tester)){
print <<EOF;
How long do you want to play for?
1. 15 days
2. 30 days (the usual amount)
3. 45 days
4. 90 days
EOF
    chomp($length = <>);
    $tester = good_input(5, $length);
    if ($tester){
        #left over from testing high score function
        if ($length == 5){
            $person{ 'days' } = 1;
            $person{'start_days'} = 1;
            $score_file = '/home/smergler/dope/1day';
        }
        elsif ($length == 1){
            $person{ 'days' } = 15;
            $person{ 'start_days'} = 15;
            $score_file = '/home/smergler/dope/15day';
        }
        elsif($length == 2){
            $person{ 'days' } = 30;
            $person{ 'start_days'} = 30;
            $score_file = '/home/smergler/dope/30day';
        }
        elsif($length == 3){
            $person{ 'days' } = 45;
            $person{ 'start_days' } = 45;
            $score_file = '/home/smergler/dope/45day';
        }
        elsif($length == 4){
            $person{ 'days' } = 93;
            $person{ 'start_days' } = 90;
            $score_file = '/home/smergler/dope/90day';
        }
        else {print "You shouldn't see this";}
    }
    else {print 'choose again\n';}
}
                

system("clear");
random_drug_price();
#keeps asking till the 0 day
while ($person{ 'days' } >= 0){
    total();
    what_to_do();
    display_pause();
    system("clear");
}

# FUNCTIONS #
#to see if you have any drugs whatsoever
sub total{
        $total_drugs = $person{ 'acid' } +
                       $person{ 'cocaine' } +
                       $person{ 'ecstasy' } + 
                       $person{ 'pcp' } +
                       $person{ 'heroin' } +
                       $person{ 'weed' } +
                       $person{ 'shrooms' } +
                       $person{ 'speed' };
}

#you dump all your drugs 
# refreshes you total
sub dump_drugs {
        $person{ 'empty_pockets'} += $total_drugs;
        $person{ 'acid' }  = 0;
        $person{ 'cocaine' } = 0;
        $person{ 'ecstasy' } = 0;
        $person{ 'pcp' } = 0;
        $person{ 'heroin' } = 0;
        $person{ 'weed' } = 0;
        $person{ 'shrooms' } = 0;
        $person{ 'speed' } = 0;
        total();
}

# this funkshun should be called to make sure that
# user input is acceptable
# it is called by saying
# good_input( max, uinput)
# where max is the most the userinput can be
# and uinput is what it is
# returns true if 0 <= uinput <= max
# returns true also if max is not a number
sub good_input{
        my @input = @_;
        $input[0] =~ /\d/ || $input[0] =~ /\d+/ 
            ? $input[1] <= $input[0]&& $input[1] >= 0 
                ? return 1 
                : return 0
            : return 1;
}
#to staul for more time
sub display_pause{
    print "Press <enter> to continue\n";
    $resume = <>;
}

# displays the choice of drugs and their street values
# could be used for both buy and sell
sub drug_display {
    print <<EOL;
*************************************
  1. Acid at $drugs{ 'acid' }        
  2. Cocaine at $drugs{ 'cocaine' }  
  3. Ecstacy at $drugs{ 'ecstasy' }  
  4. PCP at $drugs{ 'pcp' }          
  5. Heroin at $drugs{ 'heroin' }    
  6. Weed at $drugs{ 'weed' }        
  7. Shrooms at $drugs{ 'shrooms' }  
  8. Speed at $drugs{ 'speed' }      
*************************************
EOL
}

# asks the player what they wanna do 
# and starts the appropriate funkshun
sub what_to_do{
    my $choice = 0;
    
    print <<EOL;
*****************************************
 1. M)ove to another part of the city?    
 2. B)uy drugs?                           
 3. S)ell drugs?                          
 4. L)ist the drugs you have?             
 5. G)o to the Bank                       
 6. P)ay or borrow from the Loan Shark                 
 7. V)iew the Prices of the Drugs          
 8. Q)uit the game                        
*****************************************
You are in $person{ 'location'}          
You have $person{ 'days' } days          
You have $person{ 'money' } dollars left 
you have $person{ 'empty_pockets' } empty pockets 
EOL

    $person{ 'loan' }
        ? print "You still owe $person{ 'loan' } dollars \n"
            : print "";
    $person{ 'savings' }
        ?  print "You have $person{ 'savings' } in the bank\n" 
            : print "";
    $person{ 'gun'}
        ? print " You have a gun \n"
            : print "" ;
    print "What do you wanna do?                    \n";
    chomp($choice = <>);
    my $make_sure = 1; #good_input( 8, $choice);
    $make_sure ? 
            system("clear") &&
            $choice =~ /\d/
                ?   $choice == 1    ? move()        :
                    $choice == 2    ? buy()         :
                    $choice == 3    ? sell()        :
                    $choice == 4    ? your_drugs()  :
                    $choice == 5    ? go_to_bank()  :
                    $choice == 6    ? go_to_shark() :
                    $choice == 7    ? drug_display():
                    $choice == 8    ? exit_game()   :
                        print "you shouldn't see this \n"
                :   $choice =~ /m/i
                            ? move()        
                :   $choice =~ /b/i
                            ? buy()         
                :   $choice =~ /s/i
                            ? sell()        
                :   $choice =~ /l/i
                            ? your_drugs()
                :   $choice =~ /g/i
                            ? go_to_bank()
                :   $choice =~ /p/i
                            ? go_to_shark() 
                :   $choice =~ /v/i
                            ? drug_display()
                :   $choice =~ /q/i
                            ? exit_game()
                : print "you shouldn't see this \n"
    
    : print "error!!!!!\n";
}

#moves you to another part of the city
# uses changeday()
sub move{
    my $new_location;
    system("clear");
    print <<EOL;
*********************************
 1. The Bronx                    
 2. Queens                       
 3. Central Park                 
 4. Manhattan                    
 5. Coney Island                 
 6. Brooklyn                     
*********************************
You are currently in $person{ 'location' }
Where do you wanna go?
(choose 0 if you want to go back to the main menu)
EOL
    chomp($new_location = <>);
    if (good_input( 6, $new_location)){ 
            if (($new_location == 1) && 
                ($person{ 'location' } ne "The Bronx")){
                    $person{ 'location' } = "The Bronx";
                    change_day();
            }
            elsif (($new_location == 2) &&
                   ($person{ 'location' } ne "Queens")){
                        $person{ 'location' } = "Queens";                    
                        change_day(); 
            }       
            elsif (($new_location == 3) &&
                   ($person{ 'location' } ne "Central Park")){
                        $person{ 'location' } = "Central Park";
                        change_day();
            }
            elsif (($new_location == 4) &&
                   ($person{ 'location' } ne "Manhattan")){
                        $person{ 'location' } = "Manhattan";
                        change_day();
            }
            elsif (($new_location == 5) &&
                   ($person{ 'location' } ne "Coney Island")){
                        $person{ 'location' } = "Coney Island";
                        change_day();                              
            }         
            elsif (($new_location == 6) &&
                   ($person{ 'location' } ne "Brooklyn")){
                        $person{ 'location' } = "Brooklyn";
                        change_day();   
            }
            else { print "you are already there!!!\n"; }
    }    
    else { print "Thats not an option!!!\n";}
}

#makes sure you can return to the menu..
sub return{
    print "choose 0 to return to the menu\n";
}
# choose your poison
#assign values to %drug_hash
sub choose{
    my $drug_num;
    %drug_hash = (
        "price" => 0,
        "drug_poss" => 0,
        "number" => 0,
        "name" => "",
    );
    chomp($drug_num = <>);
    if (good_input(8, $drug_num)){    
        if ($drug_num == 1){
                $drug_hash{ 'price' } = $drugs{ 'acid' };
                $drug_hash{ 'drug_poss' } = $person{ 'acid' };
                $drug_hash{ 'number' } = 1;
                $drug_hash{ 'name' } = "acid";
        }
        elsif ($drug_num == 2){
                $drug_hash{ 'price' } = $drugs{ 'cocaine' };
                $drug_hash{ 'drug_poss' } = $person{ 'cocaine' };
                $drug_hash{ 'number' } = 2;
                $drug_hash{ 'name' } = "cocaine";
        }
        elsif ($drug_num == 3){
                $drug_hash{ 'price' } = $drugs{ 'ecstasy' };
                $drug_hash{ 'drug_poss' } = $person{ 'ecstasy' };
                $drug_hash{ 'number' } = 3;
                $drug_hash{ 'name' } = "ecstasy";
        }                       
        elsif ($drug_num == 4){
                $drug_hash{ 'price' } = $drugs{ 'pcp' };
                $drug_hash{ 'drug_poss' } = $person{'pcp' };
                $drug_hash{ 'number' } = 4;
                $drug_hash{ 'name' } = "pcp";
        }
        elsif ($drug_num == 5){
                $drug_hash{ 'price' } = $drugs{ 'heroin' };
                $drug_hash{ 'drug_poss' } = $person{ 'heroin' };
                $drug_hash{ 'number' } = 5;
                $drug_hash{ 'name' } = "heroin";
        }
        elsif ($drug_num == 6){
                $drug_hash{ 'price' } = $drugs{ 'weed' };
                $drug_hash{ 'drug_poss'} = $person{ 'weed' };
                $drug_hash{ 'number' } = 6;
                $drug_hash{ 'name' } = "weed";
        }
        elsif ($drug_num == 7){
                $drug_hash{ 'price' } = $drugs{ 'shrooms' };
                $drug_hash{ 'drug_poss' } = $person{ 'shrooms' };
                $drug_hash{ 'number' } = 7;
                $drug_hash{ 'name' } = "shrooms";
        }
        elsif ($drug_num == 8){
                $drug_hash{ 'price' } = $drugs{ 'speed' };
                $drug_hash{ 'drug_poss' } = $person{ 'speed' };
                $drug_hash{ 'number' } = 8;
                $drug_hash{ 'name' } = "speed";
        }
        elsif ($drug_num == 0){
                $drug_hash{ 'price' } = 0;
                $drug_hash{ 'drug_poss' } = 0;
                $drug_hash{ 'number' } = 0;
                $drug_hash{ 'name' } = "";
        }
        else { print "THATS NOT AN OPTION\n"; 
                $drug_hash{ 'price' } = 0;
                $drug_hash{ 'drug_poss' } = 0;
                $drug_hash{ 'number' } = 0;
                $drug_hash{ 'name' } = "";
        }
    } 
    
    $drug_hash{ 'number' }
        ? return %drug_hash
    : print "";
}
   
# buy your poison
sub buy {
    my $num_items; my $price;
    my $possible;
    drug_display();
    print <<EOL;
 you have $person{'money'} dollars left. 
 what drug do you want?
EOL
    choose();
    if ($drug_hash{ 'price' }){
        # make sure they can afford at least one
        if ($drug_hash{ 'price' } > $person{ 'money' }){
            print "whoooaaa look at that price!! you're not going to be able to afford that\n";
        }
        else {
            $possible = $person{ 'money' } / $drug_hash{ 'price' };
            $possible = floor($possible); 
            if ($possible > $person{'empty_pockets'}){
                $possible = $person{'empty_pockets'};
            }
            print <<EOL;
You can afford and have space for $possible units of $drug_hash{'name'}
how many do you want?
EOL
            chomp($num_items = <>);
            if ($num_items){
                # test to see they can afford that many
                # and if they have room
                if (($num_items * $drug_hash{ 'price' }) > ($person{'money'})) {
                    print "You cant afford that many!!!\n";
                }
                elsif (($num_items) > $person{ 'empty_pockets' }) {
                print "You don't have enough pockets to hold this many units\n";
                }
                else {
                    $person{ $drug_hash{ 'name' }} += $num_items;
                    $person{ 'money' } -= $num_items * $drug_hash{ 'price' };
                    $person{'empty_pockets'}-= $num_items;
                }
            }
            else { return ;}
        }
    }
}

#displays your drugs
sub your_drugs{
    print <<EOL;
*************************************************
  you have:                                      
         $person{ 'acid'} units of Acid          
         $person{ 'cocaine' } units of Cocaine   
         $person{ 'ecstasy' } units of Ecstasy  
         $person{ 'pcp' } units of PCP          
         $person{ 'heroin' } units of Heroin     
         $person{ 'weed' } units of Weed         
         $person{ 'shrooms' } units of Shrooms   
         $person{ 'speed' } units of Speed       
*************************************************
EOL
}

# sell your poison
sub sell {
    my $num_items; my $price; my $goodq; 
    my $decide; my $choice;
    your_drugs();
    drug_display();
    print "what do you want to sell? \n";
    choose();
    if ($drug_hash{ 'drug_poss' }){
        print <<EOL;
how many units do you want to sell?  
    1. all of it 
    2. just a few
EOL
        chomp($decide = <>);
        $goodq = good_input(2, $decide);
        if ($goodq){
            if ($decide == 1){$num_items = $drug_hash{'drug_poss'};}
            elsif ($decide == 2){
                print "How many?            \n";
                chomp($num_items = <>);
            }
        }
        else { print "That's not a choice!!!\n";}
        if ($num_items){
            $goodq = good_input($drug_hash{'drug_poss'}, $num_items);
            if ($goodq){
                    $person{ $drug_hash{'name'} } -= $num_items;
                    $person{ 'money' } += $num_items * $drug_hash{ 'price' };
                    $person{ 'empty_pockets'} += $num_items;
            }
            elsif($num_items == 0){return ;}
            else { print "you do not have that many units of 
                        $drug_hash{ 'drug_poss' } !!!\n"; }
            }
        }
    elsif($drug_hash{ 'number'} == 0) {return ;}
    else { print "YOU DONT HAVE ANY OF THIS DRUG!!\n"; }
}

# changes to the next day
# increases bank roll and debt
sub change_day{
    my $bank_int = 0;
    my $loan_int = 0;
    if ($person{ 'days' } > 0){
        $bank_int = 6;
        $loan_int = 12;
        $person{ 'days'} -= 1;
        random_drug_price();
        random_other();
        big_drug_change();
        $person{ 'savings' } *= (($bank_int / 100) + 1);
        $person{ 'savings' } = ceil($person{ 'savings' });
        $person{ 'loan' } *= (($loan_int / 100) + 1);
        $person{ 'loan' } = floor($person{ 'loan' });
        if ($person{ 'days' } == 0){
            print "It's your last day, better sell your stuff\n";
        }
    }
    else { endgame(); }
}   
 # name          price range    diff    max/min   
 # acid:         1000-4500      3500    3.5
 # cocaine:      15000-29810    14810   1.987
 # Ecstasy:      15 - 60        45      4
 # PCP:          1084 - 3406    2322    3.142
 # Heroin:       6963-12183     5220    1.749
 # Weed :        314-723        409     2.3
 # Shrooms :     717-1275       558     1.778
 # Speed :       95-220         150     2.315
 # changes the price of all the drugs into somwhere in the ranges given
 # above
sub random_drug_price{
    $drugs{ 'acid' } = int(rand(3500)) + 1000;
    $drugs{ 'cocaine' } = int(rand(14810)) + 15000 ;
    $drugs{ 'ecstasy' } = int(rand(45)) + 15;
    $drugs{ 'pcp' } = int(rand(2322)) + 1084 ;
    $drugs{ 'heroin'} = int(rand(5220)) + 6963;
    $drugs{ 'weed' } = int(rand(409)) + 314;
    $drugs{ 'shrooms' } = int(rand(558)) + 717;
    $drugs{ 'speed' } = int(rand(150)) + 70;
}

#chooses 
    # whether a cop and his deputies show up 
    # whether you get mugged
    # offer to buy a gun
    # find drugs on dead guy
    # nothing happens
# you have a 20% chance of each happening

sub random_other{
    my $random_action;
    $random_action = int(rand(4)) + 1;
    if (($random_action  == 1) && $cop){ cop_shows(); } 
    elsif($random_action == 2){ mugged();}
    elsif(($random_action == 3) && !($person{'gun'}))
            {  offer_gun();}
    elsif($random_action == 4){ find_drugs();}
}



# a cop shows up 
sub cop_shows{
    my $times = 0; my $keep_going = 1;
    my $choice = 0; my $result = 0;
    while ($keep_going){
        system("clear");
        print "*****************************************\n";
        if ($deputy){
            if ($times){ 
                print " Officer Hardass and his $deputy  deputies are still chasing you    \n";}
            else { print " Officer Hardass and his $deputy deputies are chasing you          \n";}
        }
        else{
            if ($times){ print " Officer Hardass is still chasing you    \n";}
            else { print " Officer Hardass is chasing you          \n";}
        }
        print <<EOL;
Do you: 
    1) Run
    2) Stay   
EOL
        if ($total_drugs){
            print "    3) Dump Your Drugs              \n";
        }
        if ($person{"gun"} && $total_drugs){
            print "    4) Shoot Them                   \n";
        }
        elsif ($person{"gun"}){
            print "    3) Shoot Them                   \n";
        }
        print "***************************************************\n";
        chomp ($choice = <>);
        if ($choice =~ /\d/ ) {
            if ($choice == 1) {
                # you have a 1/5 chance of getting caught
                #you have a 1/5 chance of escaping 
                # you have a 3/5 of them still chasing you
                $result = int(rand(4)) + 1;
                if ($result == 1 ) {
                    system("clear");
                    print <<EOL;
***************************************************
 Officer Hardass shoots at you and damages you. 
 you have been caught and receive a day in jail
 as punishment
***************************************************
EOL
                    display_pause();
                    dump_drugs();
                    change_day();
                    $keep_going = 0;
                }
                elsif ($result == 2) {
                    system("clear");
                    print <<EOL;
***************************************************
 You loss them in the alleys 
***************************************************
EOL
                    display_pause();
                    $keep_going = 0;
                }
                else {
                    system("clear");
                    print <<EOL;
***************************************************
They're still chasing you!!!
***************************************************
EOL
                    display_pause();
                }
            }
            elsif ($choice == 2) {
                # tests to see if you have drugs on you
                if ($total_drugs){
                    system("clear");
                    print <<EOL;
*************************************************** 
 Officer Hardass found drugs on you. 
 You earned yourself a night in jail.
***************************************************
EOL
                    display_pause();
                    dump_drugs();   
                    change_day();
                }
                else {
                    system("clear");
                    print <<EOL;
***************************************************
 Officer Hardass patted you down        
 but he didnt find anything                               
***************************************************
EOL
                    display_pause();
                }            
                $keep_going = 0;
            } 
            elsif(($choice == 3) && $total_drugs){dump_drugs();}
            elsif(($choice == 4) || ($choice == 3)){
                my $dead; 
                $dead = shoot_cops();
                if ($dead){$keep_going = 0;}
            }
            $times = 3;
        }
        else{
            print "Use the number of your choice\n";
        }
    }
}

sub offer_gun{
    my $choice;
    system("clear");
    print <<EOL;
***************************************************
 Do you want to buy a gun for \$ 400? 
    ('y' for yes, 'n' for no)
***************************************************
EOL
    chomp($choice = <>);
    if (($choice eq 'y')||
        ($choice eq 'Y')){
        $person{'money'} -= 400;
        $person{'gun'} += 1;
        $person{'gun'} 
            ? print " You now have a gun!!! \n"
            : return 0;
    }
    
    display_pause();
    
}

#get into a gun fight with cops
# you have to go through each deputy 
#   before you can kill officer hardass
# you have a 2/3 shot of killing a deputy
#   and a 1/3 shot of killing officer hardass
# returns a "1" if you kill all of them.. 

sub shoot_cops{
    my $result; my $keepgoing = 1;
    while ($keepgoing){
        $result = int(rand(2)) + 1;
        if ($deputy){
            if ($result <= 2){
                --$deputy; 
                print <<EOL;
***************************************************
 GOOD SHOT!!!!
 YOU KILLED ONE OF THE DEPUTIES!!!!!
***************************************************
EOL
            display_pause();
            }
            else{
                print <<EOL;
***************************************************
 YOU MISSED!!!!!
***************************************************
EOL
            display_pause();
            }
            return 0;
        }
        else{
            if ($result == 1){
                $cop = 0;
                print <<EOL;
***************************************************
 GOOD SHOT!!!!
 YOU KILLED OFFICER HARDASS!!!!
 YOU DONT HAVE TO WORRY ABOUT THE COPS ANYMORE!!!
***************************************************
EOL
                display_pause();
                return 1; 
            }
            else{
                print <<EOL;
***************************************************
 YOU MISSED!!!!!
***************************************************
EOL
                display_pause();
                return 0;
            }
        }
    }
    system("clear");
}


# big drug change
# 1. Acid:         down 100 - 350 
# 2. cocaine:      up 95000 - 112000
# 3. Ecstasy:      down 1-5
# 4. PCP:          up 9000 - 10000   
# 5. Heroin:       up 20000 - 30000
# 6. Weed :        down 75 - 50      
# 7. Shrooms :     down 100 - 300 
# 8. Speed         up 1000 - 1500
#
# each drug has a 10% chance of getting changed to be in 
# the corresponding range,
# there is a 20% chance that no change will occur
# there is a 10% chance that the funkshun will call itself again
sub big_drug_change{
    my $chooser = int(rand(9)) + 1;
    if (($chooser <= 8) &&
        ($chooser >= 1)){
        system('clear');
        if (($chooser == 1)&&
            ($drugs{ 'acid' } > 350)){ 
            print " Somebody flooded the markets with Cheap Home-made Acid!!!!\n";
            $drugs{ 'acid' } = int(rand(250)) + 100;
        }
        elsif(($chooser == 2)&&
              ($drugs{'cocaine'} < 95000 )){
            print " There was a coke bust, Prices are sky high!!!! \n";
            $drugs{ 'cocaine' } =int(rand(17000)) + 95000;
        }   
        elsif(($chooser == 3)&&
              ($drugs{ 'ecstasy' } > 5)){
                print "A pharmacy was raided, and the result is cheap ecstacy\n";
                $drugs{ 'ecstasy' } = int(rand(4)) + 1;
        }
        elsif(($chooser == 4)&&
              ($drugs{ 'pcp' } < 9000)){
                print "There was a big PCP bust, prices are outrageous\n";
                $drugs{ 'pcp'} = int(rand(1000)) + 9000;
        }
        elsif(($chooser == 5) &&
              ($drugs{ 'heroin'} < 20000)){
                print "Big Heroin bust, Prices are sky high\n";
                $drugs{ 'heroin'} = int(rand(10000)) + 20000;
        }   
        elsif(($chooser == 6)&&
              ($drugs{ 'weed' } > 75)){
                print "Weed Prices have bottomed out\n";
                $drugs{ 'weed' } = int(rand(25)) + 50;
        }
        elsif(($chooser == 7)&&
              ($drugs{ 'shrooms' } > 300)){
                print "Its shroom season, prices are low because of amount of good crops \n";
                $drugs{ 'shrooms' } = int(rand(200)) + 100;
        }
        elsif(($chooser == 8) &&
              ($drugs{ 'speed' } < 1000)){
                print "Addicts are buying speed at outrageous prices\n";
                $drugs{ 'speed'} = int(rand(500)) + 1000;
        }
        else {print "you shouldn't be seeing this\n";}
    }
    my $chooser2 = int(rand(9)) + 1;
    if ($chooser2 == 3) { big_drug_change();}
    
}


# is called when you get mugged 
# a mugger takes away 1/3 of the money you have on you
sub mugged{
    print "YOU'VE JUST BEEN MUGGED!!!!!!!\n";
    $person {'money'} = floor($person{ 'money' } *= (2/3));
    display_pause();
}


# gives you access to the bank
# able to withdraw/deposit money at any time
sub go_to_bank{
    my $status_num = 0; my $want_to_do;
    my $money; my $how_much;
    print <<EOL;
*****************************************
 1) Deposit                            
 2) Withdraw       
 0) go back to the main menu
*****************************************
You have $person{'money'} dollars on you
You have $person{'savings'} dollars in the bank
What do you want to do?  
EOL
    chomp($status_num = <STDIN>);
    if (good_input( 2, $status_num)){
       if ($status_num == 1){
            $want_to_do = "deposit"; 
            print "You have $person{'money'} dollars on you\n";
       }
       elsif ($status_num == 2){ 
            $want_to_do = "withdraw"; 
            print "You have $person{'savings'} dollars in the bank\n";
       }
       else {print "That's not an option!!!/n";}
       print <<EOL;
How much money do you want to $want_to_do ?
    1. all of it
    2. just some
EOL
       chomp ($how_much = <>);
       if ($how_much == 1){ 
            if ($status_num == 2){
            $money = $person{'savings'};
            }
            elsif ($status_num == 1){
                $money = $person{ 'money'};
            }
       }
       elsif($how_much == 2){
            print ' $ ';
            chomp($money = <>);
       }
       else { print "That was not a choice\n";}
       if ($status_num == 2){
           if ($money <= $person{ 'savings' }){
               $person{ 'savings' } -= $money;
               $person{ 'money' } += $money;
           }
           else { print "YOU DONT HAVE THAT MUCH IN THE BANK \n"; }
       }
       elsif($status_num == 1) {
               if ($money <= $person{ 'money' }){
                   $person{ 'money' } -= $money;
                   $person{ 'savings' } += $money;
               }
               else { print "YOU DONT HAVE THAT MUCH MONEY ON YOU\n"; }   
       }
    }
    elsif ($status_num =~ /\d+/) {print "Thats not a choice!!\n";}
    else { print "Thats not an option!!!"; }        
}

# gives you access to the loan shark
# whether to pay your debt or borrow more
sub go_to_shark{
    my $choice1; my $choice2; my $amount;
    print <<EOL;
*******************************
    1) borrow more            
    2) pay your debt  
    3) go back to main menu
*******************************
EOL
    chomp ($choice1 = <>);
    if ($choice1 == 1){
        print "How much do you want to borrow?  \n";
        print ' $ ';
        chomp ($amount = <>);
        $person{ 'loan' } += $amount;
        $person{ 'money' } += $amount;
    }
    elsif ($choice1 == 2){
            print <<EOL;
*************************************
 You owe \$ $person{ 'loan'}
 You have \$ $person{'money'}
EOL
        if ($person{'loan'} <= $person{'money'}){
            print <<EOL;
 How much do you want to pay?
    1) all of it, $person{ 'loan'}
    2) just part of it
*************************************
EOL
            chomp ($choice2 = <>);
            if ($choice2 == 1){
                if ($person{ 'loan' } <= $person{ 'money'}){
                    $person{ 'money'} -= $person{ 'loan'};
                    $person{ 'loan' } = 0;
                }
                else{ print "you dont have enough money \n"; }
            }
            elsif ($choice2 == 2){
                    print "how much?\n";
                    chomp ($amount = <>);
                    if ($amount <= $person{'money'}){
                        $person{ 'money' } -= $amount;
                        $person{ 'loan' } -= $amount;
                    }
                    else { print "You dont have enough money\n";}
            }
            else { print "thats not a choice \n"; }
        }
        else {
             print "how much?\n";
             chomp ($amount = <>);
             if ($amount <= $person{'money'}){
                $person{ 'money' } -= $amount;
                $person{ 'loan' } -= $amount;
            }
            else { print "You dont have enough money\n"; }
        }
    }
    elsif ($choice1 == 0){ return ;}
    else { print "thats not a choice\n"; }
}

sub exit_game{
    my $sure;
    print <<EOL; 
Are you sure you want to quit?
    (y for yes, n for no)
EOL
    chomp($sure = <>);
    if ($sure =~ /y/i){
            # try(system('clear'));
            # catch(system('cls'));
            exit;
    }
}

#drug called to show that you found drugs on the subway
sub find_drugs{
    my $choice = int(rand(7)) + 1;
    my $amount = int(rand(7)) + 1;
    my $choice_drug;
    if ($person{'empty_pockets'} < $amount){
        $amount = $person{'empty_pockets'};
    }
    $choice == 1 ? $choice_drug = "acid" 
        : $choice == 2 ? $choice_drug = "cocaine"
        : $choice == 3 ? $choice_drug = "ecstasy"
        : $choice == 4 ? $choice_drug = "pcp"
        : $choice == 5 ? $choice_drug = "heroin"
        : $choice == 6 ? $choice_drug = "weed" 
        : $choice == 7 ? $choice_drug = "shrooms"
        : $choice == 8 ? $choice_drug = "speed" 
        : print " error on find_drugs !! ";
    print <<EOL;
You found $amount units of $choice_drug
on some dead guy on the subway         
EOL
    $person{$choice_drug} += $amount;
    $person{'empty_pockets'} -= $amount;
    display_pause();
}   
sub endgame {
    $total_money = ($person{'savings'} + 
                    $person{'money'}) -
                    $person{'loan'}; 
    $total_money >= 0
        ? print "you ended up with $total_money dollars\n" 
    : print "You owe \$ $total_money to the Loan Shark\n The LoanShark and his thugs broke your legs\n";
    high_score();
    exit;
}

sub high_score{
    my $place = 0; my $oname; my $opoints;
    my @lines = io($score_file)->slurp ;
    my $inline = $person{'name'}.":".$total_money."\n";
    print "$inline\n";
    if(!($lines[$place])){
        $inline >> io($score_file);
        print "Your The First Person to Get This Far!!!\n";
        return 0;
    }
    foreach(@lines){
        ($oname, $opoints) = split /:/, $_;
        if(!($lines[$place])){
            $inline >> io($score_file);
            print "place = $place\n";
            return 0;
        }
        elsif (($opoints > $total_money ) ||
               ($opoints == $total_money)){
            print "not good enough for $place \n";
            $place++;
            next;
        }
        else{
            print "you got place: $place \n";
            while ($place < 10){
                if(!($lines[$place])){
                    $inline .= "\n";
                    $inline > io($score_file);
                    return 0;
                }
                else {
                    $inline .= $lines[$place]."";
                    $place++;
                }
            }
            #$inline > io($score_file);
            return 0;
        }
    }
    if(!($lines[$place]) and
        ($place <10)){
        $inline >> io($score_file);
        print "place = $place\n";
        return 0;
    }
}

#sub place_score{}
#sub read_score{u}

#$score_file
#/\w : \d+/

