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 | |
|
Re: TicTacToe Code Help.
by GotToBTru (Prior) on Feb 12, 2014 at 23:08 UTC | |
|
Re: TicTacToe Code Help.
by Jsamp (Initiate) on Feb 13, 2014 at 04:28 UTC | |
|
Re: TicTacToe Code Help.
by hdb (Monsignor) on Feb 13, 2014 at 09:53 UTC |