When run like this:sub test_strategy { my $func_ref = $_[0]; my $dict_file = "easy.txt"; open(FH, $dict_file) or die "Can't open $dict_file: $!\n"; my @words = <FH>; chomp(@words); my $n_runs = 200; my $verbose = 0; my $total_count = 0; for (my $i = 0; $i < $n_runs; ++$i) { my @dup_words = @words; my $count = 1; # Pick the word that should be guessed my $the_word = random_arr_elem(\@dup_words); print "The word: $the_word\n" if ($verbose); # Here the guessing starts # my $guess = &$func_ref(\@dup_words); while (1) { my $score = score($the_word, $guess); print "$guess $score\n" if ($verbose); last if ($score == $words_equal); my $ref = refine_words_array(\@dup_words, $guess, $score); @dup_words = @$ref; die if (scalar(@words) == 0); $guess = &$func_ref(\@dup_words); ++$count; } print "Guessed after $count tries\n" if ($verbose); $total_count += $count; print "$i " if ($i % 10 == 1); } my $av_count = $total_count / $n_runs; print "\nRun $n_runs games. Average guess count: $av_count\n"; }
On average, this strategy guesses a word after 7.8 tries.test_strategy(\&random_arr_elem);
In reply to Re: The Jotto word game
by spurperl
in thread The Jotto word game
by spurperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |