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

Hello Can I get some Help for this Perl scripting Exercise? thank you Create a t

ID: 3733085 • Letter: H

Question

Hello Can I get some Help for this Perl scripting Exercise? thank you

Create a tio-tac-toe game by using arrays. Consider the following requirements during your implementation (computer must generate random numbers) 1. You have the choice to play: user-to-user or user-to 2. The program should ask the user if he wants to go first (be X) or second (be 0). 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 5. e.g. one that attempts to place X or O over an already occupied location). 6. 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.) (0,0) (0,2) (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, 1, 0 he would have been told that it is illegal to attempt to move on to an already occupied location.

Explanation / Answer

-------------------------------------------------------------------------------------------------------------------------------

# cgilib.pm (filename)

package cgilib;
sub ReadParseit {

local (*in)=@_ if @_;
local ($i, $key, $val);


# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
     $in= $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
}

@in= split(/&/,$in);

foreach $i (0 .. $#in) {
#Convert pluses to spaces
$in[$i] =~ s/+/ /g;

#Split into key and value.
($key, $val)= split(/=/,$in[$i],2); # splits on the first =.

#Converts %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

#Associate key and value
$in{$key} .= "" if (defined($in{$key})); # is the multiple oper
$in{$key} .= $val;

}

return 1;

}

1;

-----------------------------------------------------------------------------------------------------------------------------

#play.cgi

#!/usr/local/bin/perl
use cgilib;
print "Content-type: text/html ";
&cgilib::ReadParseit (*data);


$action=$data{'action'};
$selection=$data{'selection'};
$active_player=$data{'active_player'};
$game_is_over=$data{'game_is_over'};



$level="level1";

$x=$data{'x'};
$y=$data{'y'};
$str_pos=$data{'str_pos'};
if ($str_pos eq "")
{
       $str_pos="222222222";
}

$msg1="";




for ($i=1; $i<4; $i++) {
for ($j=1; $j<4; $j++)
{
    $a[$i][$j]=substr ($str_pos, ($i-1)*3 + $j-1, 1);
} }


do_action($action);



$is_win=Is_Win();

if ($is_win == 1)
{
$msg1="<b><font=+1>The game is over</font></b>";
$Score0=$Score0+1;
$game_is_over=1;
}

if ($is_win == -1)
{
$msg1="<b><font=+1>The game is over</font></b>";
$Score1=$Score1+1;
$game_is_over=1;
}


if ($is_win == 0)
{
$msg1="<b><font=+1>The game is over</font></b>";
$game_is_over=1;
}



if ($a[1][1]==2) { $a11=""; }
if ($a[1][1]==1) { $a11="x"; }
if ($a[1][1]==0) { $a11="0"; }
if ($a[1][2]==2) { $a12=""; }
if ($a[1][2]==1) { $a12="x"; }
if ($a[1][2]==0) { $a12="0"; }
if ($a[1][3]==2) { $a13=""; }
if ($a[1][3]==1) { $a13="x"; }
if ($a[1][3]==0) { $a13="0"; }

if ($a[2][1]==2) { $a21=""; }
if ($a[2][1]==1) { $a21="x"; }
if ($a[2][1]==0) { $a21="0"; }
if ($a[2][2]==2) { $a22=""; }
if ($a[2][2]==1) { $a22="x"; }
if ($a[2][2]==0) { $a22="0"; }
if ($a[2][3]==2) { $a23=""; }
if ($a[2][3]==1) { $a23="x"; }
if ($a[2][3]==0) { $a23="0"; }

if ($a[3][1]==2) { $a31=""; }
if ($a[3][1]==1) { $a31="x"; }
if ($a[3][1]==0) { $a31="0"; }
if ($a[3][2]==2) { $a32=""; }
if ($a[3][2]==1) { $a32="x"; }
if ($a[3][2]==0) { $a32="0"; }
if ($a[3][3]==2) { $a33=""; }
if ($a[3][3]==1) { $a33="x"; }
if ($a[3][3]==0) { $a33="0"; }

