#!/usr/bin/perl
#---------------------------
#  C R O O K E D B U S H . C O M  -  S C R A B B L E 
#  Designed by Jason Silver.
#  Register Online at www.crookedbush.com
#---------------------------
# This scipt is by no means associated with HASBRO, the copyright holders of the game Scrabbleİ in 
# the USA and Canada. Their address is http://www.scrabble.com. 
# Scrabbleİ is a registered trademark of J.W. Spear & Son PLC and Hasbro Inc. 
#
# Unzip the graphics and save them to your computer,
# then upload them to your server in the directory
# you decide in $graphics_url (below)
#
#---------------------------
# User Defined Variables
#---------------------------
 $URL           = 'http://www.crazybri.net/Skrabble/';# url to script, with trailing slash
 $graphics_url  = 'http://www.crazybri.net/Skrabble/images'; # no trailing slash
 $cgipath       = '/home/trukfixr/public_html/Skrabble/'; # path to this script (from server root) with trailing slash
 $script        = 'crossword.pl'; # name of this script
 $datapath      = 'boards/'; # relative path to data storage place - make sure a trailing slash (eg: 'boards/';)
 $days_delete   = 15; # this is the number of days after an untouched game is deleted automatically.

 $top_message   = '<b> ENJOY the game!</b>';
 $ad_file       = '/home/trukfixr/public_html/Skrabble/ad_file.txt'; # this is included in emails
 $use_ad_file   = 1; # Set this to zero (0) if you don't want text ads sent at the end of each email

# $smtp_server  = "localhost"; # Use either SMTP or $emailprog (SENDMAIL) depending on your system but not both!
 $emailprog     = '/usr/sbin/sendmail'; # your mailer program...
 $admin_email   = 'skrabble@crazybri.net'; # email for recieving notifications
 $notify_plays  = 0; # to receive all play notification with the other players.
 $notify_finish = 1; # to receive game over notification so you can delete complete boards
 $admin_pass    = 'missusmary';

 # words to filter out of the name selection routine. 
 # It now just filters name, not passwords or emails. Be careful, because 'ass' would ban lots of normal words 
 # that have those three letters in it. (sorry that I have to write out these words. I know they're offensive.
 @name_filter   = ("shit","asshole","ass-hole","fuck","penis");

 $screen_width  = 600;
 $tile_height   = 25;
 $tile_width    = 25;
 $board_width   = 500;
 $font_face = "Tahoma, Arial";

 $default_number_of_players = 4; # between 2 and 4
 $allow_fetch   = 1; # NEW METHOD! set this to zero if you are getting unexplained 500 errors. 
                     # This displays the latest version number at bottom of page.
#---------------------------
# Body Tags
#---------------------------
 $BACKCOLOUR = '#202020';
 $TEXTCOLOUR = '#E7A26F';
 $LINKCOLOUR = '#D47E40';
 $VISITLINK  = '#D47E40';
 $ALNKCOLOUR = '#D47E40';

# DON'T CHANGE ANYTHING BELOW THIS LINE
#___________________________
#---------------------------
# Program                                           
#---------------------------
 $version   = "6.30313-2002";
 $copyright = "1999-2003";
 $light_blue_image = "$graphics_url/double_letter.gif";
 $dark_blue_image  = "$graphics_url/triple_letter.gif";
 $light_red_image  = "$graphics_url/double_word.gif";
 $dark_red_image   = "$graphics_url/triple_word.gif";
 srand;    # Allow Random Numbers (not needed for Perl 5.004 and later)

 if($allow_fetch){
     $fetch_url   = "http://www.crookedbush.com/scripts/crossword_version.txt";
     use LWP::Simple;
     unless (defined ($new_version = get $fetch_url)){
         $new_version = "Unavailable"
     }
 }# end of allow fetch
 else{ $new_version = "Unavailable" }

 print "Content-type: text/html\n\n";

 &parse_form;
 if ($use_ad_file){&open_ad_file} # get the ad file off the server
 $datafilename  = "$FROMWEB{'board'}\.txt";
 if($datafilename eq ".txt"){
     &open_dialog;
 }# end of if.
 #---------------------------
 &open_file; # Open Game File on Server
 #---------------------------
 # Fill Game Variables
 #---------------------------
 for $count(0..14){
     $all_board_data[$count] =~ tr/[A-Z]/[a-z]/;
 }# changes case from upper to lower on board only
 @rowa       = split(/&/, $all_board_data[0]);
 @rowb       = split(/&/, $all_board_data[1]);
 @rowc       = split(/&/, $all_board_data[2]);
 @rowd       = split(/&/, $all_board_data[3]);
 @rowe       = split(/&/, $all_board_data[4]);
 @rowf       = split(/&/, $all_board_data[5]);
 @rowg       = split(/&/, $all_board_data[6]);
 @rowh       = split(/&/, $all_board_data[7]);
 @rowi       = split(/&/, $all_board_data[8]);
 @rowj       = split(/&/, $all_board_data[9]);
 @rowk       = split(/&/, $all_board_data[10]);
 @rowl       = split(/&/, $all_board_data[11]);
 @rowm       = split(/&/, $all_board_data[12]);
 @rown       = split(/&/, $all_board_data[13]);
 @rowo       = split(/&/, $all_board_data[14]);
 $title      = $all_board_data[15];             # This is the title of the game
 @letter_bag = split(/&/, $all_board_data[16]); # This places all tiles in letter bag array
 @player1    = split(/=/, $all_board_data[17]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player2    = split(/=/, $all_board_data[18]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player3    = split(/=/, $all_board_data[19]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player4    = split(/=/, $all_board_data[20]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 $turn       = $all_board_data[21];             # next player number (eg:1,2,3,4) 
 $gameover   = $all_board_data[22];

 #---------------------------
 # Assign user information
 #---------------------------
 $player = "player$turn";
 chomp($player);
 $player_name = $$player[0]; # name
 $player_mail = $$player[1]; # email
 $player_tray = $$player[2]; # letter tray
 $player_pass = $$player[3]; # password
 $player_points=$$player[4]; # points
 @letter_tray = split(/&/, $player_tray);
 #---------------------------
 # Print Headers for Page.
 #---------------------------
 print <<HTML;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Crossword Board - $title</title>
</head>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<font face="$font_face" size=3>
<form action="$URL$script" method="POST">
<input type="hidden" name="board" value="$FROMWEB{'board'}">
<p align="center"><img src="$graphics_url/crossword.gif" alt="Crossword - script by CrookedBush.com"></p>
HTML
#---------------------------
if($player_name eq "") { &create_game }
if($FROMWEB{'action'} eq 'rules' ){ &print_rules; exit }
if($FROMWEB{'action'} eq 'delete'){ &delete_game; exit }
if($FROMWEB{'action'} eq 'adjust'){ &adjust_score; exit }
if($FROMWEB{'action'} =~ /switch_letters/i){ &switch_letters }
if($FROMWEB{'action'} eq 'viewtray'){ 
    &view_tray;
    $good_pass = "no";
    $tray_gif  = "";
    $waiting_message = "Those are your letters.";
    &draw_board;
    exit }
#---------------------------
  if($gameover eq "true"){
    $good_pass = "no";
    $waiting_message = "This game is over. Congratulations to all!<br>Feel free to start another game.";
    &draw_board;exit;
  }# end of if
#---------------------------
# Check Password
#---------------------------
  chomp($player_pass);
  if(($FROMWEB{'pass'} eq "")|($FROMWEB{'pass'} ne $player_pass)){
    $good_pass = "no";
    $waiting_message = "Waiting for $player_name to move.";
    &draw_board;exit;
  }# end of if
  else {
    $good_pass = "yes";
    $waiting_message = "Welcome $player_name. I hope you got great letters!";
    if($FROMWEB{'action'} eq 'undo'  ){ &undo }
  }# end of else
#---------------------------
# Find out if any word was even made!
#---------------------------
my $word_was_made = 0;
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
 for $column (0..14){
   $form= $row.$column;
   chomp($form);
   if( $FROMWEB{$form} ne "" ){
     $word_was_made = 1;
   }# end of if
  }# next $count
}# next $letter
if($word_was_made == 0) {
  &draw_board; # no word was made... so draw board.
  exit;
}# end of if
#---------------------------
# Check that Points Were Entered
#---------------------------
if (!$FROMWEB{'points'}){
    dienice ("Sorry, $player_name. You forgot to add up your points and include them. Please press BACK on your browser, and enter the points for your word.")
}
#---------------------------
# Check That Player Has Right Letters
#---------------------------
chomp(@letter_tray);
  $l1 = $letter_tray[0]; # put tray letters in new variables
  $l2 = $letter_tray[1];
  $l3 = $letter_tray[2];
  $l4 = $letter_tray[3];
  $l5 = $letter_tray[4];
  $l6 = $letter_tray[5];
  $l7 = $letter_tray[6];
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
  for $column (0..14){
    $test="$row$column";
    chomp($test);
    if($FROMWEB{$test} ne ""){
      $FROMWEB{$test} =~ tr/[A-Z]/[a-z]/;
      if($FROMWEB{$test} eq $l1){# now compare letters to tray
        $$row[$column] = $FROMWEB{$test};
        $l1="~"; # assign tilde to prevent reusing letter
      }# end if
      elsif($FROMWEB{$test} eq $l2){
        $$row[$column] = $FROMWEB{$test};
        $l2="~";
      }
      elsif($FROMWEB{$test} eq $l3){
        $$row[$column] = $FROMWEB{$test};
        $l3="~";
      }
      elsif($FROMWEB{$test} eq $l4){
        $$row[$column] = $FROMWEB{$test};
        $l4="~";
      }
      elsif($FROMWEB{$test} eq $l5){
        $$row[$column] = $FROMWEB{$test};
        $l5="~";
      }
      elsif($FROMWEB{$test} eq $l6){
        $$row[$column] = $FROMWEB{$test};
        $l6="~";
      }
      elsif($FROMWEB{$test} eq $l7){
        $$row[$column] = $FROMWEB{$test};
        $l7="~";
      }
      else{ dienice ("Sorry, $player_name. <b>$FROMWEB{$test}</b> is not in your letter tray (or you've tried to use it too many times).");}
    }# next if FROMWEB is blank
  }# next $count
}# next $letter

&pick_new_letters;

#---------------------------
# Put points and letters in player info
#---------------------------]
&points; # add points to player_points
$player = "player$turn";
$$player[2] = join("&",@letter_tray);
$$player[4] = $player_points;
&write_to_disk;
$mail_message = "It's me, $player_name. I've made a new word using the Virtual Crossword game at $URL$script, and I've made $FROMWEB{'points'} points.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIf you're unhappy with the word, or if the points entered seems wrong ($FROMWEB{'points'}), respond to this e-mail to complain. Once it's agreed that the player's word was wrong, player $turn should login, then UNDO the turn. If the challenger is incorrect, then they should pass their turn with zero points. Do this by clicking 'Swap Letters.'\nPlayer $turn, take your turn.\n\n$FROMWEB{'note_for_players'}\nYours Truly,\n\n$player_name";

$mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);

$display_letters = $$player[2];
$display_letters =~ s/\&/\, /sgi;

&dienice ("Thanks $player_name. Your tiles have been added to the board, and I've selected random letters for you from the letter bag. Your tray now contains $display_letters.");
#---------------------------
# Now move used letters out of @letter_tray and replace 
# with random letters from bag.
#---------------------------
sub pick_new_letters{
my $count = 0;
for $tray_letters(1..7){
  $count = $tray_letters - 1;
  $variable = "l$tray_letters";
  chomp($variable);
  if(($$variable eq "~")or($$variable eq "~")){
    $random_number = rand @letter_bag; # find random letter
    $letter_tray[$count] = $letter_bag[$random_number];
    $letter_bag[$random_number] = "";
    &sort_letter_bag;
  }# end of if
}# end of for $tray_letters
}#end of sub pick_new_letters
#---------------------------
# Swap Letters
#---------------------------
sub switch_letters{
 # first display letters with checkboxes so player can choose letters to switch
 if ($FROMWEB{'action'} eq "switch_letters" && $FROMWEB{'pass'} eq $player_pass){
   print <<HTML;
<center><b>Switch Letters</b><br><blockquote><blockquote>
When you press the button below you will forfeit your turn. Play will pass to the next player. <br>
If you wish to keep your letters but skip your turn, press the button below without selecting any
letters to swap.</blockquote></blockquote>
<b>Letter Bag:</b><br>
  <table width="$board_width">
    <tr>
      <td>
HTML

 $number_in_bag = @letter_bag;

 foreach $letter(@letter_bag){
     print "<img src=\"$graphics_url/-.gif\">\n";
 }#end of foreach
 print "<br>$number_in_bag in letter bag.</td></tr></table><br><b>Your Tray:</b><br>";

   $count=-1;
   foreach $letter (@letter_tray){
   $count++;
     print "<input type=\"checkbox\" name=\"$count$letter\" value=\"change\"><img src=\"$graphics_url/$letter.gif\" alt=\"$letter\">\n";
   }#end of foreach
 print <<HTML;
  <br>
  <input type="hidden" name="action" value="switch_letters_now">
  <onput type="hidden" name="pass"   value="$FROMWEB{'pass'}">
  <input type="submit" value="Skip Turn">
  </center>
  </form><br><hr>
  <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
  </font></body></html>
HTML
   exit;  
 }# end of if action is switch letters
 if ($FROMWEB{'action'} eq "switch_letters_now" || $FROMWEB{'pass'} eq $player_pass){
   # second, if letters have been chosen for a switch, put them in a bag, and replace them with a tilde
   $count = -1;
   foreach $letter (@letter_tray){
     $count++;
     if ($FROMWEB{"$count$letter"} eq "change"){
       push (@temp_letter_bag,$letter);
       $letter_tray[$count] = "~";

       $random_number = rand @letter_bag; # find random letter
       $letter_tray[$count] = $letter_bag[$random_number];
       $letter_bag[$random_number] = "";
       &sort_letter_bag;

     }#end of if letter = change
   }#end of foreach letter
   # put @temp_letter_bag into @letter_bag
     push (@letter_bag, @temp_letter_bag);

   # print a confirmation with new letters and increase turn, save file etc.
   $player = "player$turn";
   $$player[2] = join("&",@letter_tray);
   $$player[4] = $player_points;
   &write_to_disk;
   $mail_message = "It's me, $player_name, writing about our Virtual Crossword game at $URL$script. I've decided to swap my letters instead of making points.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIt's player $turn\'s turn now.\nYours Truly,\n$player_name";
   $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);
   $display_letters = $$player[2];
   $display_letters =~ s/\&/\, /sgi;
   &dienice ("Thanks $player_name. I've selected new random letters for you from the letter bag.<br>They are $display_letters. Your turn is now over.");
 }#end of if action is switch letters now

}# end of sub switch_letters
#---------------------------
#  D R A W   B O A R D
#---------------------------
sub draw_board{
#---------------------------
# Convert "_"  (blank) squares to form information
# and prefix letters with graphic information
#---------------------------
$image_properties = "align=left hspace=0 width=$tile_width height=$tile_height";
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
  for $column (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) {
  $variable = "tile".$row;
  chomp($variable);
  if($good_pass eq "no"){
    $$variable[$column] = "<img src=\"$graphics_url/$$row[$column].gif\" alt=\"$$row[$column]\" $image_properties>";
  }
  else {
    if($$row[$column] eq "_"){
     $$variable[$column] = "<center><input type=\"text\" size=\"1\" maxlength=\"1\" name=\"$row$column\"></center>";
     }
    else { 
    $$variable[$column] = "<img src=\"$graphics_url/$$row[$column].gif\" alt=\"$$row[$column]\" $image_properties>";
     }
    }# Next else
 }# Next $column
}# Next $row
#---------------------------
# Hide Tray if player is not logged in
#---------------------------
if($good_pass eq "no") {
  for $count (0..6){
    $tray_gif = "$tray_gif<img src=\"$graphics_url/-.gif\" $image_properties align=\"center\">"
  }# end of for
}# end of if
else { 
  for $count (0..6){
   $tray_gif="$tray_gif<img src=\"$graphics_url/$letter_tray[$count].gif\" alt=\"$letter_tray[$count]\" $image_properties align=\"center\">"
  }# end of for
}# end of else
 print <<HTML;
