T-Mobile LTE 3:20 PM @ 46% a savstate.view.usg.edu Perl Scripting CSCI 2215 Sect
ID: 3733078 • Letter: T
Question
T-Mobile LTE 3:20 PM @ 46% a savstate.view.usg.edu Perl Scripting CSCI 2215 Sectien Assignment 3 (100 pts) Instractions: Submit an electronic copy of the assignmene to the Dropbox CHECK CALENDAR FOR DUE Create a tie-t-toe game by using armays. Comider the following roquinemens during your implemation Yos have the cheice to play w--or use-to-computer(oompunr st peneane random umbers) 2. The program should ak the user if he wants so go fint (he X) or sccond (he O . The peogram should huve the ability to make "intielligent" moves acting either as the X-player or the O-player. I shoul 4 The pogram should have the ablry to make lligent moves acting eth as the Xplayer or the 0-player. h should ruke no illegal or mposable mnes, e temove any prenon move unil the patte The propam game has been won by X-player ee 0-player.k should stop thplayer bom mkng aivposshle mrwieg. me nit the game board) and an illegall move (egone a attempts to place X o ovar an ready accapied location) over. 5. uld have referee capubility which caa determine whether a move is a good t eve and aio whether a 6. The progra should re-display the cunent board after each mve and should hane the following appearance ncluding the matrix ndiees. (Takg this as an example of. pme·"progress.) 1.2 7. The wer should be able to enter ha move by answanag at a protpt .ur move.-wiáthg location ofbas next move r c (that is, rowl, columnity For instance, in the game above, the user being X could have responded with 2,2 d1,0 he would have boon told thai it is illicgal to anempt to move on to an aeady occupied locationExplanation / Answer
The Perl Code is given below:-
#!/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;
}
Please let me know in case of any clarifications required. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.