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

In reply to Re: TicTacToe Code Help. by Jsamp
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.