While this might be too late for your submission of code, I still would like to propose a slightly different approach. At the center of my proposal is a list of all the possible lines (rows, columns and diagonals) that constitute a potential win. This way you can replace your if, elsif chain with a loop. Secondly, I would count the Xs, Os and blanks for each line and then decide whether or not there is a win or a draw situation. It could look like this:

use strict; use warnings; my @board = qw( O X O X X O O O X ); my @tests = ( # rows [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], # columns [ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ], # diagonals [ 0, 4, 8 ], [ 2, 4, 6 ] ); # testing for win and draw my $win = 0; my $draw = 1; for my $line ( @tests ) { my %symbols = ( _ => 0, X => 0, O => 0 ); # count occurences of each symbol $symbols{$board[$_]}++ for @$line; # test for winning if( $symbols{'X'}==3 or $symbols{'O'}==3 ) { $win = 1; $draw = 0; last; } # test for "open lines" if( $symbols{'X'}==0 or $symbols{'O'}==0 ) { $draw = 0; } } print "Win: $win, draw: $draw\n";

In reply to Re: TicTacToe Code Help. by hdb
in thread TicTacToe Code Help. by Jsamp

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.