<center>$waiting_message</center><p>
<div align="center"><center>
<table border="0" width="$screen_width">
 <tr>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=1&board=$FROMWEB{'board'}">$player1[0]</a>: $player1[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=2&board=$FROMWEB{'board'}">$player2[0]</a>: $player2[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=3&board=$FROMWEB{'board'}">$player3[0]</a>: $player3[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=4&board=$FROMWEB{'board'}">$player4[0]</a>: $player4[4]</td>
 </tr>
</table>
</center></div>
<div align="center"><center><table border="4" bgcolor="#DFC400">
   <tr>
      <td width="100%"><table border="1" cellpadding="2" width="$board_width" bgcolor="#FEF1BC">
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[0]</td>
            <td>$tilerowb[0]</td>
            <td>$tilerowc[0]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[0]</td>
            <td>$tilerowe[0]</td>
            <td>$tilerowf[0]</td>
            <td>$tilerowg[0]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowh[0]</td>
            <td>$tilerowi[0]</td>
            <td>$tilerowj[0]</td>
            <td>$tilerowk[0]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[0]</td>
            <td>$tilerowm[0]</td>
            <td>$tilerown[0]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[0]</td>
         </tr>
         <tr>
            <td>$tilerowa[1]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowb[1]</td>
            <td>$tilerowc[1]</td>
            <td>$tilerowd[1]</td>
            <td>$tilerowe[1]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[1]</td>
            <td>$tilerowg[1]</td>
            <td>$tilerowh[1]</td>
            <td>$tilerowi[1]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[1]</td>
            <td>$tilerowk[1]</td>
            <td>$tilerowl[1]</td>
            <td>$tilerowm[1]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerown[1]</td>
            <td>$tilerowo[1]</td>
         </tr>
         <tr>
            <td>$tilerowa[2]</td>
            <td>$tilerowb[2]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowc[2]</td>
            <td>$tilerowd[2]</td>
            <td>$tilerowe[2]</td>
            <td>$tilerowf[2]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[2]</td>
            <td>$tilerowh[2]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[2]</td>
            <td>$tilerowj[2]</td>
            <td>$tilerowk[2]</td>
            <td>$tilerowl[2]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowm[2]</td>
            <td>$tilerown[2]</td>
            <td>$tilerowo[2]</td>
         </tr>
         <tr>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowa[3]</td>
            <td>$tilerowb[3]</td>
            <td>$tilerowc[3]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowd[3]</td>
            <td>$tilerowe[3]</td>
            <td>$tilerowf[3]</td>
            <td>$tilerowg[3]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowh[3]</td>
            <td>$tilerowi[3]</td>
            <td>$tilerowj[3]</td>
            <td>$tilerowk[3]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowl[3]</td>
            <td>$tilerowm[3]</td>
            <td>$tilerown[3]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowo[3]</td>
         </tr>
         <tr>
            <td>$tilerowa[4]</td>
            <td>$tilerowb[4]</td>
            <td>$tilerowc[4]</td>
            <td>$tilerowd[4]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowe[4]</td>
            <td>$tilerowf[4]</td>
            <td>$tilerowg[4]</td>
            <td>$tilerowh[4]</td>
            <td>$tilerowi[4]</td>
            <td>$tilerowj[4]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowk[4]</td>
            <td>$tilerowl[4]</td>
            <td>$tilerowm[4]</td>
            <td>$tilerown[4]</td>
            <td>$tilerowo[4]</td>
         </tr>
         <tr>
            <td>$tilerowa[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowb[5]</td>
            <td>$tilerowc[5]</td>
            <td>$tilerowd[5]</td>
            <td>$tilerowe[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[5]</td>
            <td>$tilerowg[5]</td>
            <td>$tilerowh[5]</td>
            <td>$tilerowi[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[5]</td>
            <td>$tilerowk[5]</td>
            <td>$tilerowl[5]</td>
            <td>$tilerowm[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerown[5]</td>
            <td>$tilerowo[5]</td>
         </tr>
         <tr>
            <td>$tilerowa[6]</td>
            <td>$tilerowb[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowc[6]</td>
            <td>$tilerowd[6]</td>
            <td>$tilerowe[6]</td>
            <td>$tilerowf[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[6]</td>
            <td>$tilerowh[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[6]</td>
            <td>$tilerowj[6]</td>
            <td>$tilerowk[6]</td>
            <td>$tilerowl[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowm[6]</td>
            <td>$tilerown[6]</td>
            <td>$tilerowo[6]</td>
         </tr>
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[7]</td>
            <td>$tilerowb[7]</td>
            <td>$tilerowc[7]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[7]</td>
            <td>$tilerowe[7]</td>
            <td>$tilerowf[7]</td>
            <td>$tilerowg[7]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowh[7]</td>
            <td>$tilerowi[7]</td>
            <td>$tilerowj[7]</td>
            <td>$tilerowk[7]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[7]</td>
            <td>$tilerowm[7]</td>
            <td>$tilerown[7]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[7]</td>
         </tr>
         <tr>
            <td>$tilerowa[8]</td>
            <td>$tilerowb[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowc[8]</td>
            <td>$tilerowd[8]</td>
            <td>$tilerowe[8]</td>
            <td>$tilerowf[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[8]</td>
            <td>$tilerowh[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[8]</td>
            <td>$tilerowj[8]</td>
            <td>$tilerowk[8]</td>
            <td>$tilerowl[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowm[8]</td>
            <td>$tilerown[8]</td>
            <td>$tilerowo[8]</td>
         </tr>
         <tr>
            <td>$tilerowa[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowb[9]</td>
            <td>$tilerowc[9]</td>
            <td>$tilerowd[9]</td>
            <td>$tilerowe[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[9]</td>
            <td>$tilerowg[9]</td>
            <td>$tilerowh[9]</td>
            <td>$tilerowi[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[9]</td>
            <td>$tilerowk[9]</td>
            <td>$tilerowl[9]</td>
            <td>$tilerowm[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerown[9]</td>
            <td>$tilerowo[9]</td>
         </tr>
         <tr>
            <td>$tilerowa[10]</td>
            <td>$tilerowb[10]</td>
            <td>$tilerowc[10]</td>
            <td>$tilerowd[10]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowe[10]</td>
            <td>$tilerowf[10]</td>
            <td>$tilerowg[10]</td>
            <td>$tilerowh[10]</td>
            <td>$tilerowi[10]</td>
            <td>$tilerowj[10]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowk[10]</td>
            <td>$tilerowl[10]</td>
            <td>$tilerowm[10]</td>
            <td>$tilerown[10]</td>
            <td>$tilerowo[10]</td>
         </tr>
         <tr>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowa[11]</td>
            <td>$tilerowb[11]</td>
            <td>$tilerowc[11]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowd[11]</td>
            <td>$tilerowe[11]</td>
            <td>$tilerowf[11]</td>
            <td>$tilerowg[11]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowh[11]</td>
            <td>$tilerowi[11]</td>
            <td>$tilerowj[11]</td>
            <td>$tilerowk[11]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowl[11]</td>
            <td>$tilerowm[11]</td>
            <td>$tilerown[11]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowo[11]</td>
         </tr>
         <tr>
            <td>$tilerowa[12]</td>
            <td>$tilerowb[12]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowc[12]</td>
            <td>$tilerowd[12]</td>
            <td>$tilerowe[12]</td>
            <td>$tilerowf[12]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[12]</td>
            <td>$tilerowh[12]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[12]</td>
            <td>$tilerowj[12]</td>
            <td>$tilerowk[12]</td>
            <td>$tilerowl[12]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowm[12]</td>
            <td>$tilerown[12]</td>
            <td>$tilerowo[12]</td>
         </tr>
         <tr>
            <td>$tilerowa[13]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowb[13]</td>
            <td>$tilerowc[13]</td>
            <td>$tilerowd[13]</td>
            <td>$tilerowe[13]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[13]</td>
            <td>$tilerowg[13]</td>
            <td>$tilerowh[13]</td>
            <td>$tilerowi[13]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[13]</td>
            <td>$tilerowk[13]</td>
            <td>$tilerowl[13]</td>
            <td>$tilerowm[13]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerown[13]</td>
            <td>$tilerowo[13]</td>
         </tr>
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[14]</td>
            <td>$tilerowb[14]</td>
            <td>$tilerowc[14]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[14]</td>
            <td>$tilerowe[14]</td>
            <td>$tilerowf[14]</td>
            <td>$tilerowg[14]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowh[14]</td>
            <td>$tilerowi[14]</td>
            <td>$tilerowj[14]</td>
            <td>$tilerowk[14]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[14]</td>
            <td>$tilerowm[14]</td>
            <td>$tilerown[14]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[14]</td>
         </tr>
      </table>
   </td>
</tr>
</table>
</center></div>

<!-- Draw Controls -->
<font face="$font_face" size=3>
<div align="center"><center>
 <table align="center" border="0" width="100" bgcolor="#F4C453" cellpadding="0" cellspacing="0">
    <tr>
        <td align="center">
        $tray_gif
        </td>
    </tr>
    <tr><td bgcolor="#B39109" height="4"><table></table></td></tr>
</table>
</center></div><div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0" width="$screen_width">
 <tr>
  <td align="center" width="50%">
HTML
 if($good_pass eq "yes"){
   print<<HTML;
         <br>(Use minus sign for blank tiles)<br>
         <p>Points for this round: <input type="text" size ="12" name="points"><br>
         Note for players:<br><textarea rows="8" cols="30" name="note_for_players"></textarea><br>
         <input type="hidden" name="pass" value="$FROMWEB{'pass'}">
         <input type="submit" value="Place Word"></p></td>
HTML
 }# end of if
 # don't show password box if game is over.
 else{
   if($gameover eq ""){
     print "Password for $player_name: <input type=\"password\" size=\"12\" name=\"pass\"><br>\n";
     print "<input type=\"submit\" value=\"Log In\"></p></td>";
   }# end of if $player_tray
 }# end of else
print <<HTML;
    <td align="left" valign="top" width="50%">
        <font size="2"><br><b>Legend:</b><br>
        Dark red squares: Triple word score<br>
        Light red squares: Double word score<br>
        Dark blue squares: Triple letter score<br>
        Light blue squares: Double letter score<br>
        <br>
        <b>Commands</b><br>
        <a href="$script?action=rules&board=$FROMWEB{'board'}">Rules</a><br>
        <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
        <a href="$script?action=switch_letters&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">Swap&nbsp;Letters/Skip&nbsp;Turn</a><br>
        <a href="$script?action=adjust&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">Adjust Score</a><br>
        <a href="$script?action=undo&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">UNDO Play</a><br>
        <a href="$script?action=delete&board=$FROMWEB{'board'}">Delete Game</a><br>
        </font>
    </td>
  </tr>
</table>
</form>
<center>
    <FORM TARGET="_search" METHOD="GET" ACTION="http://www.dictionary.com/cgi-bin/dict.pl">
    Online Dictionary: <INPUT TYPE=TEXT NAME="term">
    <INPUT TYPE=SUBMIT VALUE="Submit">
   </form>
</center>

<!-- Draw Footer -->
<table border="0" cellpadding="0" cellspacing="0" width="$screen_width">
 <tr>
  <td align="center" width="100%">
    <tr>
     <td> <big><a href="http://www.crazybri.net">CLICK HERE</a> to return to CrazyBri Home Page<br><br></big><font size="1"><i>This site is by no means associated with HASBRO, the copyright holders of the game Scrabbleİ in the USA and Canada. If you are looking for the Official Scrabbleİ site you have come to the wrong site, and their address is
     <a href="http://www.scrabble.com">http://www.scrabble.com</a>.
     <br><br>Scrabbleİ is a registered trademark of J.W. Spear &amp; Son PLC and Hasbro Inc. Any and all uses of the word &quot;Scrabbleİ&quot; on my pages refers to this trademark.
     <br><br>This Web Page in no way is an attempt to confuse the visitor that it is the web page of Hasbro brand Scrabbleİ, although we play and love that game.
     <br><br>Please do not read any further if you are looking for the official Hasbro Scrabbleİ site because this is not it.</i></font></td>
    </tr>
   </table>
  </center></div></font><br><hr>
  <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
 </body>
</html>
HTML
}# End of draw_board subroutine
#---------------------------
# Create a New Game
#---------------------------
sub create_game{
 if($FROMWEB{'name1'} eq ""){
     # print form
     print <<HTML;
     <div align="center"><center>
     <table border="0" width="$screen_width">
       <tr>
         <td align="center"><a href="$URL$script?board=- A_New_Game&number_of_players=2">2 Player Game</a> | 
             <a href="$URL$script?board=- A_New_Game&number_of_players=3">3 Player Game</a> |
             <a href="$URL$script?board=- A_New_Game&number_of_players=4">4 Player Game</a> </td>
       </tr>
       <tr><td width="100%">
       <hr width="$screen_width"><p align="center"><br>
         Choose a name and password for each player, and include a password for them.<br>
         This info will be emailed to them automatically.<br>
         <b>Make yourself PLAYER 1.</b><br>
HTML
     if ($FROMWEB{'number_of_players'}<2){$FROMWEB{'number_of_players'} = $default_number_of_players}

     for $count (1..$FROMWEB{'number_of_players'}){
     print<<HTML;
         Player $count Name: <input type="text" size="15" name="name$count"> E-mail: <input type="text" size="15" name="email$count"> Password: <input type="text" size="7" name="pass$count" ><br>
HTML
     }# end of for

print<<HTML;
        <hr width="$screen_width"><p align="center">
        <input type="hidden" name="number_of_players" value="$FROMWEB{'number_of_players'}">
        <input type="submit" value="Start Game"></p></form></td>
       </tr>
     </table>
     </center></div>
    </font><br><hr>
    <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
 </body>
</html>

HTML
exit;
}# end of if
 else {
  # check to see all fields have been filled
  @name_fields = (name1,name2,name3,name4);
  @email_fields = (email1,email2,email3,email4);
  @pass_fields = (pass1,pass2,pass3,pass4);
  $count = 0;
  if ($FROMWEB{'number_of_players'}<2){$FROMWEB{'number_of_players'} = $default_number_of_players}
  $fields_to_check = $FROMWEB{'number_of_players'}-1;
  for $count (0..$fields_to_check){
      $check_field = $name_fields[$count];
      chomp ($check_field);
      if($FROMWEB{$check_field} eq "" || $FROMWEB{$check_field} =~ /\./i){
           &dienice("There appears to be a problem with a name you've chosen. Periods are not allowed in the name, and all names are required. Check the $check_field box.");
      }# end of if...
      else{
          foreach $swear (@name_filter){
              if ($FROMWEB{$check_field} =~ /$swear/i){&dienice("Please do not use swear words in the name fields - \"$swear\"")}
          }# end of foreach
      }# end of else
  }# next foreach $check_field

  for $count (0..$fields_to_check){
      $check_field = $email_fields[$count];
      if($FROMWEB{$check_field} eq ""){
           &dienice("There appears to be a problem with an email address you've chosen. Check the $check_field box.");
      }# end of if...
  }# next foreach $check_field

  for $count (0..$fields_to_check){
      $check_field = $pass_fields[$count];
      if($FROMWEB{$check_field} eq ""){
           &dienice("There appears to be a problem with a password you've chosen. Check the $check_field box.");
      }# end of if...
  }# next foreach $check_field

   $whole_bag="a&a&a&a&a&a&a&a&a&b&b&c&c&d&d&d&d&e&e&e&e&e&e&e&e&e&e&e&e&f&f&g&g&g&h&h&i&i&i&i&i&i&i&i&i&j&k&l&l&l&l&m&m&n&n&n&n&n&n&o&o&o&o&o&o&o&o&p&p&q&r&r&r&r&r&r&s&s&s&s&t&t&t&t&t&t&u&u&u&u&v&v&w&w&x&y&y&z&-&-";
   @letter_bag = split(/&/, $whole_bag); # This places all tiles in letter bag array

# Calculate number of players and titles of game.
  chomp($FROMWEB{'name4'});
  chomp($FROMWEB{'name3'});
  if (length($FROMWEB{'name4'}) == 0 && length($FROMWEB{'name3'}) > 0){
     $number_of_players = 3; 
     $title = "$FROMWEB{'name1'}, $FROMWEB{'name2'}, and $FROMWEB{'name3'}\'s Game";
     $datafilename = "$FROMWEB{'name1'},_$FROMWEB{'name2'},_and_$FROMWEB{'name3'}\.txt"
  }
  elsif (length($FROMWEB{'name4'}) == 0 && length($FROMWEB{'name3'}) == 0){
     $number_of_players = 2;
     $title = "$FROMWEB{'name1'} and $FROMWEB{'name2'}\'s Game";
     $datafilename = "$FROMWEB{'name1'}_and_$FROMWEB{'name2'}\.txt"
  }
  else{ 
     $number_of_players = 4; 
     $title = "$FROMWEB{'name1'}, $FROMWEB{'name2'}, $FROMWEB{'name3'} and $FROMWEB{'name4'}\'s Game";
     $datafilename = "$FROMWEB{'name1'},_$FROMWEB{'name2'},_$FROMWEB{'name3'},_and_$FROMWEB{'name4'}\.txt"

  }
  $datafilename =~ tr/ /_/;
  $datafilename =~ s/\#//sgi;
  $datafilename =~ s/\*//sgi;
  $datafilename =~ s/\"//sgi;
  $datafilename =~ s/\[//sgi;
  $datafilename =~ s/\]//sgi;
  $datafilename =~ s/\-/dash/sgi;
  $datafilename =~ s/\%/percent/sgi;
  $datafilename =~ s/\!/./sgi;

  # choose random letters
  for $player_number (1..$number_of_players){
     for $count (1..7){ # (number of letters in each tray)
       &sort_letter_bag;
       $random_number = rand @letter_bag; # (choose random number)
       $new_letters[$player_number] = "$new_letters[$player_number]$letter_bag[$random_number]\&"; # put in tray...
       $letter_bag[$random_number] = ""; # ...remove from letter bag
       &sort_letter_bag;
     }# end of for $count
  }# end of for $player_number

   # apply web input and random letters to file
   @player1 = ($FROMWEB{'name1'},$FROMWEB{'email1'},$new_letters[1],$FROMWEB{'pass1'});
   @player2 = ($FROMWEB{'name2'},$FROMWEB{'email2'},$new_letters[2],$FROMWEB{'pass2'});
   @player3 = ($FROMWEB{'name3'},$FROMWEB{'email3'},$new_letters[3],$FROMWEB{'pass3'});
   @player4 = ($FROMWEB{'name4'},$FROMWEB{'email4'},$new_letters[4],$FROMWEB{'pass4'});

   $turn  = 4; # so that on roll over, player one starts

   #set points to zero
   $player1[4]=0; $player2[4]=0; $player3[4]=0; $player4[4]=0;
#---------------------------
# Check to see if game name exists (don't overwrite)
#---------------------------
  # try to open board
  $board_exists = true;
  open (BOARD, "$datapath$datafilename")
      or $board_exists = false;
  close (BOARD);
  if($board_exists eq true){
      &dienice("That board already exists. Please change one of the name fields.");
  }
#---------------------------
# Now save all information to hard disk
#---------------------------
   &write_to_disk;
#---------------------------
# Now send each player a separate email with password
#---------------------------
   $player_mail = $FROMWEB{'email1'};
   foreach $player_number ('1','2','3','4'){
     # chomp($player_number); #commenting this out seems to fix mail problems, and final signup screen problems
     $email = "email$player_number";
     $name  = "name$player_number";
     $pass  = "pass$player_number";
     $recipients = $FROMWEB{$email};
     $display_letters = $new_letters[$player_number];
     $display_letters =~ s/\&/\, /sgi;

     $mail_message = "Hello $FROMWEB{$name},\nYou have been signed up for a Virtual Crossword Game at\n$URL$script.\nYour password is $FROMWEB{$pass}.\nThe letters in your tray are $display_letters\.\nYours,\nThe Administrator\n";
     $mail_result = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);
#     print $mail_result;
   }# end of foreach
   $FROMWEB{'board'} = $datafilename;
   $FROMWEB{'board'} =~ s/\.txt//sgi;
   &dienice("Thanks!  This game has been started, and seven random letters have been chosen for each player.");
 }#end of else
}#end of create_game subroutine 
#---------------------------
# Take empty spots out of letter bag.
#---------------------------
sub sort_letter_bag{
 @sort_letter_bag = @letter_bag;
 chomp (@sort_letter_bag);
 @letter_bag=();
 foreach $item (@sort_letter_bag){
   if($item ne '' ) {
     push(@letter_bag, $item);
   }# end of if
 }# end of foreach $number
}# end of sort_letter_bag subroutine
#---------------------------
# Points subroutine
#---------------------------
sub points{

 $player_points = $player_points + $FROMWEB{'points'};
 # Check for empty bag and an empty tray (all letters were used)
 if(join("",@letter_bag) eq "" && join("",@letter_tray) eq ""){
   &fill_points_array;
   foreach $person ("player1","player2","player3","player4"){
#   chomp ($person);
     if($person ne $player){
       @each_letter = split(/&/, $$person[2]); # each letter in this player's tray
       $remaining_points = 0;
       foreach $temp_letter (@each_letter){
         $remaining_points = $remaining_points + $POINTS{$temp_letter};
       }# end of foreach
       # Take points off their final score
       $$person[4] = $$person[4] - $remaining_points;
       # Add same points to score of player who emptied tray first.
       $player_points = $player_points + $remaining_points;
       $total_remaining_points = $total_remaining_points + $remaining_points;
     }# end of if
   }# end of foreach
   $gameover = "true";
   $$player[4] = $player_points; # put points in array.
   &write_to_disk;
   $mail_message = "Hello $other_player_names. It's me, $player_name. I've made the LAST word for the Virtual Crossword Game at $URL$script which earned me $FROMWEB{'points'} points. Since I have used ALL MY LETTERS, the game is now over. The value of all players remaining letters \($total_remaining_points\) has been added to my score, and your individual scores have been decreased by the points left in your trays.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\n\nCongratulations to all.\n\n$FROMWEB{'note_for_players'}\n\nYours Truly,\n$player_name";
   $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);

   if($notify_finish == 1){
       $mail_message = "Hello Administrator.\n$player_name has just won a Crossword game playing against $other_player_names. Let the game remain on the server for the other players to view before you delete it.\n\n$URL$script?board=$FROMWEB{'board'}&action=delete";
       $mailresult = &sendmail($admin_email, $admin_email, $admin_email, $smtp_server, 'Admin Report - Game Over', $mail_message);
   }#end of if $notify_finish

   &dienice("Congratulations $player_name. You've made the last word, using all the letters in your tray and got $FROMWEB{'points'} points from that word. You have also earned $total_remaining_points points from the other players trays. That puts your game grand total at $player_points. The other players have been informed by email.");
 }# end of if tray and bag are empty
}# end of sub points
#---------------------------
# Fill Points Array
#---------------------------
sub fill_points_array{
 my @letter_values = split(/&/, "a=1&b=3&c=3&d=2&e=1&f=4&g=2&h=4&i=1&j=8&k=5&l=1&m=3&n=1&o=1&p=3&q=10&r=1&s=1&t=1&u=1&v=4&w=4&x=8&y=4&z=10&-=0");
 foreach $chunk (@letter_values) {
   ($name, $value) = split(/=/, $chunk);
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $POINTS{$name} = $value;
 }# end of foreach
}# end of sub fill_points
#---------------------------
# Print Rules to Screen
#---------------------------
sub print_rules{
 print <<HTML;
 <center><div align="center">
 <table border="0" width="$screen_width">
 <FONT FACE="$font_face">
 <h3>Crossword Rules</h3>
 <h4>Game Play</h4><br>
  <ol>
     <LI> The first player combines two or more of his or her letters to form a word and places it on the board to read either across or down with one letter on the center square. Diagonal words are not allowed. 
  <P><LI> Complete your turn by counting and announcing your score for that turn. Then draw as many new letters as you played; always keep seven letters on your rack, as long as there are enough tiles left in the bag.
  <P><LI> Play passes to the left. The second player, and then each in turn, adds one or more letters to those already played to form new words. All letters played on a turn must be placed in one row across or down the board, to form at least one complete word. If, at the same time, they touch others letters in adjacent rows, those must also form complete words, crossword fashion, with all such letters. The player gets full credit for all words formed or modified on his or her turn.
  <P><LI> New words may be formed by: 
       <ul>
         <LI> Adding one or more letters to a word or letters already on the board. 
         <LI> Placing a word at right angles to a word already on the board. The new word must use one of the letters already on the board or must add a letter to it. (See Turns 2, 3 and 4 below.) 
         <LI> Placing a complete word parallel to a word already played so that adjacent letters also form complete words. 
       </ul>
  <P><LI> No tile may be shifted or replaced after it has been played and scored. 
  <P><LI> Blanks: The two blank tiles may be used as any letters. When playing a blank, you must state which letter it represents. It remains that letter for the rest of the game. 
  <P><LI> You may use a turn to exchange all, some, or none of the letters. To do this, place your discarded letter(s) facedown. Draw the same number of letters from the pool, then mix your discarded letter(s) into the pool. This ends your turn.
  <P><LI> Any play may be challenged before the next player starts a turn. If the play challenged is unacceptable, the challenged player takes back his or her tiles and loses that turn. If the play challenged is acceptable, the challenger loses his or her next turn. Consult the dictionary for challenges only. All words made in one play are challenged simultaneously. If any word is unacceptable, then the entire play is unacceptable. Only one turn is lost on any challenge.
  <P><LI> The game ends when all letters have been drawn and one player uses his or her last letter; or when all possible plays have been made. 
 </ol>
 <br><br><h4>Scoring</h4><br>
 <FONT FACE="$font_face">
 <ol>
        <LI> Use a score pad or piece of paper to keep a tally of each player's score, entering it after each turn. The score value of each letter is indicated by a number at the bottom of the tile. The score value of a blank is zero. 
     <P><LI> The score for each turn is the sum of the letter values in each word(s) formed or modified on that turn, plus the additional points obtained from placing letters on Premium Squares. 
     <P><LI> <b>Premium Letter Squares: </b><BR>
                A light blue square doubles the score of a letter placed on it; a dark blue square triples the letter score. 
     <P><LI> <B>Premium Word Squares:</b> <BR>
                The score for an entire word is doubled when one of its letters is placed on a pink square: it is tripled when one of its letters is placed on a red square. Include premiums for double or triple letter values, if any, before doubling or tripling the word score. 
             <P>If a word is formed that covers two premium word squares, the score is doubled and then re-doubled (4 times the letter count), or tripled and then re-tripled (9 times the letter count). NOTE: the center square is a pink square, which doubles the score for the first word. 
     <P><LI> Letter and word premiums count <b>only</b> on the turn in which they are played. On later turns, letters already played on premium squares count at face value. 
     <P><LI> When a blank tile is played on a pink or red square, the value of the word is doubled or tripled, even though the blank itself has no score value. 
     <P><LI> When two or more words are formed in the same play, each is scored. The common letter is counted (with full premium value, if any) for each word. 
     <P><LI> BINGO! If you play seven tiles on a turn, it's a Bingo. You score a premium of 50 points after totaling your score for the turn. 
     <P><LI> Unplayed Letters: When the game ends, each player's score is reduced by the sum of his or her <b>unplayed letters</b>. In addition, if a player has used all of his or her letters, the sum of the other players' unplayed letters is added to that player's score. 
     <P><LI> The player with the highest final score wins the game. In case of a tie, the player with the highest score before adding or deducting unplayed letters wins. 
 </ol>
<br><a href="$script?board=$FROMWEB{'board'}">Click to Return to Board</a><br>
<table></div></center>
</body>
</html>
HTML
}# end of sub_rules
#---------------------------
# Search Directory for Scrabble Boards (.txt)
#---------------------------
sub open_dialog{
 &game_expire; # delete untouched games.
 opendir (FILELIST, "$datapath");
 rewinddir (FILELIST);
 @dirlist =  grep(!/^\.\.?$/, readdir (FILELIST));
 closedir (FILELIST);
 $num_in_dir = @dirlist; # the number of items in @dirlist
 $counter = 0;
 while ($num_in_dir > $counter) {
  @check = split(/\./, $dirlist[$counter]);
   if(($check[1] eq "txt")|($check[1] eq "TXT")){
   push (@directory, $dirlist[$counter]);
   }  # end of if
   $counter++;
 }# end of while
 #---------------------------
 # Display 'open file' dialog if multiple files exist
 #---------------------------
 # @directory contains all txt files on server.
 if(scalar(@directory) > 1){
   print <<HTML;
<HTML>
<HEAD>
<TITLE>Choose Crossword File</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="GET">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <img src="$graphics_url/crossword.gif" alt="Crossword - script by CrookedBush.com"><br>
     <img src="$graphics_url/board_image.jpg" align="right">
     <font face="$font_face" size="2">$top_message<br>If a game goes $days_delete days without play it will be deleted automatically.
     There are $#directory games being played. Select a game below to view it, or to login and play... or choose 'A New Game' if you want to play but are not in a game.: <br></font>
     <br><select name="board" size="1">
HTML
  @directory = sort {uc($a) cmp uc($b)} @directory; # sort case insensitively
  foreach $item(@directory){
    $item =~ s/\.txt//;
    $item_name = $item;
    $item_name =~ tr/_/ /;
    $item =~ s/\.txt/\./;
    print "<option value=\"$item\">$item_name</option>\n";
}# end of foreach $item
    print <<HTML;
         </select><br>
     <font face="$font_face" size=1>(Click, then press the first letter of the board name to speed things up)</font><br>
     <input type="submit" value="Okay"><br>
     </font>
    </td>
   </tr>
    </table><br><font face="$font_face" size=3"><a href="http://www.crazybri.net">CLICK HERE</a> to return to CrazyBri Home Page<br><br></font>"<font face="$font_face" size=1">Version $version. Copyright $copyright CrookedBush.com. All Rights Reserved. International copyright secured.</font>
  </div><p>&nbsp;</p>
  <input type="hidden" name="action" value="$FROMWEB{'action'}">
</form>
<br><br><br>
</body>
</html>
HTML
exit;
 }# end of if $#directory > 1
 else {
 $datafilename = $directory[0];
 }# end of else
}# end of sub open_dialog
#---------------------------
#  WRITE TO DISK SUBROUTINE
#---------------------------
sub write_to_disk{
  # Build string to write to disk
  $write_data = '';
  $write_data = "$write_data$rowa[0]&$rowa[1]&$rowa[2]&$rowa[3]&$rowa[4]&$rowa[5]&$rowa[6]&$rowa[7]&$rowa[8]&$rowa[9]&$rowa[10]&$rowa[11]&$rowa[12]&$rowa[13]&$rowa[14]\n";
  $write_data = "$write_data$rowb[0]&$rowb[1]&$rowb[2]&$rowb[3]&$rowb[4]&$rowb[5]&$rowb[6]&$rowb[7]&$rowb[8]&$rowb[9]&$rowb[10]&$rowb[11]&$rowb[12]&$rowb[13]&$rowb[14]\n";
  $write_data = "$write_data$rowc[0]&$rowc[1]&$rowc[2]&$rowc[3]&$rowc[4]&$rowc[5]&$rowc[6]&$rowc[7]&$rowc[8]&$rowc[9]&$rowc[10]&$rowc[11]&$rowc[12]&$rowc[13]&$rowc[14]\n";
  $write_data = "$write_data$rowd[0]&$rowd[1]&$rowd[2]&$rowd[3]&$rowd[4]&$rowd[5]&$rowd[6]&$rowd[7]&$rowd[8]&$rowd[9]&$rowd[10]&$rowd[11]&$rowd[12]&$rowd[13]&$rowd[14]\n";
  $write_data = "$write_data$rowe[0]&$rowe[1]&$rowe[2]&$rowe[3]&$rowe[4]&$rowe[5]&$rowe[6]&$rowe[7]&$rowe[8]&$rowe[9]&$rowe[10]&$rowe[11]&$rowe[12]&$rowe[13]&$rowe[14]\n";
  $write_data = "$write_data$rowf[0]&$rowf[1]&$rowf[2]&$rowf[3]&$rowf[4]&$rowf[5]&$rowf[6]&$rowf[7]&$rowf[8]&$rowf[9]&$rowf[10]&$rowf[11]&$rowf[12]&$rowf[13]&$rowf[14]\n";
  $write_data = "$write_data$rowg[0]&$rowg[1]&$rowg[2]&$rowg[3]&$rowg[4]&$rowg[5]&$rowg[6]&$rowg[7]&$rowg[8]&$rowg[9]&$rowg[10]&$rowg[11]&$rowg[12]&$rowg[13]&$rowg[14]\n";
  $write_data = "$write_data$rowh[0]&$rowh[1]&$rowh[2]&$rowh[3]&$rowh[4]&$rowh[5]&$rowh[6]&$rowh[7]&$rowh[8]&$rowh[9]&$rowh[10]&$rowh[11]&$rowh[12]&$rowh[13]&$rowh[14]\n";
  $write_data = "$write_data$rowi[0]&$rowi[1]&$rowi[2]&$rowi[3]&$rowi[4]&$rowi[5]&$rowi[6]&$rowi[7]&$rowi[8]&$rowi[9]&$rowi[10]&$rowi[11]&$rowi[12]&$rowi[13]&$rowi[14]\n";
  $write_data = "$write_data$rowj[0]&$rowj[1]&$rowj[2]&$rowj[3]&$rowj[4]&$rowj[5]&$rowj[6]&$rowj[7]&$rowj[8]&$rowj[9]&$rowj[10]&$rowj[11]&$rowj[12]&$rowj[13]&$rowj[14]\n";
  $write_data = "$write_data$rowk[0]&$rowk[1]&$rowk[2]&$rowk[3]&$rowk[4]&$rowk[5]&$rowk[6]&$rowk[7]&$rowk[8]&$rowk[9]&$rowk[10]&$rowk[11]&$rowk[12]&$rowk[13]&$rowk[14]\n";
  $write_data = "$write_data$rowl[0]&$rowl[1]&$rowl[2]&$rowl[3]&$rowl[4]&$rowl[5]&$rowl[6]&$rowl[7]&$rowl[8]&$rowl[9]&$rowl[10]&$rowl[11]&$rowl[12]&$rowl[13]&$rowl[14]\n";
  $write_data = "$write_data$rowm[0]&$rowm[1]&$rowm[2]&$rowm[3]&$rowm[4]&$rowm[5]&$rowm[6]&$rowm[7]&$rowm[8]&$rowm[9]&$rowm[10]&$rowm[11]&$rowm[12]&$rowm[13]&$rowm[14]\n";
  $write_data = "$write_data$rown[0]&$rown[1]&$rown[2]&$rown[3]&$rown[4]&$rown[5]&$rown[6]&$rown[7]&$rown[8]&$rown[9]&$rown[10]&$rown[11]&$rown[12]&$rown[13]&$rown[14]\n";
  $write_data = "$write_data$rowo[0]&$rowo[1]&$rowo[2]&$rowo[3]&$rowo[4]&$rowo[5]&$rowo[6]&$rowo[7]&$rowo[8]&$rowo[9]&$rowo[10]&$rowo[11]&$rowo[12]&$rowo[13]&$rowo[14]\n";
  $write_data = "$write_data$title\n";
  for $count (0..99) {
    if($letter_bag[$count]){
      $write_letter_bag = "$write_letter_bag$letter_bag[$count]\&";
    }
    chomp($write_letter_bag);
  }
  $write_data = "$write_data$write_letter_bag\n";
  $write_data = "$write_data$player1[0]=$player1[1]=$player1[2]=$player1[3]=$player1[4]\n";
  $write_data = "$write_data$player2[0]=$player2[1]=$player2[2]=$player2[3]=$player2[4]\n";
  $write_data = "$write_data$player3[0]=$player3[1]=$player3[2]=$player3[3]=$player3[4]\n";
  $write_data = "$write_data$player4[0]=$player4[1]=$player4[2]=$player4[3]=$player4[4]\n";
#---------------------------
# Set up mail recipients BEFORE increasing turn
#---------------------------
if($turn == 1){
 $recipients = "$player2[1], $player3[1], $player4[1]";
 $other_player_names = "$player2[0], $player3[0] and $player4[0]";
 }
elsif($turn == 2){
 $recipients = "$player1[1], $player3[1], $player4[1]";
 $other_player_names = "$player1[0], $player3[0] and $player4[0]";
 }
elsif($turn == 3){
 $recipients = "$player1[1], $player2[1], $player4[1]";
 $other_player_names = "$player1[0], $player2[0] and $player4[0]";
 }
elsif($turn == 4){
 $recipients = "$player1[1], $player2[1], $player3[1]";
 $other_player_names = "$player1[0], $player2[0] and $player3[0]";
 }
 # turn number
 if    ($player3[0] eq ""){$number_of_players = 2}
 elsif ($player4[0] eq ""){$number_of_players = 3}
 else                     {$number_of_players = 4}
 $turn++;
 if ($turn > $number_of_players) {$turn = 1}
 $write_data .= $turn."\n";
 $write_data .= $gameover;

 # NOW ADD UNDO DATA TO END
 $write_data .= "\n".join("\n",@backup_board_data);
    # TROULESHOOTING: print "<pre>$write_data</pre>";
    # exit;
#---------------------------
# Open file and write to it
#---------------------------
 open (data_file, ">$datapath$datafilename")
   or dienice("I can't open the board data file to write: $datapath$datafilename. Error Code: $!");
  print data_file $write_data;
 close (data_file);
#exit;
}# end of subroutine write_to_disk
#---------------------------
# Open File on Server
#---------------------------
sub open_file{
 open (data_file, "$datapath$datafilename")
   or dienice("I can't open the board data file: $datapath$datafilename. Error Code: $!");
 @all_board_data = <data_file>;
 close (data_file);
 chomp(@all_board_data); # take carriage returns off all of the lines
 @backup_board_data = @all_board_data;
}# end of sub open_file
#---------------------------
# Open Ad File on Server
#---------------------------
sub open_ad_file{
 open (ad_file, "$ad_file")
   or dienice("I can't open the ad file: $ad_file. Error Code: $!");
 $ad_text = <ad_file>;
 close (ad_file);
}# end of sub open_ad_file
#---------------------------
# Send Mail notification to all players
#---------------------------
#--------------------------------
# Sendmail.pm by Milivoj Ivkovic.
#--------------------------------
sub sendmail{
 # error codes below for those who bother to check result codes :)
 # 1 success
 # -1 $smtphost unknown
 # -2 socket() failed
 # -3 connect() failed
 # -4 service not available
 # -5 unspecified communication error
 # -6 local user $to unknown on host $smtp
 # -7 transmission of message failed
 # -8 argument $to empty
 #  Sample call: &sendmail($from, $reply, $to, $smtp, $subject, $message );
 my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;
 $to        =~ s/[ \t]+/, /g;       # pack spaces and add comma
 $fromaddr  =~ s/.*<([^\s]*?)>/$1/; # get from email address
 $replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
 $replyaddr =~ s/^([^\s]+).*/$1/;   # use first address
 $message   =~ s/^\./\.\./gm;       # handle . as first character
 $message   =~ s/\r\n/\n/g;         # handle line ending
 $message   =~ s/\n/\r\n/g;
 $smtp      =~ s/^\s+//g;           # remove spaces around $smtp
 $smtp      =~ s/\s+$//g;
 #--------------------------------
 if (!$to){return(-8);}
 #--------------------------------
 # if administrator wants to be notified of all plays, add his name to $to
 if ($notify_plays == 1){ $to .= $admin_email }
 #--------------------------------
 if ($smtp_server){
   my($proto)    = (getprotobyname('tcp'))[2];
   my($port)     = (getservbyname('smtp', 'tcp'))[2];
   my($smtpaddr) = ($smtp =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) ? pack('C4',$1,$2,$3,$4) : (gethostbyname($smtp))[4];
   if (!defined($smtpaddr)){
     return(-1);
   }#end of if
   if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto)){
     return(-2);
   }#end of if
   if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr))){
     return(-3);
   }#end of if
   my($oldfh) = select(MAIL);
   $| = 1;
   select($oldfh);
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-4);
   }#end of if
   print MAIL "helo $smtp_server\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-5);
   }#end of if
   print MAIL "mail from: <$fromaddr>\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-5);
   }#end of if
   foreach (split(/, /, $to)){
     print MAIL "rcpt to: <$_>\r\n";
     $_ = <MAIL>;
     if (/^[45]/){
       close(MAIL);
       return(-6);
     }#end of if
   }#end of foreach

   print MAIL "data\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close MAIL;
     return(-5);
   }#end of if
 }#end of if
 #--------------------------------
 if ($emailprog){
   open(MAIL,"|$emailprog -t");
 }#end of if

 print MAIL "To: $to\n";
 print MAIL "From: $fromaddr\n";
 print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
 print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
 print MAIL "Subject: $subject\n\n";
 print MAIL "$message\n\n";
 print MAIL "$ad_text";
 print MAIL "\n\n";

 if ($smtp_server ne ""){
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-7);
   }#end of if

   print MAIL "quit\r\n";
   $_ = <MAIL>;
 }#end of if
 close(MAIL);
 return(1);
}#end of sub sendmail
#---------------------------
# Delete Game
#---------------------------
sub delete_game{
 if ($FROMWEB{'board'} eq ""){
   &open_dialog;
 }# end of if
 if ($FROMWEB{'pass'} ne $admin_pass && $FROMWEB{'pass'} ne $player_pass){
     if ($gameover eq ""){$gameover= false}
      print <<HTML;
<HTML>
<HEAD>
<TITLE>Enter Administrator or User Password</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="POST">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <font face="$font_face" size="3"><b>Game over equals: $gameover<br>
     Please enter a valid password to delete this file.<br></font>
     <br>
     <input type="password"  name="pass" value=""><br>
     <input type="hidden" name="action" value="$FROMWEB{'action'}">
     <input type="hidden" name="board" value="$FROMWEB{'board'}">
     <input type="submit" value="Log In"><br>
     <br>
     <font face="$font_face" size=1>This version: $version. <br>
     Version available: $new_version</a><br>
     <br>
     <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
     </font>
    </td>
   </tr>
  </table>
  </div>
</form>
</body>
</html>
HTML
 }# end of if
 else {
   print "<center><font face=\"$font_face\" size='3'><b>Deleting game $FROMWEB{'board'}\.txt</b></font></center>";
   unlink( "$datapath$datafilename" );
   print "<p align='center'><font face=\"$font_face\" size='3'><b>$FROMWEB{'board'}\.txt is DELETED.</b></font><br>";
   print "<a href=\"$script?\">Open&nbsp;New&nbsp;Board</a></p><br>";
 }
 exit;
}# end of sub delete_game

#---------------------------
# Game Expire - if a game isn't used for a period, it is auto deleted
#---------------------------
sub game_expire{
 local(@items, $item);
 opendir (FILELIST, "$datapath");
   @items = grep(/\.txt/,readdir(FILELIST));
 closedir(FILELIST);
 foreach $item (@items){
   if (-M "$datapath$item" > $days_delete && $item ne '- A_New_Game.txt' && $item !~ /Jason/){
     unlink("$datapath$item");
   }#end of if
 }#end of foreach
}#end of sub game expire

#---------------------------
# UNDO Play
#---------------------------
sub undo{

 if ($FROMWEB{'areyousure'} eq "yes"){
     @rowa       = split(/&/, $all_board_data[23]);
     @rowb       = split(/&/, $all_board_data[24]);
     @rowc       = split(/&/, $all_board_data[25]);
     @rowd       = split(/&/, $all_board_data[26]);
     @rowe       = split(/&/, $all_board_data[27]);
     @rowf       = split(/&/, $all_board_data[28]);
     @rowg       = split(/&/, $all_board_data[29]);
     @rowh       = split(/&/, $all_board_data[30]);
     @rowi       = split(/&/, $all_board_data[31]);
     @rowj       = split(/&/, $all_board_data[32]);
     @rowk       = split(/&/, $all_board_data[33]);
     @rowl       = split(/&/, $all_board_data[34]);
     @rowm       = split(/&/, $all_board_data[35]);
     @rown       = split(/&/, $all_board_data[36]);
     @rowo       = split(/&/, $all_board_data[37]);
     $title      = $all_board_data[38];             # This is the title of the game
     @letter_bag = split(/&/, $all_board_data[39]); # This places all tiles in letter bag array
     @player1    = split(/=/, $all_board_data[40]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player2    = split(/=/, $all_board_data[41]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player3    = split(/=/, $all_board_data[42]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player4    = split(/=/, $all_board_data[43]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     $turn       = $all_board_data[44];             # next player number (eg:1,2,3,4) 
     $gameover   = $all_board_data[45];

     # data has been changed
     # now save it
     &write_to_disk;

     # email all users about lost turn
     $mail_message = "It's me, $player_name, writing about our Virtual Crossword game at $URL$script. I performed an UNDO of the last turn.\n";
     $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - UNDO Performed', $mail_message);

     # display current player's board
     return;

 }#end of if you're sure
 elsif ($FROMWEB{'areyousure'} eq ""){
     &dienice("Are you sure you want to undo the last player's letters?  They will lose their turn.<br><a href=\"$URL$script?board=$FROMWEB{'board'}&action=undo&areyousure=yes&pass=$FROMWEB{'pass'}\">Yes I'm certain</a> | <a href=\"$URL$script?board=$FROMWEB{'board'}&action=undo&areyousure=no&pass=$FROMWEB{'pass'}\">Whups, I don't want to do that.</a>");
 }#end of elseif
 else{
     print "<center>Undo Cancelled</center>";
 }# end of else- not sure
}# end of sub undo

#---------------------------
# Adjust Score
#---------------------------
sub adjust_score{
 if ($FROMWEB{'pass'} ne $admin_pass && $FROMWEB{'pass'} ne $player_pass){
      print <<HTML;
<HTML>
<HEAD>
<TITLE>Enter Administrator or Your User Password</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="POST">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <font face="$font_face" size="3">
     Please enter a valid Administrator password (or this player's password) to edit the score.<br></font>
     <br>
     <input type="password"  name="pass" value=""><br>
     <input type="hidden" name="action" value="$FROMWEB{'action'}">
     <input type="hidden" name="board" value="$FROMWEB{'board'}">
     <input type="submit" value="Log In"><br>
     <br>
     <font face="$font_face" size=1>This version: $version. <br>
     Version available: $new_version</a><br>
     <br>
     <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
     </font>
    </td>
   </tr>
  </table>
  </div>
</form>
</body>
</html>
HTML
 }# end of if
 else { # pass is correct
     # check to see if new score is passed, if so save score to file
     if ($FROMWEB{'new_score'}){
         # save score to file
         $player1[4] = $FROMWEB{'player1score'};
         $player2[4] = $FROMWEB{'player2score'};
         $player3[4] = $FROMWEB{'player3score'};
         $player4[4] = $FROMWEB{'player4score'};
         $turn--;# decrease turn number so that saving to disk doesn't mess up whose turn it is
         if ($turn < $number_of_players) {$turn = $number_of_players}

         &write_to_disk;

         # display confirmation
         print "<center><font face=\"$font_face\" size='3'><b>Saving new score(s) to $FROMWEB{'board'}\.txt</b></font><br><br>";
         print "<a href=\"$script?board=$FROMWEB{'board'}\">Click to Return to Board</a><br></center>";
         print "</body></html>";

         $mail_message = "The score has just been adjusted at $URL$script.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIf this is a mistake, then player $turn or the administrator should log in and fix the score. Otherwise, player $turn, take your turn.\n\nYours Truly,\n\nThe Administrator";
         $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Score Adjustment', $mail_message);

     }# end of if new score
     # if not, display scores (all if admin, or just players if player) in input box(es).
     else{
         # display all scores
         print <<HTML;
           <center><div align="center">
           <h2>Score Adjustment</h2>
           <table>
             <tr>
               <td>$player1[0]</td>
               <td><input type="text" size=5 name="player1score" value="$player1[4]"></td>
             </tr>
             <tr>
               <td>$player2[0]</td>
               <td><input type="text" size=5 name="player2score" value="$player2[4]"></td>
             </tr>
             <tr>
               <td>$player3[0]</td>
               <td><input type="text" size=5 name="player3score" value="$player3[4]"></td>
             </tr>
             <tr>
               <td>$player4[0]</td>
               <td><input type="text" size=5 name="player4score" value="$player4[4]"></td>
             </tr>
           </table>
           <input type="hidden" name="new_score" value=1>
           <input type="hidden" name="action" value="adjust">
           <input type="hidden" name="pass" value="$FROMWEB{'pass'}">
           <input type="submit" value="Okay">
           </div></center>
       </form>
HTML

     }# end of else
 }
 exit;

 
}#end of sub adjust score
#---------------------------
# View Letter Tray
#---------------------------
sub view_tray{
 # if password incorrect, ask for users password
 $this_player = "player".$FROMWEB{'player'};
 if ($FROMWEB{'pass'} ne $$this_player[3]){
     #print password dialog
     &dienice (qq~
         <form action="$URL$script" method="POST"><b>View Your Tray</b><br><br>
         <input type="hidden" name="board" value="$FROMWEB{'board'}">
         <input type="hidden" name="action" value="viewtray">
         <input type="hidden" name="player" value="$FROMWEB{'player'}">
         Enter Your Password: <input type="password" name="pass"> <input type=submit value="View Tray">
         </form>~);
 }
 else{
     # print tray
     @letter_tray = split(/&/, $$this_player[2]);
     for $count (0..6){
         $tray_gif .= qq~<img src="$graphics_url/$letter_tray[$count].gif" alt="$letter_tray[$count]">~;
     }# end of for
     print "<center>".$tray_gif."</center>\n";
 }
}
#---------------------------
# Parse Form
#---------------------------
sub parse_form{
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 @input_chunks = split(/&/, $buffer);
 foreach $chunk (@input_chunks) {
   ($name, $value) = split(/=/, $chunk);
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $name =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $FROMWEB{$name} = $value;
 }# end of foreach
 @input_chunks = split(/&/, $ENV{'QUERY_STRING'});
 foreach $chunk (@input_chunks) {
   ($name, $value) = split(/=/, $chunk);
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $name =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $FROMWEB{$name} = $value;
 }# end of foreach
}#parse_form
#---------------------------
# Die Nice
#---------------------------
sub dienice {
    my($errmsg) = @_;
    print <<HTML;
<div align="center"><center>
<table border="0" width="$screen_width"><tr><td width="100%">
   <hr width="$screen_width"><p align="center">
     $errmsg<p>\n
     <b><a href=\"$URL$script?board=$FROMWEB{'board'}\">Return to Board</a></b><br>
   <hr width="$screen_width"><p align="center">
  </tr>
</table>
</center></div>
HTML
 exit;
}# End of dienice subroutine
#!/usr/bin/perl
#---------------------------
#  C R O O K E D B U S H . C O M  -  S C R A B B L E 
#  Designed by Jason Silver.
#  Register Online at www.crookedbush.com
#---------------------------
# This scipt is by no means associated with HASBRO, the copyright holders of the game Scrabbleİ in 
# the USA and Canada. Their address is http://www.scrabble.com. 
# Scrabbleİ is a registered trademark of J.W. Spear & Son PLC and Hasbro Inc. 
#
# Unzip the graphics and save them to your computer,
# then upload them to your server in the directory
# you decide in $graphics_url (below)
#
#---------------------------
# User Defined Variables
#---------------------------
 $URL           = 'http://www.crazybri.net/Skrabble/';# url to script, with trailing slash
 $graphics_url  = 'http://www.crazybri.net/Skrabble/images'; # no trailing slash
 $cgipath       = '/home/trukfixr/public_html/Skrabble/'; # path to this script (from server root) with trailing slash
 $script        = 'crossword.pl'; # name of this script
 $datapath      = 'boards/'; # relative path to data storage place - make sure a trailing slash (eg: 'boards/';)
 $days_delete   = 15; # this is the number of days after an untouched game is deleted automatically.

 $top_message   = '<b> ENJOY the game!</b>';
 $ad_file       = '/home/trukfixr/public_html/Skrabble/ad_file.txt'; # this is included in emails
 $use_ad_file   = 1; # Set this to zero (0) if you don't want text ads sent at the end of each email

# $smtp_server  = "localhost"; # Use either SMTP or $emailprog (SENDMAIL) depending on your system but not both!
 $emailprog     = '/usr/sbin/sendmail'; # your mailer program...
 $admin_email   = 'skrabble@crazybri.net'; # email for recieving notifications
 $notify_plays  = 0; # to receive all play notification with the other players.
 $notify_finish = 1; # to receive game over notification so you can delete complete boards
 $admin_pass    = 'missusmary';

 # words to filter out of the name selection routine. 
 # It now just filters name, not passwords or emails. Be careful, because 'ass' would ban lots of normal words 
 # that have those three letters in it. (sorry that I have to write out these words. I know they're offensive.
 @name_filter   = ("shit","asshole","ass-hole","fuck","penis");

 $screen_width  = 600;
 $tile_height   = 25;
 $tile_width    = 25;
 $board_width   = 500;
 $font_face = "Tahoma, Arial";

 $default_number_of_players = 4; # between 2 and 4
 $allow_fetch   = 1; # NEW METHOD! set this to zero if you are getting unexplained 500 errors. 
                     # This displays the latest version number at bottom of page.
#---------------------------
# Body Tags
#---------------------------
 $BACKCOLOUR = '#202020';
 $TEXTCOLOUR = '#E7A26F';
 $LINKCOLOUR = '#D47E40';
 $VISITLINK  = '#D47E40';
 $ALNKCOLOUR = '#D47E40';

# DON'T CHANGE ANYTHING BELOW THIS LINE
#___________________________
#---------------------------
# Program                                           
#---------------------------
 $version   = "6.30313-2002";
 $copyright = "1999-2003";
 $light_blue_image = "$graphics_url/double_letter.gif";
 $dark_blue_image  = "$graphics_url/triple_letter.gif";
 $light_red_image  = "$graphics_url/double_word.gif";
 $dark_red_image   = "$graphics_url/triple_word.gif";
 srand;    # Allow Random Numbers (not needed for Perl 5.004 and later)

 if($allow_fetch){
     $fetch_url   = "http://www.crookedbush.com/scripts/crossword_version.txt";
     use LWP::Simple;
     unless (defined ($new_version = get $fetch_url)){
         $new_version = "Unavailable"
     }
 }# end of allow fetch
 else{ $new_version = "Unavailable" }

 print "Content-type: text/html\n\n";

 &parse_form;
 if ($use_ad_file){&open_ad_file} # get the ad file off the server
 $datafilename  = "$FROMWEB{'board'}\.txt";
 if($datafilename eq ".txt"){
     &open_dialog;
 }# end of if.
 #---------------------------
 &open_file; # Open Game File on Server
 #---------------------------
 # Fill Game Variables
 #---------------------------
 for $count(0..14){
     $all_board_data[$count] =~ tr/[A-Z]/[a-z]/;
 }# changes case from upper to lower on board only
 @rowa       = split(/&/, $all_board_data[0]);
 @rowb       = split(/&/, $all_board_data[1]);
 @rowc       = split(/&/, $all_board_data[2]);
 @rowd       = split(/&/, $all_board_data[3]);
 @rowe       = split(/&/, $all_board_data[4]);
 @rowf       = split(/&/, $all_board_data[5]);
 @rowg       = split(/&/, $all_board_data[6]);
 @rowh       = split(/&/, $all_board_data[7]);
 @rowi       = split(/&/, $all_board_data[8]);
 @rowj       = split(/&/, $all_board_data[9]);
 @rowk       = split(/&/, $all_board_data[10]);
 @rowl       = split(/&/, $all_board_data[11]);
 @rowm       = split(/&/, $all_board_data[12]);
 @rown       = split(/&/, $all_board_data[13]);
 @rowo       = split(/&/, $all_board_data[14]);
 $title      = $all_board_data[15];             # This is the title of the game
 @letter_bag = split(/&/, $all_board_data[16]); # This places all tiles in letter bag array
 @player1    = split(/=/, $all_board_data[17]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player2    = split(/=/, $all_board_data[18]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player3    = split(/=/, $all_board_data[19]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 @player4    = split(/=/, $all_board_data[20]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
 $turn       = $all_board_data[21];             # next player number (eg:1,2,3,4) 
 $gameover   = $all_board_data[22];

 #---------------------------
 # Assign user information
 #---------------------------
 $player = "player$turn";
 chomp($player);
 $player_name = $$player[0]; # name
 $player_mail = $$player[1]; # email
 $player_tray = $$player[2]; # letter tray
 $player_pass = $$player[3]; # password
 $player_points=$$player[4]; # points
 @letter_tray = split(/&/, $player_tray);
 #---------------------------
 # Print Headers for Page.
 #---------------------------
 print <<HTML;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Crossword Board - $title</title>
</head>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<font face="$font_face" size=3>
<form action="$URL$script" method="POST">
<input type="hidden" name="board" value="$FROMWEB{'board'}">
<p align="center"><img src="$graphics_url/crossword.gif" alt="Crossword - script by CrookedBush.com"></p>
HTML
#---------------------------
if($player_name eq "") { &create_game }
if($FROMWEB{'action'} eq 'rules' ){ &print_rules; exit }
if($FROMWEB{'action'} eq 'delete'){ &delete_game; exit }
if($FROMWEB{'action'} eq 'adjust'){ &adjust_score; exit }
if($FROMWEB{'action'} =~ /switch_letters/i){ &switch_letters }
if($FROMWEB{'action'} eq 'viewtray'){ 
    &view_tray;
    $good_pass = "no";
    $tray_gif  = "";
    $waiting_message = "Those are your letters.";
    &draw_board;
    exit }
#---------------------------
  if($gameover eq "true"){
    $good_pass = "no";
    $waiting_message = "This game is over. Congratulations to all!<br>Feel free to start another game.";
    &draw_board;exit;
  }# end of if
#---------------------------
# Check Password
#---------------------------
  chomp($player_pass);
  if(($FROMWEB{'pass'} eq "")|($FROMWEB{'pass'} ne $player_pass)){
    $good_pass = "no";
    $waiting_message = "Waiting for $player_name to move.";
    &draw_board;exit;
  }# end of if
  else {
    $good_pass = "yes";
    $waiting_message = "Welcome $player_name. I hope you got great letters!";
    if($FROMWEB{'action'} eq 'undo'  ){ &undo }
  }# end of else
#---------------------------
# Find out if any word was even made!
#---------------------------
my $word_was_made = 0;
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
 for $column (0..14){
   $form= $row.$column;
   chomp($form);
   if( $FROMWEB{$form} ne "" ){
     $word_was_made = 1;
   }# end of if
  }# next $count
}# next $letter
if($word_was_made == 0) {
  &draw_board; # no word was made... so draw board.
  exit;
}# end of if
#---------------------------
# Check that Points Were Entered
#---------------------------
if (!$FROMWEB{'points'}){
    dienice ("Sorry, $player_name. You forgot to add up your points and include them. Please press BACK on your browser, and enter the points for your word.")
}
#---------------------------
# Check That Player Has Right Letters
#---------------------------
chomp(@letter_tray);
  $l1 = $letter_tray[0]; # put tray letters in new variables
  $l2 = $letter_tray[1];
  $l3 = $letter_tray[2];
  $l4 = $letter_tray[3];
  $l5 = $letter_tray[4];
  $l6 = $letter_tray[5];
  $l7 = $letter_tray[6];
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
  for $column (0..14){
    $test="$row$column";
    chomp($test);
    if($FROMWEB{$test} ne ""){
      $FROMWEB{$test} =~ tr/[A-Z]/[a-z]/;
      if($FROMWEB{$test} eq $l1){# now compare letters to tray
        $$row[$column] = $FROMWEB{$test};
        $l1="~"; # assign tilde to prevent reusing letter
      }# end if
      elsif($FROMWEB{$test} eq $l2){
        $$row[$column] = $FROMWEB{$test};
        $l2="~";
      }
      elsif($FROMWEB{$test} eq $l3){
        $$row[$column] = $FROMWEB{$test};
        $l3="~";
      }
      elsif($FROMWEB{$test} eq $l4){
        $$row[$column] = $FROMWEB{$test};
        $l4="~";
      }
      elsif($FROMWEB{$test} eq $l5){
        $$row[$column] = $FROMWEB{$test};
        $l5="~";
      }
      elsif($FROMWEB{$test} eq $l6){
        $$row[$column] = $FROMWEB{$test};
        $l6="~";
      }
      elsif($FROMWEB{$test} eq $l7){
        $$row[$column] = $FROMWEB{$test};
        $l7="~";
      }
      else{ dienice ("Sorry, $player_name. <b>$FROMWEB{$test}</b> is not in your letter tray (or you've tried to use it too many times).");}
    }# next if FROMWEB is blank
  }# next $count
}# next $letter

&pick_new_letters;

#---------------------------
# Put points and letters in player info
#---------------------------]
&points; # add points to player_points
$player = "player$turn";
$$player[2] = join("&",@letter_tray);
$$player[4] = $player_points;
&write_to_disk;
$mail_message = "It's me, $player_name. I've made a new word using the Virtual Crossword game at $URL$script, and I've made $FROMWEB{'points'} points.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIf you're unhappy with the word, or if the points entered seems wrong ($FROMWEB{'points'}), respond to this e-mail to complain. Once it's agreed that the player's word was wrong, player $turn should login, then UNDO the turn. If the challenger is incorrect, then they should pass their turn with zero points. Do this by clicking 'Swap Letters.'\nPlayer $turn, take your turn.\n\n$FROMWEB{'note_for_players'}\nYours Truly,\n\n$player_name";

$mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);

$display_letters = $$player[2];
$display_letters =~ s/\&/\, /sgi;

&dienice ("Thanks $player_name. Your tiles have been added to the board, and I've selected random letters for you from the letter bag. Your tray now contains $display_letters.");
#---------------------------
# Now move used letters out of @letter_tray and replace 
# with random letters from bag.
#---------------------------
sub pick_new_letters{
my $count = 0;
for $tray_letters(1..7){
  $count = $tray_letters - 1;
  $variable = "l$tray_letters";
  chomp($variable);
  if(($$variable eq "~")or($$variable eq "~")){
    $random_number = rand @letter_bag; # find random letter
    $letter_tray[$count] = $letter_bag[$random_number];
    $letter_bag[$random_number] = "";
    &sort_letter_bag;
  }# end of if
}# end of for $tray_letters
}#end of sub pick_new_letters
#---------------------------
# Swap Letters
#---------------------------
sub switch_letters{
 # first display letters with checkboxes so player can choose letters to switch
 if ($FROMWEB{'action'} eq "switch_letters" && $FROMWEB{'pass'} eq $player_pass){
   print <<HTML;
<center><b>Switch Letters</b><br><blockquote><blockquote>
When you press the button below you will forfeit your turn. Play will pass to the next player. <br>
If you wish to keep your letters but skip your turn, press the button below without selecting any
letters to swap.</blockquote></blockquote>
<b>Letter Bag:</b><br>
  <table width="$board_width">
    <tr>
      <td>
HTML

 $number_in_bag = @letter_bag;

 foreach $letter(@letter_bag){
     print "<img src=\"$graphics_url/-.gif\">\n";
 }#end of foreach
 print "<br>$number_in_bag in letter bag.</td></tr></table><br><b>Your Tray:</b><br>";

   $count=-1;
   foreach $letter (@letter_tray){
   $count++;
     print "<input type=\"checkbox\" name=\"$count$letter\" value=\"change\"><img src=\"$graphics_url/$letter.gif\" alt=\"$letter\">\n";
   }#end of foreach
 print <<HTML;
  <br>
  <input type="hidden" name="action" value="switch_letters_now">
  <onput type="hidden" name="pass"   value="$FROMWEB{'pass'}">
  <input type="submit" value="Skip Turn">
  </center>
  </form><br><hr>
  <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
  </font></body></html>
HTML
   exit;  
 }# end of if action is switch letters
 if ($FROMWEB{'action'} eq "switch_letters_now" || $FROMWEB{'pass'} eq $player_pass){
   # second, if letters have been chosen for a switch, put them in a bag, and replace them with a tilde
   $count = -1;
   foreach $letter (@letter_tray){
     $count++;
     if ($FROMWEB{"$count$letter"} eq "change"){
       push (@temp_letter_bag,$letter);
       $letter_tray[$count] = "~";

       $random_number = rand @letter_bag; # find random letter
       $letter_tray[$count] = $letter_bag[$random_number];
       $letter_bag[$random_number] = "";
       &sort_letter_bag;

     }#end of if letter = change
   }#end of foreach letter
   # put @temp_letter_bag into @letter_bag
     push (@letter_bag, @temp_letter_bag);

   # print a confirmation with new letters and increase turn, save file etc.
   $player = "player$turn";
   $$player[2] = join("&",@letter_tray);
   $$player[4] = $player_points;
   &write_to_disk;
   $mail_message = "It's me, $player_name, writing about our Virtual Crossword game at $URL$script. I've decided to swap my letters instead of making points.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIt's player $turn\'s turn now.\nYours Truly,\n$player_name";
   $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);
   $display_letters = $$player[2];
   $display_letters =~ s/\&/\, /sgi;
   &dienice ("Thanks $player_name. I've selected new random letters for you from the letter bag.<br>They are $display_letters. Your turn is now over.");
 }#end of if action is switch letters now

}# end of sub switch_letters
#---------------------------
#  D R A W   B O A R D
#---------------------------
sub draw_board{
#---------------------------
# Convert "_"  (blank) squares to form information
# and prefix letters with graphic information
#---------------------------
$image_properties = "align=left hspace=0 width=$tile_width height=$tile_height";
for $row ("rowa","rowb","rowc","rowd","rowe","rowf","rowg","rowh","rowi","rowj","rowk","rowl","rowm","rown","rowo"){
  for $column (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) {
  $variable = "tile".$row;
  chomp($variable);
  if($good_pass eq "no"){
    $$variable[$column] = "<img src=\"$graphics_url/$$row[$column].gif\" alt=\"$$row[$column]\" $image_properties>";
  }
  else {
    if($$row[$column] eq "_"){
     $$variable[$column] = "<center><input type=\"text\" size=\"1\" maxlength=\"1\" name=\"$row$column\"></center>";
     }
    else { 
    $$variable[$column] = "<img src=\"$graphics_url/$$row[$column].gif\" alt=\"$$row[$column]\" $image_properties>";
     }
    }# Next else
 }# Next $column
}# Next $row
#---------------------------
# Hide Tray if player is not logged in
#---------------------------
if($good_pass eq "no") {
  for $count (0..6){
    $tray_gif = "$tray_gif<img src=\"$graphics_url/-.gif\" $image_properties align=\"center\">"
  }# end of for
}# end of if
else { 
  for $count (0..6){
   $tray_gif="$tray_gif<img src=\"$graphics_url/$letter_tray[$count].gif\" alt=\"$letter_tray[$count]\" $image_properties align=\"center\">"
  }# end of for
}# end of else
 print <<HTML;
<center>$waiting_message</center><p>
<div align="center"><center>
<table border="0" width="$screen_width">
 <tr>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=1&board=$FROMWEB{'board'}">$player1[0]</a>: $player1[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=2&board=$FROMWEB{'board'}">$player2[0]</a>: $player2[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=3&board=$FROMWEB{'board'}">$player3[0]</a>: $player3[4]</td>
   <td align="center" width="25%"><a href="$script?action=viewtray&player=4&board=$FROMWEB{'board'}">$player4[0]</a>: $player4[4]</td>
 </tr>
</table>
</center></div>
<div align="center"><center><table border="4" bgcolor="#DFC400">
   <tr>
      <td width="100%"><table border="1" cellpadding="2" width="$board_width" bgcolor="#FEF1BC">
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[0]</td>
            <td>$tilerowb[0]</td>
            <td>$tilerowc[0]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[0]</td>
            <td>$tilerowe[0]</td>
            <td>$tilerowf[0]</td>
            <td>$tilerowg[0]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowh[0]</td>
            <td>$tilerowi[0]</td>
            <td>$tilerowj[0]</td>
            <td>$tilerowk[0]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[0]</td>
            <td>$tilerowm[0]</td>
            <td>$tilerown[0]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[0]</td>
         </tr>
         <tr>
            <td>$tilerowa[1]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowb[1]</td>
            <td>$tilerowc[1]</td>
            <td>$tilerowd[1]</td>
            <td>$tilerowe[1]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[1]</td>
            <td>$tilerowg[1]</td>
            <td>$tilerowh[1]</td>
            <td>$tilerowi[1]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[1]</td>
            <td>$tilerowk[1]</td>
            <td>$tilerowl[1]</td>
            <td>$tilerowm[1]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerown[1]</td>
            <td>$tilerowo[1]</td>
         </tr>
         <tr>
            <td>$tilerowa[2]</td>
            <td>$tilerowb[2]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowc[2]</td>
            <td>$tilerowd[2]</td>
            <td>$tilerowe[2]</td>
            <td>$tilerowf[2]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[2]</td>
            <td>$tilerowh[2]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[2]</td>
            <td>$tilerowj[2]</td>
            <td>$tilerowk[2]</td>
            <td>$tilerowl[2]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowm[2]</td>
            <td>$tilerown[2]</td>
            <td>$tilerowo[2]</td>
         </tr>
         <tr>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowa[3]</td>
            <td>$tilerowb[3]</td>
            <td>$tilerowc[3]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowd[3]</td>
            <td>$tilerowe[3]</td>
            <td>$tilerowf[3]</td>
            <td>$tilerowg[3]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowh[3]</td>
            <td>$tilerowi[3]</td>
            <td>$tilerowj[3]</td>
            <td>$tilerowk[3]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowl[3]</td>
            <td>$tilerowm[3]</td>
            <td>$tilerown[3]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowo[3]</td>
         </tr>
         <tr>
            <td>$tilerowa[4]</td>
            <td>$tilerowb[4]</td>
            <td>$tilerowc[4]</td>
            <td>$tilerowd[4]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowe[4]</td>
            <td>$tilerowf[4]</td>
            <td>$tilerowg[4]</td>
            <td>$tilerowh[4]</td>
            <td>$tilerowi[4]</td>
            <td>$tilerowj[4]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowk[4]</td>
            <td>$tilerowl[4]</td>
            <td>$tilerowm[4]</td>
            <td>$tilerown[4]</td>
            <td>$tilerowo[4]</td>
         </tr>
         <tr>
            <td>$tilerowa[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowb[5]</td>
            <td>$tilerowc[5]</td>
            <td>$tilerowd[5]</td>
            <td>$tilerowe[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[5]</td>
            <td>$tilerowg[5]</td>
            <td>$tilerowh[5]</td>
            <td>$tilerowi[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[5]</td>
            <td>$tilerowk[5]</td>
            <td>$tilerowl[5]</td>
            <td>$tilerowm[5]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerown[5]</td>
            <td>$tilerowo[5]</td>
         </tr>
         <tr>
            <td>$tilerowa[6]</td>
            <td>$tilerowb[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowc[6]</td>
            <td>$tilerowd[6]</td>
            <td>$tilerowe[6]</td>
            <td>$tilerowf[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[6]</td>
            <td>$tilerowh[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[6]</td>
            <td>$tilerowj[6]</td>
            <td>$tilerowk[6]</td>
            <td>$tilerowl[6]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowm[6]</td>
            <td>$tilerown[6]</td>
            <td>$tilerowo[6]</td>
         </tr>
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[7]</td>
            <td>$tilerowb[7]</td>
            <td>$tilerowc[7]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[7]</td>
            <td>$tilerowe[7]</td>
            <td>$tilerowf[7]</td>
            <td>$tilerowg[7]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowh[7]</td>
            <td>$tilerowi[7]</td>
            <td>$tilerowj[7]</td>
            <td>$tilerowk[7]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[7]</td>
            <td>$tilerowm[7]</td>
            <td>$tilerown[7]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[7]</td>
         </tr>
         <tr>
            <td>$tilerowa[8]</td>
            <td>$tilerowb[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowc[8]</td>
            <td>$tilerowd[8]</td>
            <td>$tilerowe[8]</td>
            <td>$tilerowf[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[8]</td>
            <td>$tilerowh[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[8]</td>
            <td>$tilerowj[8]</td>
            <td>$tilerowk[8]</td>
            <td>$tilerowl[8]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowm[8]</td>
            <td>$tilerown[8]</td>
            <td>$tilerowo[8]</td>
         </tr>
         <tr>
            <td>$tilerowa[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowb[9]</td>
            <td>$tilerowc[9]</td>
            <td>$tilerowd[9]</td>
            <td>$tilerowe[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[9]</td>
            <td>$tilerowg[9]</td>
            <td>$tilerowh[9]</td>
            <td>$tilerowi[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[9]</td>
            <td>$tilerowk[9]</td>
            <td>$tilerowl[9]</td>
            <td>$tilerowm[9]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerown[9]</td>
            <td>$tilerowo[9]</td>
         </tr>
         <tr>
            <td>$tilerowa[10]</td>
            <td>$tilerowb[10]</td>
            <td>$tilerowc[10]</td>
            <td>$tilerowd[10]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowe[10]</td>
            <td>$tilerowf[10]</td>
            <td>$tilerowg[10]</td>
            <td>$tilerowh[10]</td>
            <td>$tilerowi[10]</td>
            <td>$tilerowj[10]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowk[10]</td>
            <td>$tilerowl[10]</td>
            <td>$tilerowm[10]</td>
            <td>$tilerown[10]</td>
            <td>$tilerowo[10]</td>
         </tr>
         <tr>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowa[11]</td>
            <td>$tilerowb[11]</td>
            <td>$tilerowc[11]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowd[11]</td>
            <td>$tilerowe[11]</td>
            <td>$tilerowf[11]</td>
            <td>$tilerowg[11]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowh[11]</td>
            <td>$tilerowi[11]</td>
            <td>$tilerowj[11]</td>
            <td>$tilerowk[11]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowl[11]</td>
            <td>$tilerowm[11]</td>
            <td>$tilerown[11]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowo[11]</td>
         </tr>
         <tr>
            <td>$tilerowa[12]</td>
            <td>$tilerowb[12]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowc[12]</td>
            <td>$tilerowd[12]</td>
            <td>$tilerowe[12]</td>
            <td>$tilerowf[12]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowg[12]</td>
            <td>$tilerowh[12]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowi[12]</td>
            <td>$tilerowj[12]</td>
            <td>$tilerowk[12]</td>
            <td>$tilerowl[12]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowm[12]</td>
            <td>$tilerown[12]</td>
            <td>$tilerowo[12]</td>
         </tr>
         <tr>
            <td>$tilerowa[13]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerowb[13]</td>
            <td>$tilerowc[13]</td>
            <td>$tilerowd[13]</td>
            <td>$tilerowe[13]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowf[13]</td>
            <td>$tilerowg[13]</td>
            <td>$tilerowh[13]</td>
            <td>$tilerowi[13]</td>
            <td bgcolor="#0000FF" background="$dark_blue_image">$tilerowj[13]</td>
            <td>$tilerowk[13]</td>
            <td>$tilerowl[13]</td>
            <td>$tilerowm[13]</td>
            <td bgcolor="#FF8080" background="$light_red_image">$tilerown[13]</td>
            <td>$tilerowo[13]</td>
         </tr>
         <tr>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowa[14]</td>
            <td>$tilerowb[14]</td>
            <td>$tilerowc[14]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowd[14]</td>
            <td>$tilerowe[14]</td>
            <td>$tilerowf[14]</td>
            <td>$tilerowg[14]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowh[14]</td>
            <td>$tilerowi[14]</td>
            <td>$tilerowj[14]</td>
            <td>$tilerowk[14]</td>
            <td bgcolor="#8080FF" background="$light_blue_image">$tilerowl[14]</td>
            <td>$tilerowm[14]</td>
            <td>$tilerown[14]</td>
            <td bgcolor="#800000" background="$dark_red_image">$tilerowo[14]</td>
         </tr>
      </table>
   </td>
</tr>
</table>
</center></div>

<!-- Draw Controls -->
<font face="$font_face" size=3>
<div align="center"><center>
 <table align="center" border="0" width="100" bgcolor="#F4C453" cellpadding="0" cellspacing="0">
    <tr>
        <td align="center">
        $tray_gif
        </td>
    </tr>
    <tr><td bgcolor="#B39109" height="4"><table></table></td></tr>
</table>
</center></div><div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0" width="$screen_width">
 <tr>
  <td align="center" width="50%">
HTML
 if($good_pass eq "yes"){
   print<<HTML;
         <br>(Use minus sign for blank tiles)<br>
         <p>Points for this round: <input type="text" size ="12" name="points"><br>
         Note for players:<br><textarea rows="8" cols="30" name="note_for_players"></textarea><br>
         <input type="hidden" name="pass" value="$FROMWEB{'pass'}">
         <input type="submit" value="Place Word"></p></td>
HTML
 }# end of if
 # don't show password box if game is over.
 else{
   if($gameover eq ""){
     print "Password for $player_name: <input type=\"password\" size=\"12\" name=\"pass\"><br>\n";
     print "<input type=\"submit\" value=\"Log In\"></p></td>";
   }# end of if $player_tray
 }# end of else
print <<HTML;
    <td align="left" valign="top" width="50%">
        <font size="2"><br><b>Legend:</b><br>
        Dark red squares: Triple word score<br>
        Light red squares: Double word score<br>
        Dark blue squares: Triple letter score<br>
        Light blue squares: Double letter score<br>
        <br>
        <b>Commands</b><br>
        <a href="$script?action=rules&board=$FROMWEB{'board'}">Rules</a><br>
        <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
        <a href="$script?action=switch_letters&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">Swap&nbsp;Letters/Skip&nbsp;Turn</a><br>
        <a href="$script?action=adjust&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">Adjust Score</a><br>
        <a href="$script?action=undo&pass=$FROMWEB{'pass'}&board=$FROMWEB{'board'}">UNDO Play</a><br>
        <a href="$script?action=delete&board=$FROMWEB{'board'}">Delete Game</a><br>
        </font>
    </td>
  </tr>
</table>
</form>
<center>
    <FORM TARGET="_search" METHOD="GET" ACTION="http://www.dictionary.com/cgi-bin/dict.pl">
    Online Dictionary: <INPUT TYPE=TEXT NAME="term">
    <INPUT TYPE=SUBMIT VALUE="Submit">
   </form>
</center>

<!-- Draw Footer -->
<table border="0" cellpadding="0" cellspacing="0" width="$screen_width">
 <tr>
  <td align="center" width="100%">
    <tr>
     <td> <font size="3"><a href="http://www.crazybri.net">CLICK HERE</a> to return to CrazyBri Home Page<br><br></font><font size="1"><i>This site is by no means associated with HASBRO, the copyright holders of the game Scrabbleİ in the USA and Canada. If you are looking for the Official Scrabbleİ site you have come to the wrong site, and their address is
     <a href="http://www.scrabble.com">http://www.scrabble.com</a>.
     <br><br>Scrabbleİ is a registered trademark of J.W. Spear &amp; Son PLC and Hasbro Inc. Any and all uses of the word &quot;Scrabbleİ&quot; on my pages refers to this trademark.
     <br><br>This Web Page in no way is an attempt to confuse the visitor that it is the web page of Hasbro brand Scrabbleİ, although we play and love that game.
     <br><br>Please do not read any further if you are looking for the official Hasbro Scrabbleİ site because this is not it.</i></font></td>
    </tr>
   </table>
  </center></div></font><br><hr>
  <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
 </body>
</html>
HTML
}# End of draw_board subroutine
#---------------------------
# Create a New Game
#---------------------------
sub create_game{
 if($FROMWEB{'name1'} eq ""){
     # print form
     print <<HTML;
     <div align="center"><center>
     <table border="0" width="$screen_width">
       <tr>
         <td align="center"><a href="$URL$script?board=- A_New_Game&number_of_players=2">2 Player Game</a> | 
             <a href="$URL$script?board=- A_New_Game&number_of_players=3">3 Player Game</a> |
             <a href="$URL$script?board=- A_New_Game&number_of_players=4">4 Player Game</a> </td>
       </tr>
       <tr><td width="100%">
       <hr width="$screen_width"><p align="center"><br>
         Choose a name and password for each player, and include a password for them.<br>
         This info will be emailed to them automatically.<br>
         <b>Make yourself PLAYER 1.</b><br>
HTML
     if ($FROMWEB{'number_of_players'}<2){$FROMWEB{'number_of_players'} = $default_number_of_players}

     for $count (1..$FROMWEB{'number_of_players'}){
     print<<HTML;
         Player $count Name: <input type="text" size="15" name="name$count"> E-mail: <input type="text" size="15" name="email$count"> Password: <input type="text" size="7" name="pass$count" ><br>
HTML
     }# end of for

print<<HTML;
        <hr width="$screen_width"><p align="center">
        <input type="hidden" name="number_of_players" value="$FROMWEB{'number_of_players'}">
        <input type="submit" value="Start Game"></p></form></td>
       </tr>
     </table>
     </center></div>
    </font><br><hr>
    <b>Virtual Crossword Version $version</b><br>Made by <a href="http://www.crookedbush.com/scripts/" target="_new">CrookedBush.com</a><br>
 </body>
</html>

HTML
exit;
}# end of if
 else {
  # check to see all fields have been filled
  @name_fields = (name1,name2,name3,name4);
  @email_fields = (email1,email2,email3,email4);
  @pass_fields = (pass1,pass2,pass3,pass4);
  $count = 0;
  if ($FROMWEB{'number_of_players'}<2){$FROMWEB{'number_of_players'} = $default_number_of_players}
  $fields_to_check = $FROMWEB{'number_of_players'}-1;
  for $count (0..$fields_to_check){
      $check_field = $name_fields[$count];
      chomp ($check_field);
      if($FROMWEB{$check_field} eq "" || $FROMWEB{$check_field} =~ /\./i){
           &dienice("There appears to be a problem with a name you've chosen. Periods are not allowed in the name, and all names are required. Check the $check_field box.");
      }# end of if...
      else{
          foreach $swear (@name_filter){
              if ($FROMWEB{$check_field} =~ /$swear/i){&dienice("Please do not use swear words in the name fields - \"$swear\"")}
          }# end of foreach
      }# end of else
  }# next foreach $check_field

  for $count (0..$fields_to_check){
      $check_field = $email_fields[$count];
      if($FROMWEB{$check_field} eq ""){
           &dienice("There appears to be a problem with an email address you've chosen. Check the $check_field box.");
      }# end of if...
  }# next foreach $check_field

  for $count (0..$fields_to_check){
      $check_field = $pass_fields[$count];
      if($FROMWEB{$check_field} eq ""){
           &dienice("There appears to be a problem with a password you've chosen. Check the $check_field box.");
      }# end of if...
  }# next foreach $check_field

   $whole_bag="a&a&a&a&a&a&a&a&a&b&b&c&c&d&d&d&d&e&e&e&e&e&e&e&e&e&e&e&e&f&f&g&g&g&h&h&i&i&i&i&i&i&i&i&i&j&k&l&l&l&l&m&m&n&n&n&n&n&n&o&o&o&o&o&o&o&o&p&p&q&r&r&r&r&r&r&s&s&s&s&t&t&t&t&t&t&u&u&u&u&v&v&w&w&x&y&y&z&-&-";
   @letter_bag = split(/&/, $whole_bag); # This places all tiles in letter bag array

# Calculate number of players and titles of game.
  chomp($FROMWEB{'name4'});
  chomp($FROMWEB{'name3'});
  if (length($FROMWEB{'name4'}) == 0 && length($FROMWEB{'name3'}) > 0){
     $number_of_players = 3; 
     $title = "$FROMWEB{'name1'}, $FROMWEB{'name2'}, and $FROMWEB{'name3'}\'s Game";
     $datafilename = "$FROMWEB{'name1'},_$FROMWEB{'name2'},_and_$FROMWEB{'name3'}\.txt"
  }
  elsif (length($FROMWEB{'name4'}) == 0 && length($FROMWEB{'name3'}) == 0){
     $number_of_players = 2;
     $title = "$FROMWEB{'name1'} and $FROMWEB{'name2'}\'s Game";
     $datafilename = "$FROMWEB{'name1'}_and_$FROMWEB{'name2'}\.txt"
  }
  else{ 
     $number_of_players = 4; 
     $title = "$FROMWEB{'name1'}, $FROMWEB{'name2'}, $FROMWEB{'name3'} and $FROMWEB{'name4'}\'s Game";
     $datafilename = "$FROMWEB{'name1'},_$FROMWEB{'name2'},_$FROMWEB{'name3'},_and_$FROMWEB{'name4'}\.txt"

  }
  $datafilename =~ tr/ /_/;
  $datafilename =~ s/\#//sgi;
  $datafilename =~ s/\*//sgi;
  $datafilename =~ s/\"//sgi;
  $datafilename =~ s/\[//sgi;
  $datafilename =~ s/\]//sgi;
  $datafilename =~ s/\-/dash/sgi;
  $datafilename =~ s/\%/percent/sgi;
  $datafilename =~ s/\!/./sgi;

  # choose random letters
  for $player_number (1..$number_of_players){
     for $count (1..7){ # (number of letters in each tray)
       &sort_letter_bag;
       $random_number = rand @letter_bag; # (choose random number)
       $new_letters[$player_number] = "$new_letters[$player_number]$letter_bag[$random_number]\&"; # put in tray...
       $letter_bag[$random_number] = ""; # ...remove from letter bag
       &sort_letter_bag;
     }# end of for $count
  }# end of for $player_number

   # apply web input and random letters to file
   @player1 = ($FROMWEB{'name1'},$FROMWEB{'email1'},$new_letters[1],$FROMWEB{'pass1'});
   @player2 = ($FROMWEB{'name2'},$FROMWEB{'email2'},$new_letters[2],$FROMWEB{'pass2'});
   @player3 = ($FROMWEB{'name3'},$FROMWEB{'email3'},$new_letters[3],$FROMWEB{'pass3'});
   @player4 = ($FROMWEB{'name4'},$FROMWEB{'email4'},$new_letters[4],$FROMWEB{'pass4'});

   $turn  = 4; # so that on roll over, player one starts

   #set points to zero
   $player1[4]=0; $player2[4]=0; $player3[4]=0; $player4[4]=0;
#---------------------------
# Check to see if game name exists (don't overwrite)
#---------------------------
  # try to open board
  $board_exists = true;
  open (BOARD, "$datapath$datafilename")
      or $board_exists = false;
  close (BOARD);
  if($board_exists eq true){
      &dienice("That board already exists. Please change one of the name fields.");
  }
#---------------------------
# Now save all information to hard disk
#---------------------------
   &write_to_disk;
#---------------------------
# Now send each player a separate email with password
#---------------------------
   $player_mail = $FROMWEB{'email1'};
   foreach $player_number ('1','2','3','4'){
     # chomp($player_number); #commenting this out seems to fix mail problems, and final signup screen problems
     $email = "email$player_number";
     $name  = "name$player_number";
     $pass  = "pass$player_number";
     $recipients = $FROMWEB{$email};
     $display_letters = $new_letters[$player_number];
     $display_letters =~ s/\&/\, /sgi;

     $mail_message = "Hello $FROMWEB{$name},\nYou have been signed up for a Virtual Crossword Game at\n$URL$script.\nYour password is $FROMWEB{$pass}.\nThe letters in your tray are $display_letters\.\nYours,\nThe Administrator\n";
     $mail_result = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);
#     print $mail_result;
   }# end of foreach
   $FROMWEB{'board'} = $datafilename;
   $FROMWEB{'board'} =~ s/\.txt//sgi;
   &dienice("Thanks!  This game has been started, and seven random letters have been chosen for each player.");
 }#end of else
}#end of create_game subroutine 
#---------------------------
# Take empty spots out of letter bag.
#---------------------------
sub sort_letter_bag{
 @sort_letter_bag = @letter_bag;
 chomp (@sort_letter_bag);
 @letter_bag=();
 foreach $item (@sort_letter_bag){
   if($item ne '' ) {
     push(@letter_bag, $item);
   }# end of if
 }# end of foreach $number
}# end of sort_letter_bag subroutine
#---------------------------
# Points subroutine
#---------------------------
sub points{

 $player_points = $player_points + $FROMWEB{'points'};
 # Check for empty bag and an empty tray (all letters were used)
 if(join("",@letter_bag) eq "" && join("",@letter_tray) eq ""){
   &fill_points_array;
   foreach $person ("player1","player2","player3","player4"){
#   chomp ($person);
     if($person ne $player){
       @each_letter = split(/&/, $$person[2]); # each letter in this player's tray
       $remaining_points = 0;
       foreach $temp_letter (@each_letter){
         $remaining_points = $remaining_points + $POINTS{$temp_letter};
       }# end of foreach
       # Take points off their final score
       $$person[4] = $$person[4] - $remaining_points;
       # Add same points to score of player who emptied tray first.
       $player_points = $player_points + $remaining_points;
       $total_remaining_points = $total_remaining_points + $remaining_points;
     }# end of if
   }# end of foreach
   $gameover = "true";
   $$player[4] = $player_points; # put points in array.
   &write_to_disk;
   $mail_message = "Hello $other_player_names. It's me, $player_name. I've made the LAST word for the Virtual Crossword Game at $URL$script which earned me $FROMWEB{'points'} points. Since I have used ALL MY LETTERS, the game is now over. The value of all players remaining letters \($total_remaining_points\) has been added to my score, and your individual scores have been decreased by the points left in your trays.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\n\nCongratulations to all.\n\n$FROMWEB{'note_for_players'}\n\nYours Truly,\n$player_name";
   $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Update', $mail_message);

   if($notify_finish == 1){
       $mail_message = "Hello Administrator.\n$player_name has just won a Crossword game playing against $other_player_names. Let the game remain on the server for the other players to view before you delete it.\n\n$URL$script?board=$FROMWEB{'board'}&action=delete";
       $mailresult = &sendmail($admin_email, $admin_email, $admin_email, $smtp_server, 'Admin Report - Game Over', $mail_message);
   }#end of if $notify_finish

   &dienice("Congratulations $player_name. You've made the last word, using all the letters in your tray and got $FROMWEB{'points'} points from that word. You have also earned $total_remaining_points points from the other players trays. That puts your game grand total at $player_points. The other players have been informed by email.");
 }# end of if tray and bag are empty
}# end of sub points
#---------------------------
# Fill Points Array
#---------------------------
sub fill_points_array{
 my @letter_values = split(/&/, "a=1&b=3&c=3&d=2&e=1&f=4&g=2&h=4&i=1&j=8&k=5&l=1&m=3&n=1&o=1&p=3&q=10&r=1&s=1&t=1&u=1&v=4&w=4&x=8&y=4&z=10&-=0");
 foreach $chunk (@letter_values) {
   ($name, $value) = split(/=/, $chunk);
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $POINTS{$name} = $value;
 }# end of foreach
}# end of sub fill_points
#---------------------------
# Print Rules to Screen
#---------------------------
sub print_rules{
 print <<HTML;
 <center><div align="center">
 <table border="0" width="$screen_width">
 <FONT FACE="$font_face">
 <h3>Crossword Rules</h3>
 <h4>Game Play</h4><br>
  <ol>
     <LI> The first player combines two or more of his or her letters to form a word and places it on the board to read either across or down with one letter on the center square. Diagonal words are not allowed. 
  <P><LI> Complete your turn by counting and announcing your score for that turn. Then draw as many new letters as you played; always keep seven letters on your rack, as long as there are enough tiles left in the bag.
  <P><LI> Play passes to the left. The second player, and then each in turn, adds one or more letters to those already played to form new words. All letters played on a turn must be placed in one row across or down the board, to form at least one complete word. If, at the same time, they touch others letters in adjacent rows, those must also form complete words, crossword fashion, with all such letters. The player gets full credit for all words formed or modified on his or her turn.
  <P><LI> New words may be formed by: 
       <ul>
         <LI> Adding one or more letters to a word or letters already on the board. 
         <LI> Placing a word at right angles to a word already on the board. The new word must use one of the letters already on the board or must add a letter to it. (See Turns 2, 3 and 4 below.) 
         <LI> Placing a complete word parallel to a word already played so that adjacent letters also form complete words. 
       </ul>
  <P><LI> No tile may be shifted or replaced after it has been played and scored. 
  <P><LI> Blanks: The two blank tiles may be used as any letters. When playing a blank, you must state which letter it represents. It remains that letter for the rest of the game. 
  <P><LI> You may use a turn to exchange all, some, or none of the letters. To do this, place your discarded letter(s) facedown. Draw the same number of letters from the pool, then mix your discarded letter(s) into the pool. This ends your turn.
  <P><LI> Any play may be challenged before the next player starts a turn. If the play challenged is unacceptable, the challenged player takes back his or her tiles and loses that turn. If the play challenged is acceptable, the challenger loses his or her next turn. Consult the dictionary for challenges only. All words made in one play are challenged simultaneously. If any word is unacceptable, then the entire play is unacceptable. Only one turn is lost on any challenge.
  <P><LI> The game ends when all letters have been drawn and one player uses his or her last letter; or when all possible plays have been made. 
 </ol>
 <br><br><h4>Scoring</h4><br>
 <FONT FACE="$font_face">
 <ol>
        <LI> Use a score pad or piece of paper to keep a tally of each player's score, entering it after each turn. The score value of each letter is indicated by a number at the bottom of the tile. The score value of a blank is zero. 
     <P><LI> The score for each turn is the sum of the letter values in each word(s) formed or modified on that turn, plus the additional points obtained from placing letters on Premium Squares. 
     <P><LI> <b>Premium Letter Squares: </b><BR>
                A light blue square doubles the score of a letter placed on it; a dark blue square triples the letter score. 
     <P><LI> <B>Premium Word Squares:</b> <BR>
                The score for an entire word is doubled when one of its letters is placed on a pink square: it is tripled when one of its letters is placed on a red square. Include premiums for double or triple letter values, if any, before doubling or tripling the word score. 
             <P>If a word is formed that covers two premium word squares, the score is doubled and then re-doubled (4 times the letter count), or tripled and then re-tripled (9 times the letter count). NOTE: the center square is a pink square, which doubles the score for the first word. 
     <P><LI> Letter and word premiums count <b>only</b> on the turn in which they are played. On later turns, letters already played on premium squares count at face value. 
     <P><LI> When a blank tile is played on a pink or red square, the value of the word is doubled or tripled, even though the blank itself has no score value. 
     <P><LI> When two or more words are formed in the same play, each is scored. The common letter is counted (with full premium value, if any) for each word. 
     <P><LI> BINGO! If you play seven tiles on a turn, it's a Bingo. You score a premium of 50 points after totaling your score for the turn. 
     <P><LI> Unplayed Letters: When the game ends, each player's score is reduced by the sum of his or her <b>unplayed letters</b>. In addition, if a player has used all of his or her letters, the sum of the other players' unplayed letters is added to that player's score. 
     <P><LI> The player with the highest final score wins the game. In case of a tie, the player with the highest score before adding or deducting unplayed letters wins. 
 </ol>
<br><a href="$script?board=$FROMWEB{'board'}">Click to Return to Board</a><br>
<table></div></center>
</body>
</html>
HTML
}# end of sub_rules
#---------------------------
# Search Directory for Scrabble Boards (.txt)
#---------------------------
sub open_dialog{
 &game_expire; # delete untouched games.
 opendir (FILELIST, "$datapath");
 rewinddir (FILELIST);
 @dirlist =  grep(!/^\.\.?$/, readdir (FILELIST));
 closedir (FILELIST);
 $num_in_dir = @dirlist; # the number of items in @dirlist
 $counter = 0;
 while ($num_in_dir > $counter) {
  @check = split(/\./, $dirlist[$counter]);
   if(($check[1] eq "txt")|($check[1] eq "TXT")){
   push (@directory, $dirlist[$counter]);
   }  # end of if
   $counter++;
 }# end of while
 #---------------------------
 # Display 'open file' dialog if multiple files exist
 #---------------------------
 # @directory contains all txt files on server.
 if(scalar(@directory) > 1){
   print <<HTML;
<HTML>
<HEAD>
<TITLE>Choose Crossword File</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="GET">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <img src="$graphics_url/crossword.gif" alt="Crossword - script by CrookedBush.com"><br>
     <img src="$graphics_url/board_image.jpg" align="right">
     <font face="$font_face" size="2">$top_message<br>If a game goes $days_delete days without play it will be deleted automatically.
     There are $#directory games being played. Select a game below to view it, or to login and play... or choose 'A New Game' if you want to play but are not in a game.: <br></font>
     <br><select name="board" size="1">
HTML
  @directory = sort {uc($a) cmp uc($b)} @directory; # sort case insensitively
  foreach $item(@directory){
    $item =~ s/\.txt//;
    $item_name = $item;
    $item_name =~ tr/_/ /;
    $item =~ s/\.txt/\./;
    print "<option value=\"$item\">$item_name</option>\n";
}# end of foreach $item
    print <<HTML;
         </select><br>
     <font face="$font_face" size=1>(Click, then press the first letter of the board name to speed things up)</font><br>
     <input type="submit" value="Okay"><br>
     </font>
    </td>
   </tr>
  </table><br><br><font face="$font_face" size=1">Version $version. Copyright $copyright CrookedBush.com. All Rights Reserved. International copyright secured.</font>
  </div><p>&nbsp;</p>
  <input type="hidden" name="action" value="$FROMWEB{'action'}">
</form>
<br><br><br>
</body>
</html>
HTML
exit;
 }# end of if $#directory > 1
 else {
 $datafilename = $directory[0];
 }# end of else
}# end of sub open_dialog
#---------------------------
#  WRITE TO DISK SUBROUTINE
#---------------------------
sub write_to_disk{
  # Build string to write to disk
  $write_data = '';
  $write_data = "$write_data$rowa[0]&$rowa[1]&$rowa[2]&$rowa[3]&$rowa[4]&$rowa[5]&$rowa[6]&$rowa[7]&$rowa[8]&$rowa[9]&$rowa[10]&$rowa[11]&$rowa[12]&$rowa[13]&$rowa[14]\n";
  $write_data = "$write_data$rowb[0]&$rowb[1]&$rowb[2]&$rowb[3]&$rowb[4]&$rowb[5]&$rowb[6]&$rowb[7]&$rowb[8]&$rowb[9]&$rowb[10]&$rowb[11]&$rowb[12]&$rowb[13]&$rowb[14]\n";
  $write_data = "$write_data$rowc[0]&$rowc[1]&$rowc[2]&$rowc[3]&$rowc[4]&$rowc[5]&$rowc[6]&$rowc[7]&$rowc[8]&$rowc[9]&$rowc[10]&$rowc[11]&$rowc[12]&$rowc[13]&$rowc[14]\n";
  $write_data = "$write_data$rowd[0]&$rowd[1]&$rowd[2]&$rowd[3]&$rowd[4]&$rowd[5]&$rowd[6]&$rowd[7]&$rowd[8]&$rowd[9]&$rowd[10]&$rowd[11]&$rowd[12]&$rowd[13]&$rowd[14]\n";
  $write_data = "$write_data$rowe[0]&$rowe[1]&$rowe[2]&$rowe[3]&$rowe[4]&$rowe[5]&$rowe[6]&$rowe[7]&$rowe[8]&$rowe[9]&$rowe[10]&$rowe[11]&$rowe[12]&$rowe[13]&$rowe[14]\n";
  $write_data = "$write_data$rowf[0]&$rowf[1]&$rowf[2]&$rowf[3]&$rowf[4]&$rowf[5]&$rowf[6]&$rowf[7]&$rowf[8]&$rowf[9]&$rowf[10]&$rowf[11]&$rowf[12]&$rowf[13]&$rowf[14]\n";
  $write_data = "$write_data$rowg[0]&$rowg[1]&$rowg[2]&$rowg[3]&$rowg[4]&$rowg[5]&$rowg[6]&$rowg[7]&$rowg[8]&$rowg[9]&$rowg[10]&$rowg[11]&$rowg[12]&$rowg[13]&$rowg[14]\n";
  $write_data = "$write_data$rowh[0]&$rowh[1]&$rowh[2]&$rowh[3]&$rowh[4]&$rowh[5]&$rowh[6]&$rowh[7]&$rowh[8]&$rowh[9]&$rowh[10]&$rowh[11]&$rowh[12]&$rowh[13]&$rowh[14]\n";
  $write_data = "$write_data$rowi[0]&$rowi[1]&$rowi[2]&$rowi[3]&$rowi[4]&$rowi[5]&$rowi[6]&$rowi[7]&$rowi[8]&$rowi[9]&$rowi[10]&$rowi[11]&$rowi[12]&$rowi[13]&$rowi[14]\n";
  $write_data = "$write_data$rowj[0]&$rowj[1]&$rowj[2]&$rowj[3]&$rowj[4]&$rowj[5]&$rowj[6]&$rowj[7]&$rowj[8]&$rowj[9]&$rowj[10]&$rowj[11]&$rowj[12]&$rowj[13]&$rowj[14]\n";
  $write_data = "$write_data$rowk[0]&$rowk[1]&$rowk[2]&$rowk[3]&$rowk[4]&$rowk[5]&$rowk[6]&$rowk[7]&$rowk[8]&$rowk[9]&$rowk[10]&$rowk[11]&$rowk[12]&$rowk[13]&$rowk[14]\n";
  $write_data = "$write_data$rowl[0]&$rowl[1]&$rowl[2]&$rowl[3]&$rowl[4]&$rowl[5]&$rowl[6]&$rowl[7]&$rowl[8]&$rowl[9]&$rowl[10]&$rowl[11]&$rowl[12]&$rowl[13]&$rowl[14]\n";
  $write_data = "$write_data$rowm[0]&$rowm[1]&$rowm[2]&$rowm[3]&$rowm[4]&$rowm[5]&$rowm[6]&$rowm[7]&$rowm[8]&$rowm[9]&$rowm[10]&$rowm[11]&$rowm[12]&$rowm[13]&$rowm[14]\n";
  $write_data = "$write_data$rown[0]&$rown[1]&$rown[2]&$rown[3]&$rown[4]&$rown[5]&$rown[6]&$rown[7]&$rown[8]&$rown[9]&$rown[10]&$rown[11]&$rown[12]&$rown[13]&$rown[14]\n";
  $write_data = "$write_data$rowo[0]&$rowo[1]&$rowo[2]&$rowo[3]&$rowo[4]&$rowo[5]&$rowo[6]&$rowo[7]&$rowo[8]&$rowo[9]&$rowo[10]&$rowo[11]&$rowo[12]&$rowo[13]&$rowo[14]\n";
  $write_data = "$write_data$title\n";
  for $count (0..99) {
    if($letter_bag[$count]){
      $write_letter_bag = "$write_letter_bag$letter_bag[$count]\&";
    }
    chomp($write_letter_bag);
  }
  $write_data = "$write_data$write_letter_bag\n";
  $write_data = "$write_data$player1[0]=$player1[1]=$player1[2]=$player1[3]=$player1[4]\n";
  $write_data = "$write_data$player2[0]=$player2[1]=$player2[2]=$player2[3]=$player2[4]\n";
  $write_data = "$write_data$player3[0]=$player3[1]=$player3[2]=$player3[3]=$player3[4]\n";
  $write_data = "$write_data$player4[0]=$player4[1]=$player4[2]=$player4[3]=$player4[4]\n";
#---------------------------
# Set up mail recipients BEFORE increasing turn
#---------------------------
if($turn == 1){
 $recipients = "$player2[1], $player3[1], $player4[1]";
 $other_player_names = "$player2[0], $player3[0] and $player4[0]";
 }
elsif($turn == 2){
 $recipients = "$player1[1], $player3[1], $player4[1]";
 $other_player_names = "$player1[0], $player3[0] and $player4[0]";
 }
elsif($turn == 3){
 $recipients = "$player1[1], $player2[1], $player4[1]";
 $other_player_names = "$player1[0], $player2[0] and $player4[0]";
 }
elsif($turn == 4){
 $recipients = "$player1[1], $player2[1], $player3[1]";
 $other_player_names = "$player1[0], $player2[0] and $player3[0]";
 }
 # turn number
 if    ($player3[0] eq ""){$number_of_players = 2}
 elsif ($player4[0] eq ""){$number_of_players = 3}
 else                     {$number_of_players = 4}
 $turn++;
 if ($turn > $number_of_players) {$turn = 1}
 $write_data .= $turn."\n";
 $write_data .= $gameover;

 # NOW ADD UNDO DATA TO END
 $write_data .= "\n".join("\n",@backup_board_data);
    # TROULESHOOTING: print "<pre>$write_data</pre>";
    # exit;
#---------------------------
# Open file and write to it
#---------------------------
 open (data_file, ">$datapath$datafilename")
   or dienice("I can't open the board data file to write: $datapath$datafilename. Error Code: $!");
  print data_file $write_data;
 close (data_file);
#exit;
}# end of subroutine write_to_disk
#---------------------------
# Open File on Server
#---------------------------
sub open_file{
 open (data_file, "$datapath$datafilename")
   or dienice("I can't open the board data file: $datapath$datafilename. Error Code: $!");
 @all_board_data = <data_file>;
 close (data_file);
 chomp(@all_board_data); # take carriage returns off all of the lines
 @backup_board_data = @all_board_data;
}# end of sub open_file
#---------------------------
# Open Ad File on Server
#---------------------------
sub open_ad_file{
 open (ad_file, "$ad_file")
   or dienice("I can't open the ad file: $ad_file. Error Code: $!");
 $ad_text = <ad_file>;
 close (ad_file);
}# end of sub open_ad_file
#---------------------------
# Send Mail notification to all players
#---------------------------
#--------------------------------
# Sendmail.pm by Milivoj Ivkovic.
#--------------------------------
sub sendmail{
 # error codes below for those who bother to check result codes :)
 # 1 success
 # -1 $smtphost unknown
 # -2 socket() failed
 # -3 connect() failed
 # -4 service not available
 # -5 unspecified communication error
 # -6 local user $to unknown on host $smtp
 # -7 transmission of message failed
 # -8 argument $to empty
 #  Sample call: &sendmail($from, $reply, $to, $smtp, $subject, $message );
 my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;
 $to        =~ s/[ \t]+/, /g;       # pack spaces and add comma
 $fromaddr  =~ s/.*<([^\s]*?)>/$1/; # get from email address
 $replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
 $replyaddr =~ s/^([^\s]+).*/$1/;   # use first address
 $message   =~ s/^\./\.\./gm;       # handle . as first character
 $message   =~ s/\r\n/\n/g;         # handle line ending
 $message   =~ s/\n/\r\n/g;
 $smtp      =~ s/^\s+//g;           # remove spaces around $smtp
 $smtp      =~ s/\s+$//g;
 #--------------------------------
 if (!$to){return(-8);}
 #--------------------------------
 # if administrator wants to be notified of all plays, add his name to $to
 if ($notify_plays == 1){ $to .= $admin_email }
 #--------------------------------
 if ($smtp_server){
   my($proto)    = (getprotobyname('tcp'))[2];
   my($port)     = (getservbyname('smtp', 'tcp'))[2];
   my($smtpaddr) = ($smtp =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) ? pack('C4',$1,$2,$3,$4) : (gethostbyname($smtp))[4];
   if (!defined($smtpaddr)){
     return(-1);
   }#end of if
   if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto)){
     return(-2);
   }#end of if
   if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr))){
     return(-3);
   }#end of if
   my($oldfh) = select(MAIL);
   $| = 1;
   select($oldfh);
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-4);
   }#end of if
   print MAIL "helo $smtp_server\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-5);
   }#end of if
   print MAIL "mail from: <$fromaddr>\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-5);
   }#end of if
   foreach (split(/, /, $to)){
     print MAIL "rcpt to: <$_>\r\n";
     $_ = <MAIL>;
     if (/^[45]/){
       close(MAIL);
       return(-6);
     }#end of if
   }#end of foreach

   print MAIL "data\r\n";
   $_ = <MAIL>;
   if (/^[45]/){
     close MAIL;
     return(-5);
   }#end of if
 }#end of if
 #--------------------------------
 if ($emailprog){
   open(MAIL,"|$emailprog -t");
 }#end of if

 print MAIL "To: $to\n";
 print MAIL "From: $fromaddr\n";
 print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
 print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
 print MAIL "Subject: $subject\n\n";
 print MAIL "$message\n\n";
 print MAIL "$ad_text";
 print MAIL "\n\n";

 if ($smtp_server ne ""){
   $_ = <MAIL>;
   if (/^[45]/){
     close(MAIL);
     return(-7);
   }#end of if

   print MAIL "quit\r\n";
   $_ = <MAIL>;
 }#end of if
 close(MAIL);
 return(1);
}#end of sub sendmail
#---------------------------
# Delete Game
#---------------------------
sub delete_game{
 if ($FROMWEB{'board'} eq ""){
   &open_dialog;
 }# end of if
 if ($FROMWEB{'pass'} ne $admin_pass && $FROMWEB{'pass'} ne $player_pass){
     if ($gameover eq ""){$gameover= false}
      print <<HTML;
<HTML>
<HEAD>
<TITLE>Enter Administrator or User Password</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="POST">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <font face="$font_face" size="3"><b>Game over equals: $gameover<br>
     Please enter a valid password to delete this file.<br></font>
     <br>
     <input type="password"  name="pass" value=""><br>
     <input type="hidden" name="action" value="$FROMWEB{'action'}">
     <input type="hidden" name="board" value="$FROMWEB{'board'}">
     <input type="submit" value="Log In"><br>
     <br>
     <font face="$font_face" size=1>This version: $version. <br>
     Version available: $new_version</a><br>
     <br>
     <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
     </font>
    </td>
   </tr>
  </table>
  </div>
</form>
</body>
</html>
HTML
 }# end of if
 else {
   print "<center><font face=\"$font_face\" size='3'><b>Deleting game $FROMWEB{'board'}\.txt</b></font></center>";
   unlink( "$datapath$datafilename" );
   print "<p align='center'><font face=\"$font_face\" size='3'><b>$FROMWEB{'board'}\.txt is DELETED.</b></font><br>";
   print "<a href=\"$script?\">Open&nbsp;New&nbsp;Board</a></p><br>";
 }
 exit;
}# end of sub delete_game

#---------------------------
# Game Expire - if a game isn't used for a period, it is auto deleted
#---------------------------
sub game_expire{
 local(@items, $item);
 opendir (FILELIST, "$datapath");
   @items = grep(/\.txt/,readdir(FILELIST));
 closedir(FILELIST);
 foreach $item (@items){
   if (-M "$datapath$item" > $days_delete && $item ne '- A_New_Game.txt' && $item !~ /Jason/){
     unlink("$datapath$item");
   }#end of if
 }#end of foreach
}#end of sub game expire

#---------------------------
# UNDO Play
#---------------------------
sub undo{

 if ($FROMWEB{'areyousure'} eq "yes"){
     @rowa       = split(/&/, $all_board_data[23]);
     @rowb       = split(/&/, $all_board_data[24]);
     @rowc       = split(/&/, $all_board_data[25]);
     @rowd       = split(/&/, $all_board_data[26]);
     @rowe       = split(/&/, $all_board_data[27]);
     @rowf       = split(/&/, $all_board_data[28]);
     @rowg       = split(/&/, $all_board_data[29]);
     @rowh       = split(/&/, $all_board_data[30]);
     @rowi       = split(/&/, $all_board_data[31]);
     @rowj       = split(/&/, $all_board_data[32]);
     @rowk       = split(/&/, $all_board_data[33]);
     @rowl       = split(/&/, $all_board_data[34]);
     @rowm       = split(/&/, $all_board_data[35]);
     @rown       = split(/&/, $all_board_data[36]);
     @rowo       = split(/&/, $all_board_data[37]);
     $title      = $all_board_data[38];             # This is the title of the game
     @letter_bag = split(/&/, $all_board_data[39]); # This places all tiles in letter bag array
     @player1    = split(/=/, $all_board_data[40]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player2    = split(/=/, $all_board_data[41]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player3    = split(/=/, $all_board_data[42]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     @player4    = split(/=/, $all_board_data[43]); # [0]=name, [1]=e-mail address, [2]=lettertray [3]=password [4]=points
     $turn       = $all_board_data[44];             # next player number (eg:1,2,3,4) 
     $gameover   = $all_board_data[45];

     # data has been changed
     # now save it
     &write_to_disk;

     # email all users about lost turn
     $mail_message = "It's me, $player_name, writing about our Virtual Crossword game at $URL$script. I performed an UNDO of the last turn.\n";
     $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - UNDO Performed', $mail_message);

     # display current player's board
     return;

 }#end of if you're sure
 elsif ($FROMWEB{'areyousure'} eq ""){
     &dienice("Are you sure you want to undo the last player's letters?  They will lose their turn.<br><a href=\"$URL$script?board=$FROMWEB{'board'}&action=undo&areyousure=yes&pass=$FROMWEB{'pass'}\">Yes I'm certain</a> | <a href=\"$URL$script?board=$FROMWEB{'board'}&action=undo&areyousure=no&pass=$FROMWEB{'pass'}\">Whups, I don't want to do that.</a>");
 }#end of elseif
 else{
     print "<center>Undo Cancelled</center>";
 }# end of else- not sure
}# end of sub undo

#---------------------------
# Adjust Score
#---------------------------
sub adjust_score{
 if ($FROMWEB{'pass'} ne $admin_pass && $FROMWEB{'pass'} ne $player_pass){
      print <<HTML;
<HTML>
<HEAD>
<TITLE>Enter Administrator or Your User Password</TITLE>
</HEAD>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="POST">
 <div align="center"><table border="0" width="$screen_width" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <font face="$font_face" size="3">
     Please enter a valid Administrator password (or this player's password) to edit the score.<br></font>
     <br>
     <input type="password"  name="pass" value=""><br>
     <input type="hidden" name="action" value="$FROMWEB{'action'}">
     <input type="hidden" name="board" value="$FROMWEB{'board'}">
     <input type="submit" value="Log In"><br>
     <br>
     <font face="$font_face" size=1>This version: $version. <br>
     Version available: $new_version</a><br>
     <br>
     <a href="$script?">Open&nbsp;New&nbsp;Board</a><br>
     </font>
    </td>
   </tr>
  </table>
  </div>
</form>
</body>
</html>
HTML
 }# end of if
 else { # pass is correct
     # check to see if new score is passed, if so save score to file
     if ($FROMWEB{'new_score'}){
         # save score to file
         $player1[4] = $FROMWEB{'player1score'};
         $player2[4] = $FROMWEB{'player2score'};
         $player3[4] = $FROMWEB{'player3score'};
         $player4[4] = $FROMWEB{'player4score'};
         $turn--;# decrease turn number so that saving to disk doesn't mess up whose turn it is
         if ($turn < $number_of_players) {$turn = $number_of_players}

         &write_to_disk;

         # display confirmation
         print "<center><font face=\"$font_face\" size='3'><b>Saving new score(s) to $FROMWEB{'board'}\.txt</b></font><br><br>";
         print "<a href=\"$script?board=$FROMWEB{'board'}\">Click to Return to Board</a><br></center>";
         print "</body></html>";

         $mail_message = "The score has just been adjusted at $URL$script.\n Players:\n 1- $player1[0]: $player1[4] $player1[1]\n 2- $player2[0]: $player2[4] $player2[1]\n 3- $player3[0]: $player3[4] $player3[1]\n 4- $player4[0]: $player4[4] $player4[1]\nIf this is a mistake, then player $turn or the administrator should log in and fix the score. Otherwise, player $turn, take your turn.\n\nYours Truly,\n\nThe Administrator";
         $mailresult = &sendmail($player_mail, $player_mail, $recipients, $smtp_server, 'Virtual Crossword - Score Adjustment', $mail_message);

     }# end of if new score
     # if not, display scores (all if admin, or just players if player) in input box(es).
     else{
         # display all scores
         print <<HTML;
           <center><div align="center">
           <h2>Score Adjustment</h2>
           <table>
             <tr>
               <td>$player1[0]</td>
               <td><input type="text" size=5 name="player1score" value="$player1[4]"></td>
             </tr>
             <tr>
               <td>$player2[0]</td>
               <td><input type="text" size=5 name="player2score" value="$player2[4]"></td>
             </tr>
             <tr>
               <td>$player3[0]</td>
               <td><input type="text" size=5 name="player3score" value="$player3[4]"></td>
             </tr>
             <tr>
               <td>$player4[0]</td>
               <td><input type="text" size=5 name="player4score" value="$player4[4]"></td>
             </tr>
           </table>
           <input type="hidden" name="new_score" value=1>
           <input type="hidden" name="action" value="adjust">
           <input type="hidden" name="pass" value="$FROMWEB{'pass'}">
           <input type="submit" value="Okay">
           </div></center>
       </form>
HTML

     }# end of else
 }
 exit;

 
}#end of sub adjust score
#---------------------------
# View Letter Tray
#---------------------------
sub view_tray{
 # if password incorrect, ask for users password
 $this_player = "player".$FROMWEB{'player'};
 if ($FROMWEB{'pass'} ne $$this_player[3]){
     #print password dialog
     &dienice (qq~
         <form action="$URL$script" method="POST"><b>View Your Tray</b><br><br>
         <input type="hidden" name="board" value="$FROMWEB{'board'}">
         <input type="hidden" name="action" value="viewtray">
         <input type="hidden" name="player" value="$FROMWEB{'player'}">
         Enter Your Password: <input type="password" name="pass"> <input type=submit value="View Tray">
         </form>~);
 }
 else{
     # print tray
     @letter_tray = split(/&/, $$this_player[2]);
     for $count (0..6){
         $tray_gif .= qq~<img src="$graphics_url/$letter_tray[$count].gif" alt="$letter_tray[$count]">~;
     }# end of for
     print "<center>".$tray_gif."</center>\n";
 }
}
#---------------------------
# Parse Form
#---------------------------
sub parse_form{
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 @input_chunks = split(/&/, $buffer);
 foreach $chunk (@input_chunks) {
   ($name, $value) = split(/=/, $chunk);
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $name =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $FROMWEB{$name} = $value;
 }# end of foreach
 @input_chunks = split(/&/, $ENV{'QUERY_STRING'});
 foreach $chunk (@input_chunks) {
   ($name, $value) = split(/=/, $chunk);
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $name =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $FROMWEB{$name} = $value;
 }# end of foreach
}#parse_form
#---------------------------
# Die Nice
#---------------------------
sub dienice {
    my($errmsg) = @_;
    print <<HTML;
<div align="center"><center>
<table border="0" width="$screen_width"><tr><td width="100%">
   <hr width="$screen_width"><p align="center">
     $errmsg<p>\n
     <b><a href=\"$URL$script?board=$FROMWEB{'board'}\">Return to Board</a></b><br>
   <hr width="$screen_width"><p align="center">
  </tr>
</table>
</center></div>
HTML
 exit;
}# End of dienice subroutine
