Last week we looked some basic concepts of objects 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, we’ll build something to simulate a real world object: a slot machine.
Before we build our slot machine we need to ask ourselves a few questions about how we interact with such a machine (hypothetically speaking of course). A slot machine has a pretty simple interface: in a nutshell, you put money in and you pull the lever, and sometimes you even get some money back out. Modern machines haven’t changed the interface much — now you can put some quantity of money in (giving you credits) and then bet various (usually small) amounts. This allows you to put all your money in at once and then lose it without having to keep putting coins in with every pull. That’s the basic idea we will use with our slot machine.
So, if we want 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) Be able to find out how many credits we have left; 3) Be able to determine our current bet, and change our current bet; 4) Pull the lever to spin the "wheels". We will probably also want to be able to display a pay-out 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 = <STDIN>);
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 1 credit, 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 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.
*****



