print "What Goal In Cash Do You Want To Set? :";
chomp ($goal=##
sub query_user {
print @_ if @_;
chomp (my $answer = ##
sub notify_user {
return print @_;
}
####
sub query_user {
notify_user(@_) if @_;
chomp (my $answer = ##
my (%ticker, %portfolio);
my @stock_names = ('Derpco', 'Tetrabytz', 'Mojo Inc', 'Freke', 'Mega');
@portfolio{@stock_names} = (0) x @stock_names;
@ticker{@stock_names} =
map { price => generate_random(10, 100),
volitility => int(rand(7)) * 5 + 5 }, @stock_names;
####
@stocks = (0,0,0,0,0);
$derpco=genRand(10,100);
$tetrabytz=genRand(10,100);
$mojoinc=genRand(10,100);
$freke=genRand(10,100);
$mega=genRand(10,100);
$cash=genRand(500,1000);
####
#!/usr/bin/perl -w
use strict;
my($dif,$randNumber,$manip,$whichstock,$howmany,$goal,$date,$derpco,$tetrabytz,$mojoinc,$freke,$mega,$cash,@stocks,$buy,$sell,$buynum,$sellnum,$trans);
@stocks = (0,0,0,0,0);
sub genRand {
my $startNumber = shift();
my $endNumber = shift();
my $randNumber = int($startNumber + rand() * ($endNumber - $startNumber));
return $randNumber;
};
print "What Goal In Cash Do You Want To Set? :";
chomp ($goal=##
#!/usr/bin/perl
use strict;
use diagnostics;
my (%ticker, %portfolio);
my @stock_names = ('Derpco', 'Tetrabytz', 'Mojo inc', 'Freke', 'Mega');
@portfolio{@stock_names} = (0) x @stock_names;
@ticker{@stock_names} =
map { price => generate_random(10, 100),
volitility => int(rand(7)) * 5 + 5 }, @stock_names;
my $goal = query_user( qq|\nWhat Goal In Cash Do You Want To Set? :| );
notify_user( qq|Thanks\n\n| );
my $date = 0;
my $cash = generate_random(500, 1000);
sub generate_random {
my ($start, $end) = @_;
return int( $start + rand($end - $start + 1) );
}
sub query_user {
notify_user(@_) if @_;
chomp (my $answer = ##
unless ( defined $end ) {
$end = $start;
$start = 0 - $end;
}
####
sub generate_random {
my ($start, $end) = @_;
$end = 0 - ($start = 0 - $start) unless defined $end;
return int( $start + rand($end - $start + 1) );
}
####
$manip=genRand(0,50);
$derpco=$derpco+genRand(-10,10);
$tetrabytz=$tetrabytz+genRand(-40,40);
$mojoinc=$mojoinc+genRand(-20,20);
$freke=$freke+genRand(-25,25);
$mega=$mega+genRand(-35,35);
if ($derpco < 1){$derpco = 0; $stocks[0]=0; };
if ($tetrabytz < 1){$tetrabytz = 0; $stocks[1]=0;};
if ($mojoinc < 1){$mojoinc = 0; $stocks[2]=0;};
if ($freke < 1){$freke = 0; $stocks[3]=0;};
if ($mega < 1){$mega = 0; $stocks[4]=0;};
####
# update prices
foreach my $stock ( @ticker{@stock_names} ) {
$$stock{price} += generate_random($$stock{volitility});
$$stock{price} = 0 if $$stock{price} < 1;
}
# liquidate holdings if price hits 0
foreach my $stock ( @stock_names ) {
$portfolio{$stock} = 0 if $ticker{$stock}{price} == 0
}
####
notify_user (
qq|\n\nYour Stock Report for January $date\n\n|,
qq|Holdings\tValue\tCompany\n\n|);
notify_user( qq|\t$portfolio{$_}\t\$$ticker{$_}{price}\t$_\n| )
for @stock_names;
my $transaction = query_user(
qq|\nYou have \$$cash in cash and \$|,
portfolio_value( \%portfolio, \%ticker, ),
qq| in stocks.\n|,
qq|Would you like to (B)uy or (S)ell or are you (D)one? | );
####
sub portfolio_value {
my ($portfolio, $ticker) = @_;
my $total_value = 0;
$total_value += $$portfolio{$_} * $$ticker{$_}{price}
for keys %$portfolio;
return $total_value;
}
####
until ( $cash > $goal or $date == 31 ) {
# update prices
foreach my $stock ( @ticker{@stock_names} ) {
$$stock{price} += generate_random($$stock{volitility});
$$stock{price} = 0 if $$stock{price} < 1;
}
# liquidate holdings if price hits 0
foreach my $stock ( @stock_names ) {
$portfolio{$stock} = 0 if $ticker{$stock}{price} == 0
}
$date++;
print qq|Last Day!! better sell your stock| if $date == 30;
while (1) {
notify_user(daily_report(\%portfolio, \%ticker, $cash, $date));
my $transaction =
query_user( q|Would you like to (B)uy or (S)ell |,
q|or are you (D)one? | );
if ($transaction =~/^s/i) {
$cash += sell(\%portfolio, \%ticker);
} elsif ($transaction =~/^b/i) {
$cash = buy(\%portfolio, \%ticker, $cash);
} elsif ( $transaction =~ /^d/i ) {
last;
} elsif ( $transaction =~ /^q/i ) {
exit;
}
}
}
####
sub sell {
my ($portfolio, $ticker) = @_;
my $stock_to_sell = query_user(q|What Stock would you like to Sell? |);
my $cash = 0;
foreach my $stock ( keys %$portfolio ) {
if ( $stock =~ /$stock_to_sell/i ) {;
if ( $$ticker{$stock}{price} == 0 ) {
notify_user(
qq|$stock has filed for protection from creditors|,
q| - Your holdings were liquidated.| );
} else {
my $shares_to_sell = query_user(
qq|\n\nYou have $portfolio{$stock} shares in $stock\n|,
q|Sell How many? :| );
if ( $shares_to_sell > $$portfolio{$stock} ) {
notify_user( qq|Sorry, your account is not set |,
qq|up for short selling.\n| );
} else {
$$portfolio{$stock} -= $shares_to_sell;
$cash = $shares_to_sell * $$ticker{$stock}{price};
}
}
last;
}
}
return $cash;
}
####
buy sub routine fixes a bug in the original game which allowed the player to purchase a few million shares when the price reached zero.
sub buy {
my ($portfolio, $ticker, $cash) = @_;
my $stock_to_buy = query_user( q|What Stock would you like to buy? |);
foreach my $stock ( keys %$portfolio ) {
if ( $stock =~/$stock_to_buy/i ) {
if ( $$ticker{$stock}{price} == 0 ) {
notify_user(
qq|Sorry, trading has been suspended in $stock.| );
} else {
my $shares_to_buy = query_user(
qq|\n\nYou have $portfolio{$stock} shares in $stock and|,
qq|\$$cash\nBuy How many |,
qq|(max int( $cash / $$ticker{$stock}{price} ), q|) :| );
if ( $cash < $$ticker{$stock}{price} * $shares_to_buy ) {
notify_user( qq|Sorry, this isn't a margin account.\n| );
} else {
$$portfolio{$stock} += $shares_to_buy;
$cash -= $shares_to_buy * $$ticker{$stock}{price};
}
}
last;
}
}
return $cash;
}
####
if ( $cash > $goal ) {
my $dif = $cash - $goal;
notify_user( qq|Yay! you won, you beat your goal by \$$dif\n| );
}
if ($cash < $goal) {
my $dif = $goal - $cash;
notify_user( qq|Boo! you lost, you missed your goal by \$$dif\n| );
}
####
#!/usr/bin/perl
#require v5.6.1;
use strict;
use diagnostics;
#use warnings;
#use Data::Dumper;
#use CGI qw/:standard/;
#use CGI::Carp 'fatalsToBrowser';
my (%ticker, %portfolio);
my @stock_names = ('Derpco', 'Tetrabytz', 'Mojo inc', 'Freke', 'Mega');
@portfolio{@stock_names} = (0) x @stock_names;
@ticker{@stock_names} =
map { price => generate_random(10, 100),
volitility => int(rand(7)) * 5 + 5 }, @stock_names;
my $goal = query_user( qq|\nWhat Goal In Cash Do You Want To Set? :| );
notify_user( qq|Thanks\n\n| );
my $date = 0;
my $cash = generate_random(500, 1000);
until ( $cash > $goal or $date == 31 ) {
# update prices
foreach my $stock ( @ticker{@stock_names} ) {
$$stock{price} += generate_random($$stock{volitility});
$$stock{price} = 0 if $$stock{price} < 1;
}
# liquidate holdings if price hits 0
foreach my $stock ( @stock_names ) {
$portfolio{$stock} = 0 if $ticker{$stock}{price} == 0
}
$date++;
print qq|Last Day!! better sell your stock| if $date == 30;
while (1) {
notify_user(daily_report(\%portfolio, \%ticker, $cash, $date));
my $transaction =
query_user( qq|Would you like to (B)uy or (S)ell or are you (D)one? | );
if ($transaction =~/^s/i) {
$cash += sell(\%portfolio, \%ticker);
} elsif ($transaction =~/^b/i) {
$cash = buy(\%portfolio, \%ticker, $cash);
} elsif ( $transaction =~ /^d/i ) {
last;
} elsif ( $transaction =~ /^q/i ) {
exit;
}
}
}
if ( $cash > $goal ) {
my $dif = $cash - $goal;
notify_user( qq|Yay! you won, you beat your goal by \$$dif\n| );
}
if ($cash < $goal) {
my $dif = $goal - $cash;
notify_user( qq|Boo! you lost, you missed your goal by \$$dif\n| );
}
sub daily_report {
my ($portfolio, $ticker, $cash, $date) = @_;
my @report;
push @report,
qq|\n\nYour Stock Report for January $date\n\n|,
qq| Holdings Value\tCompany\n\n|;
push @report,
qq|\t$$portfolio{$_}\t \$$$ticker{$_}{price}\t$_\n|
for keys %$portfolio;
push @report,
qq|\nYou have \$$cash in cash and \$|,
portfolio_value( @_ ),
qq| in stocks.\n|;
return join '', @report;
}
sub generate_random {
my ($start, $end) = @_;
$end = 0 - ($start = 0 - $start) unless defined $end;
return int( $start + rand($end - $start + 1) );
}
sub portfolio_value {
my ($portfolio, $ticker) = @_;
my $total_value = 0;
$total_value += $$portfolio{$_} * $$ticker{$_}{price} for keys %$portfolio;
return $total_value;
}
sub sell {
my ($portfolio, $ticker) = @_;
my $cash = 0;
my $stock_to_sell = query_user(q|What Stock would you like to Sell? |);
foreach my $stock ( keys %$portfolio ) {
if ( $stock =~ /$stock_to_sell/i ) {;
if ( $$ticker{$stock}{price} == 0 ) {
notify_user( qq|$stock has filed for protection from creditors|,
q| - Your holdings were liquidated.| );
} else {
my $shares_to_sell = query_user(
qq|\n\nYou have $portfolio{$stock} shares in $stock\n|,
q|Sell How many? :| );
if ( $shares_to_sell > $$portfolio{$stock} ) {
notify_user( qq|Sorry, your account is not set up for short sales.\n| );
} else {
$$portfolio{$stock} -= $shares_to_sell;
$cash = $shares_to_sell * $$ticker{$stock}{price};
}
}
last;
}
}
return $cash;
}
sub buy {
my ($portfolio, $ticker, $cash) = @_;
my $stock_to_buy = query_user( q|What Stock would you like to buy? |);
foreach my $stock ( keys %$portfolio ) {
if ( $stock =~/$stock_to_buy/i ) {
if ( $$ticker{$stock}{price} == 0 ) {
notify_user( qq|Sorry, trading has been suspended in $stock.| );
} else {
my $shares_to_buy = query_user(
qq|\n\nYou have $portfolio{$stock} shares in $stock and \$$cash\n|,
q|Buy How many (max |, int( $cash / $$ticker{$stock}{price} ), q|) :| );
if ( $cash < $$ticker{$stock}{price} * $shares_to_buy ) {
notify_user( qq|Sorry, this isn't a margin account.\n| );
} else {
$$portfolio{$stock} += $shares_to_buy;
$cash -= $shares_to_buy * $$ticker{$stock}{price};
}
}
last;
}
}
return $cash;
}
sub query_user {
notify_user(@_) if @_;
chomp (my $answer = );
return $answer;
}
sub notify_user {
return print @_;
}
__END__