$str_pos=convert_to_string();

print<<"EOP3";
<h1> Demo for RL Tic Tac Toe Game </h1>
<form name="form1" method=post>
<input type="text" name="t11" size="1" value="$a11">
<input type="text" name="t12" size="1" value="$a12">
<input type="text" name="t13" size="1" value="$a13">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg1
<br>
<input type="text" name="t21" size="1" value="$a21">
<input type="text" name="t22" size="1" value="$a22">
<input type="text" name="t23" size="1" value="$a23">
<br>
<input type="text" name="t31" size="1" value="$a31">
<input type="text" name="t32" size="1" value="$a32">
<input type="text" name="t33" size="1" value="$a33">

<input type="hidden" name="ap" value="$active_player">
<input type="hidden" name="str_pos" value="$str_pos">
<input type="hidden" name="level" value="$level">
<br>
<br>
<input type="submit" value="New Game" name="action">
<input type="hidden" value="$game_is_over" name="game_is_over">
</form>
<b>How to play: Click on any cell to put 0. RL agent will put x.<br><//b>
EOP3


  



if ($game_is_over != 1 )
{
print<<"EOP4";

<script language="VBSCRIPT">



sub t11_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=1&y=1&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t12_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=1&y=2&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t13_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=1&y=3&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t21_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=2&y=1&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t22_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=2&y=2&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t23_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=2&y=3&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t31_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=3&y=1&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t32_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=3&y=2&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub

sub t33_onFocus
dim ap11
dim str_pos
dim level
ap11=document.form1.ap.value
str_pos=document.form1.str_pos.value
level=document.form1.level.value
Top.location.href="play.cgi?action=move&x=3&y=3&active_player=" & ap11 & "&str_pos=" & str_pos & "&level=" & level
end sub



</script>



EOP4
}

sub do_action
{
if ( $action eq "New Game" )
{
   if ($active_player==1)
   {
      $active_player=0;
   }
   else
       { $active_player=1; }


   for ($i = 1; $i<4; $i++)
   {
   for ($j=1; $j<4; $j++)
   {
     $a[$i][$j] =2;
   }
   }

   $str_pos="222222222";

   $game_is_over=0;
}

if ( $action eq "reset_score")
{
   $Score0=0;
   $Score1=0;
}


if ( substr($action,5) eq "level")
   { $filename=$action.".txt"      }



if ( $action eq "move" )
   {
     update_position($x,$y);
     if (   $game_is_over != 1 )
     {
        select_action();            # make move for RL agent
     }
   }

}
################################

sub get_V
{
my $s_board_position= shift;
my $t=":";

      $is_win_res1=Is_Win($s);  
      if ( $is_win_res1 == 0) {return 0;}
      if ( $is_win_res1 == 1.0) {return 1;}
      if ( $is_win_res1 ==-1) {return -1;}



open (FILE, $level.".txt");

while ($line=<FILE>)
{

@dd=split ($t, $line);
if ( $dd[0] eq $s_board_position)
    { close (FILE); return $dd[1]; }

}
close (FILE);
return 0.5;
}



sub update_position
{
# need first validate: valid if a x,y=2
$a[$x][$y]= 0 ;   
}



sub Is_Win
{
for ($i= 1; $i < 4 ; $i++)
{
   if ( $a[$i][1]==1 && $a[$i][2] == 1 && $a[$i][3] == 1 ) { return 1; }

   if ( $a[$i][1]==0 && $a[$i][2] == 0 && $a[$i][3] == 0 ) { return -1; }
}


for ($j= 1; $j < 4 ; $j++)
{
   if ( $a[1][$j]==1 && $a[2][$j] == 1 && $a[3][$j] == 1 ) { return 1; }

   if ( $a[1][$j]==0 && $a[2][$j] == 0 && $a[3][$j] == 0 ) { return -1; }
}


if ( $a[1][1]==1 && $a[2][2] == 1 && $a[3][3] == 1 ) { return 1; }

if ( $a[1][3]==1 && $a[2][2] == 1 && $a[3][1] == 1 ) { return 1; }

if ( $a[1][1]==0 && $a[2][2] == 0 && $a[3][3] == 0 ) { return -1; }

if ( $a[1][3]==0 && $a[2][2] == 0 && $a[3][1] == 0 ) { return -1; }

$count_emty11=0;

for ($i= 1; $i < 4 ; $i++)
{
    for ($j= 1; $j < 4 ; $j++)
    {   if ($a[$i][$j] ==2)
            { $count_emty11++; }
    }
}

if ( $count_emty11 == 0 ) {return 0; }  


return 0.3;
}


