Siaris
Simple Things
Syndicate: full/short
Siaris
Categories
General0
News2
Programming2
LanguageBits0
Perl50
Ruby10
VersionControl1
Misc1
Article Calendar
<= May, 2008
S M T W T F S
123
45678910
11121314151617
18192021222324
25262728293031
Search this blog

Key links
External Blogs
Brought to you by ...
Ruby
1and1.com

Class data and more methods (soot part V)

Andrew L. Johnson (First published by ItWorld.com 2001-06-28)

At this point in the object tutorial we have built a Slot class that builds a simple object with a few attributes and provides a couple of accessor routines for those attributes the user might need. But we don’t have working slot machine by any means.

Our slot machine will be a variation on the lucky-7 variety — it will have 3 spinning wheels each containing the following digits: 0,1,2,3,7. Getting three 7’s will pay the most, followed by three 3’s, three 2’s, three 1’s, three of any 1,2 or 3, and three 0’s.

The payoff scheme will be as follows (I don’t have much experience slot machines and odds, so I’m making this up as I go along — feel free to change things in your machine) [$bet indicates amount being bet]:

    777   pays  $bet * 500
    333   pays  $bet * 80
    222   pays  $bet * 20
    111   pays  $bet * 5
    any   pays  $bet * 2
    000   pays  $bet * 1

Here, "any" refers to spins with all wheels showing any of 1,2, or 3 (for example: 123, 332, 231). We can set this up as a hash of payoffs where the keys will be the spin result and the values will be anonymous subroutines that return the winnings:

    my %payoff = (
        "777"  => sub {$_[0] * 500},
        "333"  => sub {$_[0] * 80},
        "222"  => sub {$_[0] * 20},
        "111"  => sub {$_[0] * 5},
        "any"  => sub {$_[0] * 2},
        "000"  => sub {$_[0] * 1},
        );

This will only be accessed within our module, not by the user. We will call it like:

    $payoff = $payoff{$spin}->($bet);

But we’ll get to that shortly. So we have payoffs, but we still don’t have a machine. One very simple way to simulate this machine is with a single array containing all the digits. We can then just choose three random elements from that array and we’ll have our spin result:

    my @symbols = (0,7,0,3,0,2,0,1);

I’ve thrown in extra 0’s to make it more likely to appear (you can adjust the symbols list to better approximate a real slot machine’s probabilities if you know them).

Both the %payoff hash and the @symbols array are called class data. They are not stored with each object but only in the class, and all object methods can access them.

We still have two methods left to define: spin() and results(), according to our original specification and test program. The spin() method is the most complex and we will have to leave it to next week. But we can make an assumption about it: after spin() is called, all of the attributes will be set appropriately. That is, the credits attribute will be adjusted, the ‘win’ attribute will be set to 1 if it is a winning spin, and the ‘paid’ attribute will contain the payoff for that spin. The ‘spin’ attribute will contain a string of three digits representing the spin itself. Knowing this, we can build our results() method quite easily. However, I have decided to change the spec a little: now we will call the method display_results() and have it print the results rather than having it return the results to be printed:

    sub display_results {
        my $self = shift;
        print join(" ", split //, $self->{spin}),"\n";
        if ($self->{win}){
            print "Winner paid: $self->{paid} credits!!\n\n";
        } else {
            print "Better luck next time\n\n";
        }

    }

All we do here is grab the object and print out the spin itself by splitting it into separate digits and joining it with a space in between. We then test if the spin was a winning spin and if so we print a congratulatory message stating how much was paid, otherwise we try to encourage the loser to bet some more money.

*****