Important alert: (current site time 7/15/2013 9:02:10 PM EDT)
 

VB icon

HI HO Cherry-O!

Email
Submitted on: 1/31/2002 4:17:47 PM
By: wv-perlguy 
Level: Advanced
User Rating: Unrated
Compatibility: 5.0 (all versions)
Views: 7959
author picture
(About the author)
 
     Children's game of Hi-Ho Cherry-0!
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
=**************************************
= for :HI HO Cherry-O!
=**************************************
Co-authored with Mike Clemmens. Thanks Mike!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: HI HO Cherry-O!
= Description:Children's game of Hi-Ho Cherry-0!
= By: wv-perlguy
=
= Inputs:Colors. Type in the colors you want to use
=
= Returns:Plays out the game (since there is no strategy), it just goes...
=
= Assumes:Enter the colors on the command line with the program. Example: HIHO.pl RED GREEN BLUE
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=277&lngWId=6=for details.=**************************************

#!/usr/local/bin/perl
use strict;
# give the command line args a slightly nicer name
my @players = @ARGV;
if (@players <= 1) { # array used in scalar context
 print "Not enough players: round up some friends and try again.\n";
 exit(0);
}
# This holds the number of cherries, per player. The key of the hash
# is the player's color (contained in the @players array.)
my %tree;
# Initialize each player's tree with 10 cherries.
map { $tree{$_} = 10; } @players;
# Keep track of the current player, starting with a random one
my $current_player = int(rand @players);
# The spinner: an array of anonymous arrays!
# Format is: (number of cherries to remove, spinner space name, clever quip)
my @spinner = ([1, "one cherry", "Slow and steady wins the race."],
[2, "two cherries", "One for you and one for me!"],
[3, "three cherries", "Just one short of a grand slam."],
[4, "four cherries", "That was certainly lucky!"],
[-2, "bird", "Dratted pigeons!"],
[-2, "dog", "Isn't there a leash law in this town?"],
[-10, "spilled bucket", "Bad luck, perhaps next time."]);
# set to a nonzero value when the game is done
my $game_over = 0;
# must be declared, thanks to 'use strict'
my ($cherries, $spinner_name, $clever_quip);
while (! $game_over) {
print $players[$current_player] ," spins ";
($cherries, $spinner_name, $clever_quip) = @{$spinner[int(rand @spinner)]};
print $spinner_name, ". ", $clever_quip, " ";
$tree{$players[$current_player]} -= $cherries;
# boundary checking: can't have more than 10 cherries in the tree
if ($tree{$players[$current_player]} > 10) {
$tree{$players[$current_player]} = 10;
}
if ($game_over = ($tree{$players[$current_player]} <= 0)) {
print $players[$current_player]," has won!\n";
} else {
print "Cherries left on the tree: ", $tree{$players[$current_player]},"\n";
$current_player = ($current_player + 1) % @players;
}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.