Thread Zahlenkombinationen suchen (16 answers)
Opened by hugenyn at 2010-10-22 00:55

topeg
 2010-10-23 18:30
#142135 #142135
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Meinst du das Spiel sollte so ablaufen?

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @peg=((1..12)x4,13..19);# qw /1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11 12 12 12 12 13 14 15 16 17 18 19/;
my @range=(1..26);

#Karten mischen
for(@peg)
{
  my $p=int(rand(scalar @peg));
  my $k=$_;
  $_=$peg[$p];
  $peg[$p]=$k;
}


# Erste Hand abheben
my @hand=splice(@peg,0,4);

my $cnt=0;
while(@hand && $cnt++<1000)
{
  print "Hand ist nun: ".join(', ',sort @hand)."\n";

  my %result;
  my %found;
  for (combinations(@hand))
  {
    next if($found{"@$_"}++);
    push(@{$result{sum(@$_)}},$_);
  }

  # found
  if(%result)
  {
    my ($val,@best)=best_choice(\@range,\%result);
    # remove card from hand
    for my $card (@best)
    {
      # remove card from hand
      @hand=map{$card==$_?():$_}@hand;

      # put the Cards back
      push(@peg,$card);
    }

    print "Kombination gefunden!(".join(' + ',@best)." = $val)\n";
  }
  else
  {
    print "Keine Kombination gefunden!\n";
    # Spiel abbrechen, verloren...
    last;
  }

  # hand füllen
  push(@hand,shift(@peg)) while(@hand && @hand<4);
  print "\n";
}

if(@hand)
{ print "VERLOREN!\n"; }
else
{ print "GEWONNEN! ($cnt RUNDEN)\n"; }

########################################################################

sub combinations
{
  my @cards=@_;

  return [] unless (@cards);

  return \@cards if(@cards==1);

  my @ret;
  my %found;
  for my $cpos (0..$#cards)
  {
    my $card=$cards[$cpos];
    my @list=@cards;
    splice(@list,$cpos,1);
    push(@ret,[$card]);
    for my $r (combinations(@list))
    {
      my @rr=sort(@$r,$card);
      # übergehe schon gefundene Kombinationen...
      next if($found{"@rr"}++);
      push(@ret,\@rr);
    }
  }

  return @ret;
}

sub best_choice
{
  my $vals=shift;
  my $found=shift;

  my @best;
  my $val;

  for my $v (@$vals)
  {
    next unless($found->{$v});

    for my $cards (@{$found->{$v}})
    {
      # wenn noch nichts gesetzt,
      # dann nimm das Erstbeste
      unless(@best && $val)
      {
        @best=@$cards;
        $val=$v;
      }

      # die Längst Zahlenreihe,
      # oder wenn gleich lang,
      # die größte Summe
      if(@best<@$cards || (@best==@$cards && $val<$v) )
      {
        @best=@$cards;
        $val=$v;
      }
    }
  }

  #es wurde nichts gefunden ...
  return 0 unless($val && @best);

  # das Ergebnis
  return ($val,@best);
}

sub sum
{
  my $summ=0;
  $summ+=$_ for(@_);
  return $summ
}

View full thread Zahlenkombinationen suchen