Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > function feasible?

Reply
Thread Tools

function feasible?

 
 
Blah
Guest
Posts: n/a
 
      02-17-2006
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();
}
}

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      02-17-2006
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

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      02-17-2006
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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57