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

Hi, first post here hoping you all could give me some insight. I'm taking a perl programming class this semester and our 2nd assignment is a tic tac toe game. I've got most of the code complete but I'm having a few issues I can't seem to figure out. I'm testing the code in Eclipse as well as the Windows CMD line. Eclipse will give me the $player Wins if the win condition is met but it will not show me the winner from the CMD. Also even after a win is made I'm able to keep playing the game. I also can't seem to get a draw condition. A nudge in the right direction would be much appreciated. This is my first programming class so bear with me please.

#!/usr/bin/perl use strict; use warnings; # Create the game board my @board = qw/_ _ _ _ _ _ _ _ _/; print " _____ |$board[0]|$board[1]|$board[2]| |$board[3]|$board[4]|$board[5]| |$board[6]|$board[7]|$board[8]| "; # Create a variable to store who the winner is my $winner = "Cat"; # A subroutine to clear the screen sub cls() { system $^O eq 'MSWin32' ? 'cls' : 'clear'; } # Print directions for the user print "Please type one of the numbers for each of your moves.\n"; print " 1 2 3 4 5 6 7 8 9.\n"; # Use a for loop, and loop through 9 moves for the game for (my $i = 0; $i < 9; $i++) { #create a player variable, and using % 2, assign it as X or O my $player = ""; if ($i % 2) { $player = "O"; } else { $player = "X"; } # Using a while loop, get the player's move and make sure it is va +lid my $input = 0; my $error = ""; while ($input < 1 || $input > 9) { print $error; print "Enter your play.\n"; $input = <STDIN>; chomp $input; $error = "Invalid move.\n"; } $input = $input -1; if ($board[$input] ne 'X' and $board[$input] ne 'O') { $board[$input] = $player; } else { $i--; print "Play is already selected."; } # If this is move 5+, check for a winner if ($i > 4) { } if ($board[0] ne "_" and $board[1] eq $board[2] and $board[0] eq +$board[2]) { print "$player Wins"; } elsif ($board[3] ne "_" and $board[4] eq $board[5] and $board[3] e +q $board[5]) { print "$player Wins"; } elsif ($board[6] ne "_" and $board[7] eq $board[8] and $board[6] e +q $board[8]) { print "$player Wins"; } elsif ($board[0] ne "_" and $board[3] eq $board[6] and $board[0] e +q $board[6]) { print "$player Wins"; } elsif ($board[1] ne "_" and $board[4] eq $board[7] and $board[1] e +q $board[7]) { print "$player Wins"; } elsif ($board[2] ne "_" and $board[5] eq $board[8] and $board[2] e +q $board[8]) { print "$player Wins"; } elsif ($board[0] ne "_" and $board[4] eq $board[8] and $board[0] e +q $board[8]) { print "$player Wins"; } elsif ($board[6] ne "_" and $board[4] eq $board[2] and $board[6] e +q $board[2]) { print "$player Wins"; } cls(); print " _____ |$board[0]|$board[1]|$board[2]| |$board[3]|$board[4]|$board[5]| |$board[6]|$board[7]|$board[8]| "; }

Replies are listed 'Best First'.
Re: TicTacToe Code Help.
by toolic (Bishop) on Feb 12, 2014 at 21:43 UTC
    I can reproduce your issue on the unix command line. To figure out what is happening, comment out this line:
    # cls();

    You clear the screen too soon. You should store the results in a variable, then print after the cls.

    Also, the game continues even if someone wins. You need an exit condition.

    I got a draw with this sequence: 123465798

    See also: Basic debugging checklist

    Check out Conditional Operator for assigning to $player.

Re: TicTacToe Code Help.
by GotToBTru (Prior) on Feb 12, 2014 at 23:08 UTC

    You use a for loop for the moves, which will always execute the contents of the loop 9 times. Most of the time, you don't need that many. There is a way to break out of the loop, but the more conventional method is to have something like:

    my $win = 0; my $i = 0; do { $i += 1; printf "Player %d's turn:\n",2-$i%2; # game code here # when a winning move is detected: $win = 1; } until (($win) || ($i == 9)); printf "%s won\n", $win ? sprintf "Player %d",2-$i%2 : "Nobody";
Re: TicTacToe Code Help.
by Jsamp (Initiate) on Feb 13, 2014 at 04:28 UTC

    Thanks for the help. I did end up getting the program to exit after a winner was declared. However, I couldn't figure out how to get the draw condition working right. I had to work tonight and the assignment was due by midnight so I just went ahead and submitted what I had. GotToBTru I tried what you said but couldn't get it just right but I did take something from your advice. I also took toolic's exit condition advice. It mostly works so I'm happy. Here's what I ended up with. Thanks again.

    #!/usr/bin/perl use strict; use warnings; # Create the game board my @board = qw/_ _ _ _ _ _ _ _ _/; print " _____ |$board[0]|$board[1]|$board[2]| |$board[3]|$board[4]|$board[5]| |$board[6]|$board[7]|$board[8]|\n "; # Create a variable to store who the winner is my $winner = "Cat"; # A subroutine to clear the screen sub cls() { system $^O eq 'MSWin32' ? 'cls' : 'clear'; } # Print directions for the user print "X plays first. Please type one of the numbers for each of your +moves.\n"; print " 1 2 3 4 5 6 7 8 9.\n"; # Use a for loop, and loop through 9 moves for the game for (my $i = 0; $i < 9; $i++) { #create a player variable, and using % 2, assign it as X or O my $player = ""; if ($i % 2) { $player = "O"; } else { $player = "X"; } # Using a while loop, get the player's move and make sure it is va +lid my $input = 0; my $error = ""; my $win = 0; while ($input < 1 || $input > 9) { print $error; print "Enter your play.\n"; $input = <STDIN>; chomp $input; $error = "Invalid move.\n"; } $input = $input -1; if ($board[$input] ne 'X' and $board[$input] ne 'O') { $board[$input] = $player; } else { $i--; print "Play is already selected."; } # If this is move 5+, check for a winner if ($i > 4) { } if ($board[0] ne "_" and $board[1] eq $board[2] and $board[0] eq +$board[2]) { $win = 1; } elsif ($board[3] ne "_" and $board[4] eq $board[5] and $board[3] e +q $board[5]) { $win = 1; } elsif ($board[6] ne "_" and $board[7] eq $board[8] and $board[6] e +q $board[8]) { $win = 1; } elsif ($board[0] ne "_" and $board[3] eq $board[6] and $board[0] e +q $board[6]) { $win = 1; } elsif ($board[1] ne "_" and $board[4] eq $board[7] and $board[1] e +q $board[7]) { $win = 1; } elsif ($board[2] ne "_" and $board[5] eq $board[8] and $board[2] e +q $board[8]) { $win = 1; } elsif ($board[0] ne "_" and $board[4] eq $board[8] and $board[0] e +q $board[8]) { $win = 1; } elsif ($board[6] ne "_" and $board[4] eq $board[2] and $board[6] e +q $board[2]) { $win = 1; } cls(); print " _____ |$board[0]|$board[1]|$board[2]| |$board[3]|$board[4]|$board[5]| |$board[6]|$board[7]|$board[8]|\n "; if ($win == 1) { print "$player Wins!\n"; exit; } }
Re: TicTacToe Code Help.
by hdb (Monsignor) on Feb 13, 2014 at 09:53 UTC

    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";