From: www.itworld.com
June 6, 2001 —
Last week, we looked at some basic object concepts and how to use an
object in a program. This week, we'll begin building an object-oriented
module (a class) from scratch. Rather than create some abstract data
structure from a computer science course though, we'll build something
simulating a real world object: a slot machine.
Before building our slot machine, we must ask ourselves a few questions
about our interaction with such a machine (hypothetically speaking of
course). A slot machine has a relatively simple interface. In a
nutshell, you insert money, pull the lever, and sometimes you even get
some money back out. Modern machines haven't changed the interface
much; you can insert some quantity of money (giving you credits) and
then bet various (usually small) amounts before each pull. This allows
you to put all your money in at once and then lose it without having to
continue feeding it coins before every pull. Our slot machine will use
the same basic idea.
To write a program that uses our slot machine, we will want to be able
to do the following things: 1) Initialize the slot machine with some
amount of credits; 2) Display how many credits we have left; 3) Display
our current bet and change the current bet; 4) Pull the lever to spin
the "wheels". We will probably also want the ability to display a
payout table so that players know what results they should wish for.
Let's first build a small interactive test program to play a slot game
using our imaginary object with 100 credits to start with:
#!/usr/bin/perl -w
use strict;
use Slot;
my $slot = Slot->new(100);
while(1){
my $credits = $slot->credits();
my $curr_bet = $slot->bet();
last if $credits < 1;
print "You have $credits credits remaining: max bet is 3\n";
print "Enter your bet ('q' to quit) [$curr_bet]: ";
chomp(my $bet =
last if $bet eq 'q';
$bet ||= $curr_bet;
$slot->spin($bet);
print $slot->results();
}
print "Game Over\n";
print "You have ", $slot->credits(), " remaining\n";
print "Thank you for playing\n";
__END__
Obviously we can't run this program until we build our slot object, but
now we not only have a description of the interface to our object, but
a test program as well. To begin with, we load our Slot.pm module and
then create a new Slot object via the Slot->(100) class method (we
imagine passing in the number of credits to play with, in this case
100); then, we start an infinite loop (well, infinite only if we are
incredibly lucky and keep winning more than we lose). In the loop, we
grab the current credits remaining and the current betting value
(default will be one credit). If we have less than one credit, then the
game is over.
The next little snippet merely informs the user of how many credits
they have, the maximum bet, and prompts them to enter a bet. We grab
the bet (if any) and exit the loop if it is 'q', we then set the bet to
the current betting amount if they entered nothing (we aren't bothering
with validating user input at this point). We then spin() the slots
using the slot() method and print the results of the spin with the
results() method. If we exit the loop, then we display a message
informing the user of their winnings (if any) and the game is over.
That sums up the interface to our slot machine object. In the next few
installments, we will begin building the slot machine itself.
Next Week: Constructing an object (The Constructor).
ITworld