Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
QM,
So, here is the problem:
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; my $level = $_; my @work = {board => $level, hits => 9}; my $avail_hits = possible_hits($level); my %stat; while (@work) { my $item = pop @work; my ($board, $hits) = @{$item}{qw/board hits/}; $hits--; for my $pos (possible_hits($board)) { my $new_board = play_board($board, $pos); # No need to continue if we have won if (game_won($new_board)) { $stat{$hits}++; next; } # Not able to continue if we have lost if (! $hits) { $stat{lost}++; next; } # We need to continue push @work, {board => $new_board, hits => $hits}; } } print join(',', $level, $avail_hits, ($stat{lost} || 0), map {join(':', (9 - $_), ($stat{$_} || 0))} reverse 0 .. 8 ), "\n"; } sub game_won { my ($board) = @_; return $board =~ /^0{30}$/; } sub possible_hits { my ($board) = @_; my @possible; for (0 .. length($board) - 1) { push @possible, $_ if substr($board, $_, 1) ne '0'; } return wantarray ? @possible : scalar @possible; } sub play_board { my ($board, $pos) = @_; hit(\$board, $pos); return $board; } sub hit { my ($board, $pos) = @_; # Ran off the edge of the board return if $pos < 0 || substr($$board, $pos, 1) eq '0'; # not sure +2nd instance can ever happen my $val = substr($$board, $pos, 1); $val--; substr($$board, $pos, 1, $val); if (! $val) { hit($board, n_neighbor($board, $pos)); hit($board, s_neighbor($board, $pos)); hit($board, e_neighbor($board, $pos)); hit($board, w_neighbor($board, $pos)); } } sub n_neighbor { my ($board, $pos) = @_; while (1) { $pos -= 5; last if $pos < 0; return $pos if substr($$board, $pos, 1) ne '0'; } return -1; } sub s_neighbor { my ($board, $pos) = @_; while (1) { $pos += 5; last if $pos > 29; return $pos if substr($$board, $pos, 1) ne '0'; } return -1; } sub e_neighbor { my ($board, $pos) = @_; my $min_val = int($pos / 5) * 5; while (1) { $pos -= 1; last if $pos < $min_val; return $pos if substr($$board, $pos, 1) ne '0'; } return -1; } sub w_neighbor { my ($board, $pos) = @_; my $max_val = (int($pos / 5) * 5) + 4; while (1) { $pos += 1; last if $pos > $max_val; return $pos if substr($$board, $pos, 1) ne '0'; } return -1; } __DATA__ 101010000010101000000000010101 000000000000100000000000000000 211000000000000000000000000000 432114010304403414114124232323

It takes too long to figure out all the ways to play a game. I am thinking I need to lower the number of possible moves and limit how much time can be spent playing a single game. What do you think?

Update: I can complete approximately 40,000 games every 5 seconds. What I have done is impose a 6 hit limit along with randomly shuffling the return of possible_hits() and limited each board to 40,0000 games. I have manually tested this against a handful of boards and it appears to give a pretty good approximation of the results if all of them had been played. I would prefer a C based version of this which could likely do far more per second but my C is rusty (was never very good) and I can't remember how to manage a dynamic array.

Update 2: I turned the problem upside down and it became much easier. Instead of scoring how difficult the game was, I instead scored how easy the game was. I used a combined score of things like win to loss ratio, number of initial 1-hit moves to 1-hit wins, etc. The ones with the lowest score were the most difficult. Now I just need to code the actual game.

Cheers - L~R


In reply to Re^2: Challenge: Algorithm To Generate Bubble Blast 2 Puzzles With Difficulty by Limbic~Region
in thread Challenge: Algorithm To Generate Bubble Blast 2 Puzzles With Difficulty by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found