Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Exercise: Create a tic-tac-toe game by using arrays. Consider the fo

ID: 3733362 • Letter: P

Question

Programming Exercise: Create a tic-tac-toe game by using arrays. Consider the following requirements during your implementation: 1. You have the choice to play: user-to-user or user-to-computer (computer must generate random numbers) 2. The program should ask the user if he wants to go first (be X) or second (be O) 3. The program should have the ability to make "intelligent" moves acting either as the X-player or the 0-player. It should 4. The program should have the ability to make "intelligent" moves acting either as the X-player or the 0-player. It should make no illegal or impossible moves, nor remove any previous move until the game is over The program should have referee capability which can determine whether a move is a good move and also whether a game has been won by X-player or 0-player. It should stop the player from making an impossible move (e.g. one not on the game board) and an illegal move (e.g. one that attempts to place X or O over an already occupied location). The program should re-display the current board after each move, and should have the following appearance, including the matrix indices. (Take this as an example of a game in progress.) 5. 6. (0,0) (0,2) 0O (2,0) (2,2) 7. The user should be able to enter his move by answering at a prompt "Your move:"with the location of his next move: rc (that is, row#, column#). For instance, in the game above, the user being X could have responded with 2,2 -in which case he would have been notified that he won the game, "Tic-tac-toe!" But, for example, if the user had entered, ,0 he would have been told that it is illegal to attempt to move on to an already occupied location.

Explanation / Answer

#!/usr/bin/perl

use strict;
use warnings;
my $board = "123456789";
my @wins = qw/ 123 456 789 147 258 369 159 357 /;
sub board {
print " $_" for ($board =~ /(...)/g);
if ($_ = shift) { print; exit; }
}

sub move { # Returns 0 for invalid move or piece, 1 for valid
my $piece = shift; # X or O
my $move = shift; # 1 through 9
return 0 if $piece !~ /^[XO]$/ or $move !~ /^[1-9]$/;
return 0 unless $board =~ s/$move/$piece/;
for (0..7) {
board (" $piece wins! ") if $wins[$_] =~ s/$move/$piece/ && $wins[$_] eq $piece x 3;
}
board (" Tie! ") if $board !~ /d/;
return 1;
}
sub check {
my $piece = shift; # X or O
my $move = shift; # 1 through 9
return 0 if $piece !~ /^[XO]$/ or $move !~ /^[1-9]$/;
return grep { /$piece/ && /$move/ && /d${piece}?d/ } @wins;
}
sub player {
my $retval = 0;
my $move;
until ( $retval ) {
print " Move : ";
chomp ($move = <>);
$retval = move('X', $move);
}
}

sub bot {
return if move('O', 5);
my $move;
my @open = ($board =~ /(d)/g);
for $move (@open) { return move('O',$move) if grep {/$move/ && /Od?O/} @wins; }
for $move (@open) { return move('O',$move) if grep {/$move/ && /Xd?X/} @wins; }
for (@open) { return move('O',$_) if ( check('O', $_) > 1 ); }
for $move (@open) {
if ( my @seq = check('O', $move) ) {
my ($opposite) = $seq[0] =~ /([^O$move])/;
return move('O', $move) if ( check('X', $opposite) < 2 );
}
}
move ('O', $open[0]);
}
while (1) {
board;
player;
bot;
}