#!/usr/bin/perl use warnings; use strict; package Game; sub new { my $classname = shift; my %params = @_; my $self = {players => [Player->new($params{player1}), Player->new($params{player2})], heap => Heap->new($params{heapsize})}; return bless($self, $classname); } sub start { my $self = shift; my $player; my $answer; print "\nStapelanfang: " . $self->{heap}->showSize() . "\n"; while ($self->{heap}->hasSticksLeft()) { foreach $player (@{$self->{players}}) { print "\nSpieler '" . $player->showName() . "':\n"; if ($player->showName() eq "Mensch") { $answer = 1000; while(! $self->{heap}->checkAnswer($answer)) { $answer = $player->getInput(); } } else { $answer = $player->calculateAnswer($self->{heap}->showSize()); } my @sticks = $self->{heap}->getSticks($answer); $player->putSticks(@sticks); print "Spieler '" . $player->showName() . "' nimmt $answer.\n"; $self->{heap}->updateSize(); if (! $self->{heap}->hasSticksLeft()) { last; } print "Im Stapel sind noch: " . $self->{heap}->showSize() ."\n"; } } foreach $player (@{$self->{players}}) { if ($player->hasFinalStick()) { print "\nGewinner: Spieler '" . $player->showName() . "'.\n\n"; } } } package Player; sub new { my $classname = shift; my $name = shift; my $self = {name => $name, isActive => 0, sticks => []}; return bless($self, $classname); } sub showName { my $self = shift; return $self->{name}; } sub getInput { my $self = shift; $a = "10"; while ($a =~ /[^123]/ || length $a != 1) { print "Zug (1-3): "; chomp($a = <>); } return $a; } sub calculateAnswer { my $self = shift; my $heapsize = shift; my $i; my %h; for $i (@{[4, 8, 12, 16]}) {$h{$i} = 0;} for ($i = 1; $i <= 3; $i++) { if ($heapsize - $i == 0) { last; } if (exists($h{$heapsize - $i})) { last; } if ($i == 3) { last; } } return $i; } sub putSticks { my $self = shift; my @sticks = @_; my $stick; foreach $stick (@sticks) { push(@{$self->{sticks}}, $stick)}; } sub hasFinalStick { my $self = shift; my $stick; foreach $stick (@{$self->{sticks}}) { if($stick->isLast()) { return 1; } } return 0; } package Heap; sub new { my $classname = shift; my $heapsize = shift; my $self = {size => $heapsize, sticks => []}; my $i; for ($i = 0; $i < $heapsize; $i++) { push(@{$self->{sticks}}, Stick->new()); if ($i == $heapsize - 1) { $self->{sticks}[$i]->setLast(); } } return bless($self, $classname); } sub checkAnswer { my $self = shift; my $answer = shift; if ($self->{size} >= $answer) { return 1; } else { return 0; } } sub getSticks { my $self = shift; my $nr = shift; my $i; my $stick; my @sticks; for ($i = 0; $i < $nr; $i++) { $stick = shift(@{$self->{sticks}}); push(@sticks, $stick); } return @sticks; } sub updateSize { my $self = shift; $self->{size} = @{$self->{sticks}}; } sub hasSticksLeft { my $self = shift; if ($self->{size} > 0) { return 1; } else { return 0; } } sub showSize { my $self = shift; return $self->{size}; } package Stick; sub new { my $classname = shift; my $self = {isLast => 0}; return bless($self, $classname); } sub setLast { my $self = shift; $self->{isLast} = 1; } sub isLast { my $self = shift; if ($self->{isLast} == 1) { return 1; } else { return 0; } } package main; my $game = Game->new(player1 => "Mensch", player2 => "Computer", heapsize => 17); $game->start();