Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have an implementation of minimax lying around? I saw that there is on in O'Reilly's Perl Algorithms book, but as a poor high school student, I can't really afford it... Hell, I can't afford any perl books (thank god for perldoc.com).

Any help would be appreciated. I google'd for about 20 minutes before giving up...

Title edit by tye

  • Comment on Want MiniMax() code from Mastering Algorithms with Perl

Replies are listed 'Best First'.
Re: MiniMax
by thelenm (Vicar) on May 17, 2002 at 16:21 UTC
    Since I'm reading the Algorithms book currently, I have it right here in front of me. All the example programs are available online from here. Get either "examples.tar.gz" or "examples.zip". After untar/zipping, the minimax algorithm is "ch05/minimax".

    Aw heck, I'll just post it here too :-) (code untested by me, and it looks like you'll need the TicTacToe module included in the examples tarball)

    #!/usr/bin/perl use TicTacToe; # Usage: # To choose the next move: # ($moves,$score) = minimax($position,$depth) # You provide a game position object, and a maxmimum depth # (number of moves) to be expanded before cutting off the # move generation and evaluating the resulting position. # There are two return values: # 1: a reference to a list of moves (the last element on the # list is the position at the end of the sequence - either # it didn't look beyond because $depth moves were found, or # else it is a terminating position with no moves posible. # 2: the final score sub minimax { my ( $position, $depth ) = @_; # Have we gone as far as permitted or as far as possible? if ( $depth-- and defined($position->prepare_moves) ) { # No - keep trying additional moves from $position. my $move; my $best_score = -$position->best_rating; my $best_move_seq; while ( defined( $move = $position->next_move ) ) { # Evaluate the next move. my ( $this_move_seq, $this_score ) = minimax( $position->make_move($move), $depth ); # Opponent's score is opposite meaning of ours. $this_score = -$this_score; if ( $this_score > $best_score ) { $best_score = $this_score; $best_move_seq = $this_move_seq; unshift ( @$best_move_seq, $move ); } } # Return the best one we found. return ( $best_move_seq, $best_score ); } else { # Yes - evaluate current position, no move to be taken. return ( [ $position ], -$position->evaluate ); } } my $game = tic_tac_toe->new( ); my ( $moves, $score ) = minimax( $game, 2 ); my $my_move = $moves->[0]; print "I move: $my_move\n";
      tic tac toe module, eh? it just happens that's what i'm writing right now! mucho gracias
Re: MiniMax
by Beatnik (Parson) on May 17, 2002 at 16:24 UTC
Re: MiniMax
by atcroft (Abbot) on May 18, 2002 at 16:13 UTC

    Perhaps I'm a little jaded, having encountered homework questions before (and my appologies if I appear thus), but this sniffs suspiciously like homework to me, especially after your response about trying to write a tic-tac-toe program (a common assignment in computer science classes), and at a time generally near the end of semesters.

    Had you tried to code from the algorithm? If the code didn't work, post and someone might could point out where it could have been improved or where you went astray.

    "Google'd for 20 minutes before giving up"? Surprisingly, I found a description of entitled "How min-max works" and a description of "Minimax Trees" on the first pages of searches for "min-max tree algorithm" and "minimax tree algorithm" respectively.

    Try the Super Search here? A search there for "minimax" returned two postings containing that phrase, one being a rather long thread regarding the implementation of a tic-tac-toe game and the idea of using the minimax algorithm as part of the intelligence of the game (the other was one of the responses). Look at any of the resources in the Outside Links section?

    I understand being a poor student (it was not that long ago), but you have more resources available than you seemed to have tried to exhaust. Beyond even these, there are numerous websites relating to programmers, developers, and so forth (although in my opinion this one truly rocks), plus your local library (libraries, assuming a local public library as well as the school library, plus possibly local college or university libraries), inter-library loan programs (ask your local librarian), and looking through books and magazines available at your local bookstore.

    I expect to get quite a negative reaction for this posting, but a storehouse of information is only useful when one can find their particular needle in the haystack effectively. I hope perhaps some of the information in this posting may be of help to someone else in trying to find their own needles. It is also my hope that others will pitch in comments that will be helpful, and possibly illuminative in searching for such as this effectively. I know there are other postings on how to find things (such as Masem's most excellent How to RTFM posting, among others).