function feasible?

Discussion in 'Perl Misc' started by Blah, Feb 17, 2006.

  1. Blah

    Blah Guest

    I keep getting errors if I leave the elsif section in...is there a
    better way of doing conditionals?

    thanks...


    sub victory
    {
    if ($board[0] eq 'O' && $board[2] eq 'O' && $board[4] eq 'O'
    || $board[5] eq 'O' && $board[7] eq 'O' && $board[9] eq 'O'
    || $board[10] eq 'O' && $board[12] eq 'O' && $board[14] eq 'O'
    || $board[0] eq 'O' && $board[7] eq 'O' && $board[14] eq 'O'
    || $board[4] eq 'O' && $board[7] eq 'O' && $board[10] eq 'O')
    {
    print "Player 1 Wins\n";
    }


    elsif ($board[0] eq 'X' && $board[2] eq 'X' && $board[4] eq
    'X'
    || $board[5] eq 'X' && $board[7] eq 'X' && $board[9] eq 'X'

    || $board[10] eq 'X' && $board[12] eq 'X' && $board[14] eq
    'X'
    || $board[0] eq 'X' && $board[7] eq 'X' && $board[14] eq
    'X'
    || $board[4] eq 'X' && $board[7] eq 'X' && $board[10] eq
    'X'
    )
    {
    print "Player 2 Wins\n";
    }


    else
    {
    decider();
    }
    }
     
    Blah, Feb 17, 2006
    #1
    1. Advertising

  2. Blah

    Guest

    Blah wrote:
    > I keep getting errors if I leave the elsif section in...is there a
    > better way of doing conditionals?


    Your code works fine for me. But, if you look more closely, you will
    see that you are doing the same group of operations twice (once for "X"
    and once for "O"). That screams loop. And you are doing the same basic
    sort of operations within each block. That screams loop.

    Consider something like this:

    foreach my $group( [0,2,4],[5,7,9],[10,12,14],[0,7,14],[4,7,10] ) {
    foreach my $player('X','O') {
    if ( (join '', @board[@$group]) eq $player x 3 ) {
    print "Player $player Wins!\n";
    }
    }
    }

    --
    http://DavidFilmer.com
     
    , Feb 17, 2006
    #2
    1. Advertising

  3. Blah

    Guest

    wrote:
    > <snip not-very-well-thought-out code>


    Ya know, since the conditionals are identical for X and O, all you
    really need to do is see if you match three-in-a-row, so the inside
    loop is not really needed (ie, you don't need to check "is there XXX"
    and then check "Is there OOO" - all you need to do is check if the
    value of the first element is repeated two more times, and, if it is,
    that value determines the winner). So you could instead do something
    even more simple like this:

    foreach my $group( [0,2,4],[5,7,9],[10,12,14],[0,7,14],[4,7,10] ) {
    if ( (join '', @board[@$group]) eq $board[ $group->[0] ] x 3 ) {
    print "Player $board[ $group->[0] ] Wins!\n";
    }
    }

    --
    http://DavidFilmer.com
     
    , Feb 17, 2006
    #3
  4. Blah <> wrote:

    > I keep getting errors



    What did the error message say?



    Have you seen the Posting Guidelines that are posted here frequently?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HUANG Huan
    Replies:
    2
    Views:
    705
    Dave Higton
    Feb 24, 2004
  2. lapenta[
    Replies:
    1
    Views:
    787
    Mike Treseler
    Nov 6, 2004
  3. Taras_96
    Replies:
    2
    Views:
    4,984
    Taras_96
    Aug 3, 2005
  4. Newbie
    Replies:
    4
    Views:
    410
    Newbie
    Oct 27, 2003
  5. Shepard Tate

    Is this feasible?

    Shepard Tate, Feb 25, 2004, in forum: HTML
    Replies:
    25
    Views:
    880
    Whitecrest
    Mar 1, 2004
Loading...

Share This Page