package Klax; sub new { my $class = shift; my $self = {@_}; bless ($self, $class); $self->init (); return $self; } 1; ###################################################################### package Klax::Game; use base 'Klax'; sub init { my $self = shift; my $board = $self->{'board'} = new Klax::Board ("width" => $self->{"width"}, "height" => $self->{"height"}); my $stack = $self->{'stack'} = new Klax::Stack ("size" => $self->{"ssize"}, "posmax" => $self->{"width"}, "pos" => 0); my $grid = $self->{'grid'} = new Klax::Grid ("width" => $self->{"width"}, "height" => $self->{"depth"}); $stack->rand; $board->rand; return $self; } sub popdown { my $self = shift; my $val = $self->{"stack"}->pop; if (length ($val) and ($self->{"board"}->push ($val, $self->{"stack"}->{"pos"}))) { return $self; } return undef; } sub pos { my $self = shift; my $col = shift; if ($self->{"stack"}->pos ($col)) { return $self; } return undef; } ###################################################################### package Klax::Board; use base 'Klax'; sub init { my $self = shift; my $data = $self->{"data"} = []; for (1..$self->{"width"}) { my $col = []; push (@{$col}, ' ') for (1..$self->{"height"}); push (@{$data}, $col); } return $self; } sub rand { my $board = shift; for (1..(int (rand ($board->{"width"} * $board->{"height"})))) { $board->push (int (rand (8)), int (rand (5))); } return $board; } sub push { my $self = shift; my $num = shift; my $col = shift; return undef unless (($col >= 0) and ($col < $self->{"width"})); my $cdat = $self->{"data"}->[$col]; my $row = 0; $row++ while (($row < $self->{"height"}) and ($cdat->[$row] ne ' ')); return undef if ($row == $self->{"height"}); $cdat->[$row] = $num; return $self; } ###################################################################### package Klax::Stack; use base 'Klax'; sub init { my $self = shift; my $data = $self->{"data"} = []; return $self; } sub rand { my $stack = shift; for (1..(int (rand ($stack->{"size"} + 1)))) { $stack->push (int (rand (8))); } $stack->pos (int (rand ($stack->{"posmax"}))); return ($stack); } sub push { my $self = shift; my $num = shift; if (($#{$self->{"data"}} + 1) < $self->{"size"}) { push (@{$self->{"data"}}, $num); return $self; } else { return undef; } } sub pop { my $self = shift; return shift (@{$self->{"data"}}); } sub pos { my $self = shift; my $pos = shift; $self->{"pos"} = $pos; return $self; } ###################################################################### package Klax::Grid; use base 'Klax'; sub init { my $self = shift; my $data = $self->{"data"} = []; for (1..$self->{"width"}) { my $col = []; push (@{$col}, ' ') for (1..$self->{"height"}); push (@{$data}, $col); } return $self; }