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

In reply to 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.