sub convert_to_string
{

return $a[1][1].$a[1][2].$a[1][3].$a[2][1].$a[2][2].$a[2][3].$a[3][1].$a[3][2].$a[3][3];
}



sub select_action
{

# select action with max V


$V_max= -2;


for ($ii=1; $ii<4; $ii++)
{

    for ($jj=1; $jj<4; $jj++)
    {

    if ($a[$ii][$jj]==2)
    {
    
      $a[$ii][$jj]=1;

      $s=convert_to_string();
  
      $a[$ii][$jj]=2;

      $V1=get_V($s);
      if ($V1>$V_max)
      {
        $V_max=$V1;
        $i_max=$ii;
        $j_max=$jj;
        $x=$ii;
        $y=$jj;
      }

    }
   }
}



       $a[$i_max][$j_max]=1;
    
       $new_sss=convert_to_string();
       return $new_sss;
}


sub lookupV
{

my $s1=shift;

my $is_win_res=Is_Win($s1);  
if ( $is_win_res == 0) {$V{"$s1"} =0; return 0;}
if ( $is_win_res == 1) {$V{"$s1"} =1; return 1;}
if ( $is_win_res ==-1) {$V{"$s1"} =-1; return -1;}

return 0.5;
}

---------------------------------------------------------------------------------------------------------------------------------

#tt.cgi

#!/usr/local/bin/perl
use cgilib;
print "Content-type: text/html ";
&cgilib::ReadParseit (*data);




# RL for Tic-Toe
##
## 0      -> 0
## x      -> 1            RL agent
## empty -> 2


$ww=0;
$s_record="";


$N=400000; # number of episods
$n=0;



while ($n < $N)
{
   for ($i = 1; $i<4; $i++) {
   for ($j=1; $j<4; $j++)   {
      $a[$i][$j] =2;
   }
   }


$terminal_state=0;

while ( $terminal_state == 0 )
{


    #opponent move
    $cur_s= convert_to_string();

    make_ran_move();
  
  
    $new_s= convert_to_string();

   
     
    Update_V( $cur_s, $new_s);
   
    $is_win_res=Is_Win();
    if ( ($is_win_res==0) || ( $is_win_res==-1) )
    {
         $terminal_state=1;   
         next;
    }

    $cur_s= convert_to_string();
    $new_s=select_action();

    Update_V( $cur_s, $new_s);
  
    
    $is_win_res=Is_Win();

    if ( ($is_win_res==0) || ($is_win_res==1) || ( $is_win_res==-1) )
    {
         $terminal_state=1;
    }
  

}
$n++;
if ($is_win_res == 1)  
{ $ww=$ww+1; }

if ($n % 500 ==0 )
{

   $s_record=$s_record." ".$ww;
   $ww=0;
}

print "n= ".$n." ";

} #while n< N




print $s_record;
save_data();



