Very fun! I translated your code into nearly-pseudocode for anyone who cares.

use strict; use warnings FATAL => 'all'; use vars qw(@Game_Board @Available_Moves $Turn); play_game(); sub eqany { $_[0] eq $_ && return 1 for @_[1 .. $#_]; return } sub eqall { $_[0] ne $_ && return for @_[1 .. $#_]; return 1 } sub play_game { # Reset the game before playing. local $Turn = 0; # The first element is never used local @Game_Board = (undef, ('.') x 9); local @Available_Moves = 1 .. 9; while ( @Available_Moves ) { switch_players(); my $marker = player_marker(); make_random_move( $marker ); my $winning_move = find_winning_move( $marker ); print rendered_board( $winning_move ); return if $winning_move; pause_a_moment(); } } sub pause_a_moment { select undef, undef, undef, .1 } sub rendered_board { my $winning_move = shift; my $board = "\ec"; for my $position ( 1 .. $#Game_Board ) { # Highlight winning positions if ($winning_move and eqany( $position, @$winning_move) ) { # invert highlight $board .= "\e[7m"; $board .= $Game_Board[$position]; # Remove highlight $board .= "\e[m"; } else { $board .= $Game_Board[$position]; } # Spaces until the end of the line $board .= $position % 3 ? ' ' : "\n"; } return $board; } sub switch_players { $Turn = not $Turn } sub player_marker { $Turn ? 'X' : 'O' } sub find_winning_move { my $marker = shift; for my $candidate_win ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ], [ 1, 5, 9 ], [ 3, 5, 7 ] ) { if ( eqall( $marker, @Game_Board[ @$candidate_win ] ) ) { return $candidate_win; } } } sub make_random_move { my $marker = shift; $Game_Board[ choose_random_move() ] = $marker; } sub choose_random_move { shuffle( \ @Available_Moves ); pop @Available_Moves; } sub shuffle { # copied from perldoc my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); @$array[$i,$j] = @$array[$j,$i]; } }

In reply to Re: and another by diotalevi
in thread and another by teamster_jr

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.