sub Is_Win
{
for ($i= 1; $i < 4 ; $i++)
{
   if ( $a[$i][1]==1 && $a[$i][2] == 1 && $a[$i][3] == 1 ) { return 1; }

   if ( $a[$i][1]==0 && $a[$i][2] == 0 && $a[$i][3] == 0 ) { return -1; }
}


for ($j= 1; $j < 4 ; $j++)
{
   if ( $a[1][$j]==1 && $a[2][$j] == 1 && $a[3][$j] == 1 ) { return 1; }

   if ( $a[1][$j]==0 && $a[2][$j] == 0 && $a[3][$j] == 0 ) { return -1; }
}


if ( $a[1][1]==1 && $a[2][2] == 1 && $a[3][3] == 1 ) { return 1; }

if ( $a[1][3]==1 && $a[2][2] == 1 && $a[3][1] == 1 ) { return 1; }

if ( $a[1][1]==0 && $a[2][2] == 0 && $a[3][3] == 0 ) { return -1; }

if ( $a[1][3]==0 && $a[2][2] == 0 && $a[3][1] == 0 ) { return -1; }

$count_emty11=0;

for ($i= 1; $i < 4 ; $i++)
{
    for ($j= 1; $j < 4 ; $j++)
    {   if ($a[$i][$j] ==2)
            { $count_emty11++; }
    }
}

if ( $count_emty11 == 0 ) {return 0; }


return 0.3;
}
  

sub convert_to_string
{

return $a[1][1].$a[1][2].$a[1][3].$a[2][1].$a[2][2].$a[2][3].$a[3][1].$a[3][2].$a[3][3];
}


sub make_ran_move
{
$r=rand;
$r=$r*10;

$count_rand=0;


while ($count_rand < 1002)
{
for ($i=1; $i<4; $i++)
{
    for ($j=1; $j<4; $j++)
    {
       $ind= ($i-1)*3 + $j;
       if (($r < $ind ) && ($a[$i][$j] == 2))
       {
          $a[$i][$j]=0;
          return $ind;
        }
    }
}
$count_rand++;
$r=rand;
$r=$r*10;  
     if ($count_rand > 1000)
     { print "ERROR!!!!Unable to generate random empty cell";
       }
} #while


}


sub select_action
{

# select action with max V

$V_max= -2;


for ($ii=1; $ii<4; $ii++)
{

    for ($jj=1; $jj<4; $jj++)
    {

    if ($a[$ii][$jj]==2)
    {
    
     
       
      $a[$ii][$jj]=1;

      $s=convert_to_string();
     


      $is_win_res1=Is_Win($s);  
      if ( $is_win_res1 == 0)    {$V{"$s"} = 0;}
      if ( $is_win_res1 == 1.0) {$V{"$s"} = 1;}
      if ( $is_win_res1 ==-1)    {$V{"$s"} =-1;}

     
      $a[$ii][$jj]=2;

      if (exists $V{"$s"})
      {
      $V1=0.5;
      $V1= $V{"$s"};
      }
      else
      {
        $V{"$s"}=0.1;
        $V1=0.1;
      }

     
      if ($V1>$V_max)
      {
        $V_max=$V1;
        $i_max=$ii;
        $j_max=$jj;
      }

    }
   }
}

     # take action for Rl 1



       $a[$i_max][$j_max]=1;
       $new_sss=convert_to_string();
       return $new_sss;
}


sub lookupV
{

my $s1=shift;

   my $is_win_res=Is_Win($s1);  
if ( $is_win_res == 0) {$V{"$s1"} =0; return 0;}
if ( $is_win_res == 1) {$V{"$s1"} =1; return 1;}
if ( $is_win_res ==-1) {$V{"$s1"} =-1; return -1;}

return 0.5;


}




sub Update_V
{
my $s1=shift;
my $s2=shift;
  
$reward=0;
$alpha=0.6;

$alpha=0.08*(0.95- 0.90 * $n/$N) ;
$gamma=1;


   if (exists $V{"$s1"})
   { }
   else
      {
       lookupV ($s1);
      }

if (exists $V{"$s2"})
{ }
else
      {
       lookupV ($s2);
      }


$V{"$s1"}= $V{"$s1"} + $alpha * ( $reward+$gamma*$V{"$s2"}-$V{"$s1"});
}  


sub save_data
{

open (FH1, ">level1.txt");

foreach $k ( keys(%V) )
{
print (FH1 $k.":".$V{"$k"}." " );
}
close (FH1);

}

-------------------------------------------------------------------------------------------------------------------


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote