#!/usr/local/bin/perl
#---------------------------------------------------
#  C R O O K E D B U S H . C O M  -  C R O S S W O R D 
#  Designed by Jason Silver.  Please don't 
#  remove this copyright notice.  Thanks!
#  Register Online at www.crookedbush.com and you'll
#  actually GET PAID five bucks!
#---------------------------------------------------
# Version 1.2
# 2000
# 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. Any and all uses of the
# word "Scrabbleİ" on these pages refers to this 
# trademark. 
#
# Unzip the graphics and save them to your computer,
# then upload them to your server in the directory
# you decide in $grpath (below)
#
#---------------------------------------------------
# User Defined Variables
#---------------------------------------------------

 $URL           = 'http://www.yoyoo.com/games/crossword/';# url to script
 $grpath        = '/games/crossword/images/';                 # path for graphics
 $cgipath       = '/games/crossword/'; # path to this script (from server root)
 $script        = 'crossword.cgi';                               # name of this script
 $datapath      = 'boards/';                 # relative path to data storage place
 $emailprog     = '/usr/lib/sendmail';               # your mailer program...
 $admin_email   = 'kspooker@crosswinds.net';                   # leave this blank if you don't want to be informed when people play
 $title_image   = 'http://www.yoyoo.com/games/crossword/images/crossword.gif';

#---------------------------------------------------
# Body Tags
#---------------------------------------------------

 $BACKCOLOUR = '#000000';
 $TEXTCOLOUR = '#FFFFFF';
 $LINKCOLOUR = '#7EBA94';                 # text must be visible over...
 $VISITLINK  = '#339F61';                 # visited link
 $ALNKCOLOUR = '#ff0000';                 # ...$BACK- and $BRDR- colours


# DON'T CHANGE ANYTHING BELOW THIS LINE
#___________________________________________________
#---------------------------------------------------
# Program                                           
#---------------------------------------------------
 $troubleshoot = 0;
 srand;    # Allow Random Numbers (not needed for 5.004 and later)

 $light_blue_image = "$grpath/double_letter.gif";
 $dark_blue_image  = "$grpath/triple_letter.gif";
 $light_red_image  = "$grpath/double_word.gif";
 $dark_red_image   = "$grpath/triple_word.gif";


 print "Content-type: text/html\n\n";               # set up print type for browser

#---------------------------------------------------
# Assign Points to Letters (q=10, a=1, b=3 etc...)
#---------------------------------------------------
 #  @input_chunks = 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 (@input_chunks) {
 #    ($name, $value) = split(/=/, $chunk);
 #     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 #     $POINTS{$name} = $value;
 #   }

#---------------------------------------------------
# Parse Form - take info from web 
#---------------------------------------------------

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 $chunkread(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); 


     $datafilename  = "$FROMWEB{'board'}\.txt";

     if ($datafilename eq ".txt"){
       &open_dialog;
       # $datafilename  = 'board001.txt';

     } # end of if.

#---------------------------------------------------
# Open Data File on Server
#---------------------------------------------------

&open_file;

#---------------------------------------------------
# Assign variables from the all_board_data array 
#---------------------------------------------------
  for $count(0..14){
   $all_board_data[$count] =~ tr/[A-Z]/[a-z]/;
  }# changes case from upper to lower on board only

 @a          = split(/&/, $all_board_data[0]);  # These next arrays (@a-@o) contain 
 @tilea      = split(/&/, $all_board_data[0]);  # letter and blank square 
 @b          = split(/&/, $all_board_data[1]);  # information
 @tileb      = split(/&/, $all_board_data[1]);  # 
 @c          = split(/&/, $all_board_data[2]);  # The @tile are for display only - messy... but...?
 @tilec      = split(/&/, $all_board_data[2]);  # 
 @d          = split(/&/, $all_board_data[3]);  #
 @tiled      = split(/&/, $all_board_data[3]);  #
 @e          = split(/&/, $all_board_data[4]);  #
 @tilee      = split(/&/, $all_board_data[4]);  #
 @f          = split(/&/, $all_board_data[5]);  #
 @tilef      = split(/&/, $all_board_data[5]);  #
 @g          = split(/&/, $all_board_data[6]);  #
 @tileg      = split(/&/, $all_board_data[6]);  #
 @h          = split(/&/, $all_board_data[7]);  #
 @tileh      = split(/&/, $all_board_data[7]);  #
 @i          = split(/&/, $all_board_data[8]);  #
 @tilei      = split(/&/, $all_board_data[8]);  #
 @j          = split(/&/, $all_board_data[9]);  #
 @tilej      = split(/&/, $all_board_data[9]);  #
 @k          = split(/&/, $all_board_data[10]); #
 @tilek      = split(/&/, $all_board_data[10]); #
 @l          = split(/&/, $all_board_data[11]); #
 @tilel      = split(/&/, $all_board_data[11]); #
 @lm         = split(/&/, $all_board_data[12]); # This is @lm because m seems to cause errors.
 @tilelm     = split(/&/, $all_board_data[12]); #
 @n          = split(/&/, $all_board_data[13]); #
 @tilen      = split(/&/, $all_board_data[13]); #
 @o          = split(/&/, $all_board_data[14]); #
 @tileo      = 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) 

#---------------------------------------------------
# Assign user information
#---------------------------------------------------
 
   if     ($turn == 1) {            # If turn contains '1' then give player 1's info
      $player_name = $player1[0];
      $player_mail = $player1[1];
      $player_tray = $player1[2];
      $player_pass = $player1[3];
      $player_points=$player1[4];
      }
   elsif ($turn == 2) {             # If turn contains '1' then give player 1's info
      $player_name = $player2[0];
      $player_mail = $player2[1];
      $player_tray = $player2[2];
      $player_pass = $player2[3];
      $player_points=$player2[4];
      }
   elsif ($turn == 3) {             # If turn contains '1' then give player 1's info
      $player_name = $player3[0];
      $player_mail = $player3[1];
      $player_tray = $player3[2];
      $player_pass = $player3[3];
      $player_points=$player3[4];
      }
   elsif ($turn == 4) {             # If turn contains '1' then give player 1's info
      $player_name = $player4[0];
      $player_mail = $player4[1];
      $player_tray = $player4[2];
      $player_pass = $player4[3];
      $player_points=$player4[4];
      }
   @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 - The Virtual Game Room</title>
</head>
<body bgcolor="$BACKCOLOUR" text="$TEXTCOLOUR" link="$LINKCOLOUR" vlink="$VISITLINK">
<form action="$URL$script" method="POST">
<input type="hidden" name="board" value="$FROMWEB{'board'}">
<p align="center"><img src="$title_image" alt="Crossword - script by CrookedBush.com"></p>

HTML

#---------------------------------------------------
# Check to see if this is first game or a start of new game
#---------------------------------------------------
if ($letter_tray[0] eq "") { 
  &create_game
  }

#---------------------------------------------------
# Check to see if rules are requested
#---------------------------------------------------
if ($FROMWEB{'action'} eq "rules"){
  &print_rules;
  exit;
}

#---------------------------------------------------
# Print current players name and all points.
#---------------------------------------------------

print <<HTML;
<center>Waiting for $player_name to move.</center><p>
<div align="center"><center>

<table border="0" width="600">
    <tr>
        <td align="center" width="25%">$player1[0]: $player1[4]</td>
        <td align="center" width="25%">$player2[0]: $player2[4]</td>
        <td align="center" width="25%">$player3[0]: $player3[4]</td>
        <td align="center" width="25%">$player4[0]: $player4[4]</td>
    </tr>
</table>
</center></div>
HTML

#---------------------------------------------------
# Check Password
#---------------------------------------------------
if ($troubleshoot){print "Checking Password<br>\n";}

  chomp($player_pass);

  if ($FROMWEB{pass} eq ""){ 
       $good_pass = "no";
       &draw_board;exit; 
  }
  if ($FROMWEB{pass} ne $player_pass){
       $good_pass = "no";
       &draw_board;exit;
   }
  else {$good_pass = "yes";}

#---------------------------------------------------
# Find out if any word was even made!
#---------------------------------------------------
if ($troubleshoot){print "Looking For Word<br>\n";}

$word_was_made = 0;

for $letter (a,b,c,d,e,f,g,h,i,j,k,l,lm,n,o){
 for $count (0..14){
     $form="$letter$count";
   if ( $FROMWEB{$form} ne "" ){
      $word_was_made = 1;
   }
  }# next $count
}# next $letter

if ($word_was_made == 0) {
   &draw_board; # no word was made... so draw board.
   exit;
   }

#---------------------------------------------------
# Check that Form Entry Is In Tray
#---------------------------------------------------
if ($troubleshoot){print "Word was made.<br>Checking to see if you have the letters in your tray<br>\n";}

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];

@x_direction = ();
@y_direction = ();

for $letter (a,b,c,d,e,f,g,h,i,j,k,l,lm,n,o){
 for $count (0..14){
     $test="$letter$count";

  if ( $FROMWEB{$test} ne "" ){
       # first change case to lower
       $FROMWEB{'$test'} =~ tr/[A-Z]/[a-z]/;

       # now compare letters to tray
       if    ($FROMWEB{$test} eq $l1){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l1;
         $l1="~";
         #&points;
       }# assign tilde to prevent reusing letter

       elsif ($FROMWEB{$test} eq $l2){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l2;
         $l2="~";
         #&points;
       }

       elsif ($FROMWEB{$test} eq $l3){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l3;
         $l3="~";
         #&points;
       }

       elsif ($FROMWEB{$test} eq $l4){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l4;
         $l4="~";
         #&points;
       }

       elsif ($FROMWEB{$test} eq $l5){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l5;
         $l5="~";
         #&points;
       }

       elsif ($FROMWEB{$test} eq $l6){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l6;
         $l6="~";
         #&points;
       }

       elsif ($FROMWEB{$test} eq $l7){
         $$letter[$count] = $FROMWEB{$test};
         $letter_in=$l7;
         $l7="~";
       }

       else{ dienice ("Sorry, $player_name.  <b>$FROMWEB{$test}</b> is not in your letter tray (or you've tried to use it twice).");}

       }# next if FROMWEB is blank
  }# next $count
}# next $letter

#---------------------------------------------------
# Now move used letters out of @letter_tray and replace 
# with random letters from bag. Also find out if 
# player used all their letters for extra points
#---------------------------------------------------
if ($troubleshoot){print "Picking new letters for your tray.<br>\n";}

 my $all=0;

 if ($l1 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[0] = $letter_bag[$random_number];
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     } 
 if ($l2 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[1] = $letter_bag[$random_number]; 
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     }
 if ($l3 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[2] = $letter_bag[$random_number];
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     }
 if ($l4 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[3] = $letter_bag[$random_number]; 
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     }
 if ($l5 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[4] = $letter_bag[$random_number];
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     }
 if ($l6 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[5] = $letter_bag[$random_number];
     $letter_bag[$random_number]="";
     $all = $all + 1;
     &sort_letter_bag;
     }
 if ($l7 eq "~") {
   # find random letter
     $random_number = rand @letter_bag;
   # switch letters
     $new_letter_for_tray = $letter_bag[$random_number];
     $letter_tray[6] = $letter_bag[$random_number];
     $letter_bag[$random_number] = "";
     $all = $all + 1;
     &sort_letter_bag;
     }
####################################################
# if ($all=7) {$points=points+50;}
# $points=$points*$word_score; # add double word or triple word score
####################################################

#---------------------------------------------------
# Dictionary Check (OFFLINE)
#---------------------------------------------------

#---------------------------------------------------
# @letter_tray and points need to be put in $player(number)
#---------------------------------------------------
if ($troubleshoot){print "Adding points.<br>\n";}

 &points; # add points to player_points

if ($turn == 1){
 $player1[2] = join("&",@letter_tray);
 $player1[4] = $player_points;
 }
if ($turn == 2){
 $player2[2] = join("&",@letter_tray);
 $player2[4] = $player_points;
 }
if ($turn == 3){
 $player3[2] = join("&",@letter_tray);
 $player3[4] = $player_points;
 }
if ($turn == 4){
 $player4[2] = join("&",@letter_tray);
 $player4[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, 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.  Player $turn, take your turn.\nYours Truly,\n$player_name";

 &mail_it;
 &dienice ("Thanks $player_name.  Your tiles have been added to the board, and I've selected random letters for you from the letter bag.");


#---------------------------------------------------
#  WRITE TO DISK SUBROUTINE
#---------------------------------------------------

sub write_to_disk{
if ($troubleshoot){print "Writing to disk.<br>\n";}

#---------------------------------------------------
# Build string to write to disk 
# (so file isn't open too long)
#---------------------------------------------------

   $write_data = '';
   $write_data = "$write_data$a[0]&$a[1]&$a[2]&$a[3]&$a[4]&$a[5]&$a[6]&$a[7]&$a[8]&$a[9]&$a[10]&$a[11]&$a[12]&$a[13]&$a[14]\n";
   $write_data = "$write_data$b[0]&$b[1]&$b[2]&$b[3]&$b[4]&$b[5]&$b[6]&$b[7]&$b[8]&$b[9]&$b[10]&$b[11]&$b[12]&$b[13]&$b[14]\n";
   $write_data = "$write_data$c[0]&$c[1]&$c[2]&$c[3]&$c[4]&$c[5]&$c[6]&$c[7]&$c[8]&$c[9]&$c[10]&$c[11]&$c[12]&$c[13]&$c[14]\n";
   $write_data = "$write_data$d[0]&$d[1]&$d[2]&$d[3]&$d[4]&$d[5]&$d[6]&$d[7]&$d[8]&$d[9]&$d[10]&$d[11]&$d[12]&$d[13]&$d[14]\n";
   $write_data = "$write_data$e[0]&$e[1]&$e[2]&$e[3]&$e[4]&$e[5]&$e[6]&$e[7]&$e[8]&$e[9]&$e[10]&$e[11]&$e[12]&$e[13]&$e[14]\n";
   $write_data = "$write_data$f[0]&$f[1]&$f[2]&$f[3]&$f[4]&$f[5]&$f[6]&$f[7]&$f[8]&$f[9]&$f[10]&$f[11]&$f[12]&$f[13]&$f[14]\n";
   $write_data = "$write_data$g[0]&$g[1]&$g[2]&$g[3]&$g[4]&$g[5]&$g[6]&$g[7]&$g[8]&$g[9]&$g[10]&$g[11]&$g[12]&$g[13]&$g[14]\n";
   $write_data = "$write_data$h[0]&$h[1]&$h[2]&$h[3]&$h[4]&$h[5]&$h[6]&$h[7]&$h[8]&$h[9]&$h[10]&$h[11]&$h[12]&$h[13]&$h[14]\n";
   $write_data = "$write_data$i[0]&$i[1]&$i[2]&$i[3]&$i[4]&$i[5]&$i[6]&$i[7]&$i[8]&$i[9]&$i[10]&$i[11]&$i[12]&$i[13]&$i[14]\n";
   $write_data = "$write_data$j[0]&$j[1]&$j[2]&$j[3]&$j[4]&$j[5]&$j[6]&$j[7]&$j[8]&$j[9]&$j[10]&$j[11]&$j[12]&$j[13]&$j[14]\n";
   $write_data = "$write_data$k[0]&$k[1]&$k[2]&$k[3]&$k[4]&$k[5]&$k[6]&$k[7]&$k[8]&$k[9]&$k[10]&$k[11]&$k[12]&$k[13]&$k[14]\n";
   $write_data = "$write_data$l[0]&$l[1]&$l[2]&$l[3]&$l[4]&$l[5]&$l[6]&$l[7]&$l[8]&$l[9]&$l[10]&$l[11]&$l[12]&$l[13]&$l[14]\n";
   $write_data = "$write_data$lm[0]&$lm[1]&$lm[2]&$lm[3]&$lm[4]&$lm[5]&$lm[6]&$lm[7]&$lm[8]&$lm[9]&$lm[10]&$lm[11]&$lm[12]&$lm[13]&$lm[14]\n";
   $write_data = "$write_data$n[0]&$n[1]&$n[2]&$n[3]&$n[4]&$n[5]&$n[6]&$n[7]&$n[8]&$n[9]&$n[10]&$n[11]&$n[12]&$n[13]&$n[14]\n";
   $write_data = "$write_data$o[0]&$o[1]&$o[2]&$o[3]&$o[4]&$o[5]&$o[6]&$o[7]&$o[8]&$o[9]&$o[10]&$o[11]&$o[12]&$o[13]&$o[14]\n";
   $write_data = "$write_data$title\n";
   for $count (0..99) {
    if ($letter_bag[$count]){
    $write_data = "$write_data$letter_bag[$count]\&";
    }
   }
   $write_data = "$write_data$write_letter_bag";
   $write_data = "$write_data\n$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], $admin_email";
 $other_player_names = "$player2[0], $player3[0] and $player4[0]";
 }
elsif ($turn == 2){
 $recipients = "$player1[1], $player3[1], $player4[1], $admin_email";
 $other_player_names = "$player1[0], $player3[0] and $player4[0]";
 }
elsif ($turn == 3){
 $recipients = "$player1[1], $player2[1], $player4[1], $admin_email";
 $other_player_names = "$player1[0], $player2[0] and $player4[0]";
 }
elsif ($turn == 4){
 $recipients = "$player1[1], $player2[1], $player3[1], $admin_email";
 $other_player_names = "$player1[0], $player2[0] and $player3[0]";
 }

 # turn number
 $turn = $turn + 1;
   if ($turn > 4) {$turn = 1}
   $write_data = "$write_data$turn";

#---------------------------------------------------
# Open file and write to it
#---------------------------------------------------
if ($troubleshoot){print "Writing file to server.<br>\n";}

 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);

}# end of subroutine write_to_disk

#---------------------------------------------------
# Send Mail
# Sends mail notification to all players
#---------------------------------------------------
sub mail_it{
if ($troubleshoot){print "Sending emails to players<br>\n";}

 open(MAIL,"|$emailprog -t")|| dienice("I can't open the mail program: $emailprog.\n");
   print MAIL "To: $recipients \n";
   print MAIL "From: $player_mail \n";
   print MAIL "Subject: Virtual Crossword - Update\n\n";
   print MAIL "$mail_message";
 close MAIL;

 if ($troubleshoot){print "Finished mailing.<br>\n";}

}# end of subroutine mail_it

#---------------------------------------------------
#  D R A W   B O A R D
#---------------------------------------------------

sub draw_board{
if ($troubleshoot){print"Drawing Board<br>\n";}

#---------------------------------------------------
# Convert "_"  (blank) squares to form information
# and prefix letters with graphic information
#---------------------------------------------------

$image_properties = 'align="left" hspace="0" width="25" height="25"'; # potentially change tile size in future

 for $letter (a,b,c,d,e,f,g,h,i,j,k,l,lm,n,o) {
  for $count (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) {
  $variable = "tile$letter";
  if ($good_pass eq "no"){$$variable[$count] = "<img src=\"$grpath$$letter[$count].gif\" $image_properties>";}
   else {
    if ($$letter[$count] eq "_"){
     $$variable[$count] = "<center><input type=\"text\" size=\"1\" maxlength=\"1\" name=\"$letter$count\"></center>";
     }
    else { 
    $$variable[$count] = "<img src=\"$grpath$$letter[$count].gif\" $image_properties>";
     }
    }# Next else
 }# Next $count
}# Next $letter

#---------------------------------------------------
# Hide Tray if player is not logged in
#---------------------------------------------------
if ($troubleshoot){print"Deciding: hide tray? \n";}

if ($good_pass eq "no") {
  if ($troubleshoot){print"Yes<br>Printing...<br>\n";}
  for $count (0..6){
    $tray_gif = "$tray_gif<img src=\"$grpath-.gif\" $image_properties align=\"center\">"
  }
}
else { 
  if ($troubleshoot){print"No<br>Printing...<br>\n";}
  for $count (0..6){
   $tray_gif="$tray_gif<img src=\"$grpath$letter_tray[$count].gif\" $image_properties align=\"center\">"
  }
}

 print <<HTML;

<div align="center"><center><table border="4" bgcolor="#DFC400">
   <tr>
    <td width="100%"><table border="1" cellpadding="2" width="500" bgcolor="#FEF1BC">
     <tr>
      <td bgcolor="#800000" background="$dark_red_image">$tilea[0]</td>
                        <td>$tileb[0]</td>
                        <td>$tilec[0]</td>
                        <td>$tiled[0]</td>
                        <td>$tilee[0]</td>
                        <td>$tilef[0]</td>
                        <td>$tileg[0]</td>
      <td bgcolor="#800000" background="$dark_red_image">$tileh[0]</td>
                        <td>$tilei[0]</td>
                        <td>$tilej[0]</td>
                        <td>$tilek[0]</td>
                        <td>$tilel[0]</td>
                        <td>$tilelm[0]</td>
                        <td>$tilen[0]</td>
      <td bgcolor="#800000" background="$dark_red_image">$tileo[0]</td>
                    </tr>
                    <tr>
                        <td>$tilea[1]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tileb[1]</td>
                        <td>$tilec[1]</td>
                        <td>$tiled[1]</td>
                        <td>$tilee[1]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilef[1]</td>
                        <td>$tileg[1]</td>
                        <td>$tileh[1]</td>
                        <td>$tilei[1]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilej[1]</td>
                        <td>$tilek[1]</td>
                        <td>$tilel[1]</td>
                        <td>$tilelm[1]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilen[1]</td>
                        <td>$tileo[1]</td>
                    </tr>
                    <tr>
                        <td>$tilea[2]</td>
                        <td>$tileb[2]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilec[2]</td>
                        <td>$tiled[2]</td>
                        <td>$tilee[2]</td>
                        <td>$tilef[2]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileg[2]</td>
                        <td>$tileh[2]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilei[2]</td>
                        <td>$tilej[2]</td>
                        <td>$tilek[2]</td>
                        <td>$tilel[2]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilelm[2]</td>
                        <td>$tilen[2]</td>
                        <td>$tileo[2]</td>
                    </tr>
                    <tr>
                        <td>$tilea[3]</td>
                        <td>$tileb[3]</td>
                        <td>$tilec[3]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tiled[3]</td>
                        <td>$tilee[3]</td>
                        <td>$tilef[3]</td>
                        <td>$tileg[3]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileh[3]</td>
                        <td>$tilei[3]</td>
                        <td>$tilej[3]</td>
                        <td>$tilek[3]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilel[3]</td>
                        <td>$tilelm[3]</td>
                        <td>$tilen[3]</td>
                        <td>$tileo[3]</td>
                    </tr>
                    <tr>
                        <td>$tilea[4]</td>
                        <td>$tileb[4]</td>
                        <td>$tilec[4]</td>
                        <td>$tiled[4]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilee[4]</td>
                        <td>$tilef[4]</td>
                        <td>$tileg[4]</td>
                        <td>$tileh[4]</td>
                        <td>$tilei[4]</td>
                        <td>$tilej[4]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilek[4]</td>
                        <td>$tilel[4]</td>
                        <td>$tilelm[4]</td>
                        <td>$tilen[4]</td>
                        <td>$tileo[4]</td>
                    </tr>
                    <tr>
                        <td>$tilea[5]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tileb[5]</td>
                        <td>$tilec[5]</td>
                        <td>$tiled[5]</td>
                        <td>$tilee[5]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilef[5]</td>
                        <td>$tileg[5]</td>
                        <td>$tileh[5]</td>
                        <td>$tilei[5]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilej[5]</td>
                        <td>$tilek[5]</td>
                        <td>$tilel[5]</td>
                        <td>$tilelm[5]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilen[5]</td>
                        <td>$tileo[5]</td>
                    </tr>
                    <tr>
                        <td>$tilea[6]</td>
                        <td>$tileb[6]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilec[6]</td>
                        <td>$tiled[6]</td>
                        <td>$tilee[6]</td>
                        <td>$tilef[6]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileg[6]</td>
                        <td>$tileh[6]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilei[6]</td>
                        <td>$tilej[6]</td>
                        <td>$tilek[6]</td>
                        <td>$tilel[6]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilelm[6]</td>
                        <td>$tilen[6]</td>
                        <td>$tileo[6]</td>
                    </tr>
                    <tr>
      <td bgcolor="#800000" background="$dark_red_image">$tilea[7]</td>
                        <td>$tileb[7]</td>
                        <td>$tilec[7]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tiled[7]</td>
                        <td>$tilee[7]</td>
                        <td>$tilef[7]</td>
                        <td>$tileg[7]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tileh[7]</td>
                        <td>$tilei[7]</td>
                        <td>$tilej[7]</td>
                        <td>$tilek[7]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilel[7]</td>
                        <td>$tilelm[7]</td>
                        <td>$tilen[7]</td>
      <td bgcolor="#800000" background="$dark_red_image">$tileo[7]</td>
                    </tr>
                    <tr>
                        <td>$tilea[8]</td>
                        <td>$tileb[8]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilec[8]</td>
                        <td>$tiled[8]</td>
                        <td>$tilee[8]</td>
                        <td>$tilef[8]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileg[8]</td>
                        <td>$tileh[8]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilei[8]</td>
                        <td>$tilej[8]</td>
                        <td>$tilek[8]</td>
                        <td>$tilel[8]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilelm[8]</td>
                        <td>$tilen[8]</td>
                        <td>$tileo[8]</td>
                    </tr>
                    <tr>
                        <td>$tilea[9]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tileb[9]</td>
                        <td>$tilec[9]</td>
                        <td>$tiled[9]</td>
                        <td>$tilee[9]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilef[9]</td>
                        <td>$tileg[9]</td>
                        <td>$tileh[9]</td>
                        <td>$tilei[9]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilej[9]</td>
                        <td>$tilek[9]</td>
                        <td>$tilel[9]</td>
                        <td>$tilelm[9]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilen[9]</td>
                        <td>$tileo[9]</td>
                    </tr>
                    <tr>
                        <td>$tilea[10]</td>
                        <td>$tileb[10]</td>
                        <td>$tilec[10]</td>
                        <td>$tiled[10]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilee[10]</td>
                        <td>$tilef[10]</td>
                        <td>$tileg[10]</td>
                        <td>$tileh[10]</td>
                        <td>$tilei[10]</td>
                        <td>$tilej[10]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilek[10]</td>
                        <td>$tilel[10]</td>
                        <td>$tilelm[10]</td>
                        <td>$tilen[10]</td>
                        <td>$tileo[10]</td>
                    </tr>
                    <tr>
                        <td>$tilea[11]</td>
                        <td>$tileb[11]</td>
                        <td>$tilec[11]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tiled[11]</td>
                        <td>$tilee[11]</td>
                        <td>$tilef[11]</td>
                        <td>$tileg[11]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileh[11]</td>
                        <td>$tilei[11]</td>
                        <td>$tilej[11]</td>
                        <td>$tilek[11]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilel[11]</td>
                        <td>$tilelm[11]</td>
                        <td>$tilen[11]</td>
                        <td>$tileo[11]</td>
                    </tr>
                    <tr>
                        <td>$tilea[12]</td>
                        <td>$tileb[12]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilec[12]</td>
                        <td>$tiled[12]</td>
                        <td>$tilee[12]</td>
                        <td>$tilef[12]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tileg[12]</td>
                        <td>$tileh[12]</td>
      <td bgcolor="#8080FF" background="$light_blue_image">$tilei[12]</td>
                        <td>$tilej[12]</td>
                        <td>$tilek[12]</td>
                        <td>$tilel[12]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilelm[12]</td>
                        <td>$tilen[12]</td>
                        <td>$tileo[12]</td>
                    </tr>
                    <tr>
                        <td>$tilea[13]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tileb[13]</td>
                        <td>$tilec[13]</td>
                        <td>$tiled[13]</td>
                        <td>$tilee[13]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilef[13]</td>
                        <td>$tileg[13]</td>
                        <td>$tileh[13]</td>
                        <td>$tilei[13]</td>
      <td bgcolor="#0000FF" background="$dark_blue_image">$tilej[13]</td>
                        <td>$tilek[13]</td>
                        <td>$tilel[13]</td>
                        <td>$tilelm[13]</td>
      <td bgcolor="#FF8080" background="$light_red_image">$tilen[13]</td>
                        <td>$tileo[13]</td>
                    </tr>
                    <tr>
      <td bgcolor="#800000" background="$dark_red_image">$tilea[14]</td>
                        <td>$tileb[14]</td> 
                        <td>$tilec[14]</td> 
                        <td>$tiled[14]</td> 
                        <td>$tilee[14]</td> 
                        <td>$tilef[14]</td> 
                        <td>$tileg[14]</td> 
      <td bgcolor="#800000" background="$dark_red_image">$tileh[14]</td> 
                        <td>$tilei[14]</td> 
                        <td>$tilej[14]</td> 
                        <td>$tilek[14]</td> 
                        <td>$tilel[14]</td>
                        <td>$tilelm[14]</td>
                        <td>$tilen[14]</td>
      <td bgcolor="#800000" background="$dark_red_image">$tileo[14]</td>
                    </tr>
                </table>
                </td>
            </tr>
        </table>
        </center></div>

<!-------------------------------------------

               Draw Controls 

-------------------------------------------->

<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="600">
 <tr>
  <td align="center" width="100%">

HTML

 if ($FROMWEB{'pass'}){
   print"<br>(Use minus sign for blank tiles)<br>
         <p>Points for this round: <input type=\"text\" size =\"12\" name=\"points\">\n";
   print "<input type=\"hidden\" name=\"pass\" value=\"$FROMWEB{'pass'}\">";
 } # end of if

 else {

   print "Password for $player_name: <input type=\"password\" size=\"12\" name=\"pass\">\n";
 } # end of else
 
 print <<HTML;

       <input type="submit" value="Log In"></p>
    <br><a href="$script?action=rules&board=$FROMWEB{'board'}">Rules</a> |
        <a href="$script">Open New Board</a>
   </form>

   <FORM METHOD="GET" ACTION="http://www.hasbroscrabble.com/cgi-bin/define.cgi">
    Online Dictionary: <INPUT TYPE=TEXT NAME="TO-DEFINE">
    <INPUT TYPE=SUBMIT VALUE="Submit">
   </form>

  </td>
 </tr>

<!-------------------------------------------

               Draw Footer 

-------------------------------------------->

    <tr>
     <td><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 </i</font>
     <a href="http://www.scrabble.com"><font size="2"><i>http://www.scrabble.com</i></font></a>
     <font size="-1"><i>. </i></font><p><font size="2"><i>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. </i></font></p>
     <p><font size="-1"><i>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.</i></font></p>
     <p><font size="-1"><i>Please do not read any further if you are looking for the official Hasbro Scrabbleİ site because this is not it.</i></font></p></td>
    </tr>
   </table>
  </center></div><br><hr>
  <b>Virtual Crossword</b><br>Maintenece by <a href="http://www.yoyoo.com">yoyoo.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="600"><tr><td width="100%">
 <form action="$URL$script" method="POST">
  <hr width="600"><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>
    Player 1 Name: <input type="text" size="15" name="name1"> E-mail: <input type="text" size="15" name="email1"> Password: <input type="text" size="7" name="pass1" ><br>
    Player 2 Name: <input type="text" size="15" name="name2"> E-mail: <input type="text" size="15" name="email2"> Password: <input type="text" size="7" name="pass2" ><br>
    Player 3 Name: <input type="text" size="15" name="name3"> E-mail: <input type="text" size="15" name="email3"> Password: <input type="text" size="7" name="pass3" ><br>
    Player 4 Name: <input type="text" size="15" name="name4"> E-mail: <input type="text" size="15" name="email4"> Password: <input type="text" size="7" name="pass4"></p>
   <hr width="600"><p align="center">
   <input type="hidden" name="board" value="$FROMWEB{'board'}">
   <input type="submit" value="Start Game"></p></form></td>
  </tr>
</table>
</center></div>

HTML
exit;
}
 else {

# check to see all fields have been filled

  $check=1;
  @entry_fields = (name1,name2,name3,name4,email1,email2,email3,email4,pass1,pass2,pass3,pass4);
  foreach $check_field (@entry_fields){
   if ($FROMWEB{$check_field} eq ""){ 
     $check=0; 
     $message = "$message You forgot $check_field.";
    } # end of if...
   } # next foreach $check_field

   if ($check == 0){ 
   &dienice("$message<br>Sorry, you must fill in each of the fields.  Please press back on your browser and try again.");
   } # end of if $check=0

   $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&w&w&x&y&y&z&-&-";
   @letter_bag = split(/&/, $whole_bag); # This places all tiles in letter bag array

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

   for $count (1..7){
     &sort_letter_bag;
     $random_number       = rand @letter_bag;
     $new_letters[3] = "$new_letters[3]$letter_bag[$random_number]\&";
     $letter_bag[$random_number] = "";
     &sort_letter_bag;
   } # end of for $count

   for $count (1..7){
     &sort_letter_bag;
     $random_number       = rand @letter_bag;
     $new_letters[4] = "$new_letters[4]$letter_bag[$random_number]\&";
     $letter_bag[$random_number] = "";
     &sort_letter_bag;
   } # end of for $count

   #____________________________________________
   # 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});
   $title = "$FROMWEB{name1}, $FROMWEB{name2}, $FROMWEB{name3} and $FROMWEB{name4}\'s Game";
   $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;
#---------------------------------------------------
# Now save all information to hard disk
#---------------------------------------------------
   $datafilename = "$FROMWEB{'name1'}_$FROMWEB{'name2'}_$FROMWEB{'name3'}_$FROMWEB{'name4'}\.txt";
   &write_to_disk;
   if ($troubleshoot){print "Wrote to disk.<br>\n";}

#---------------------------------------------------
# Now send each player a separate email with password
#---------------------------------------------------
   $player_mail = $FROMWEB{'email1'};

   foreach $player_number (1,2,3,4){
   if ($troubleshoot){print "Sending mail to player $player_number.<br>\n";}

     chomp ($player_number);
     $email = "email$player_number";
     $name  = "name$player_number";
     $pass  = "pass$player_number";
     $recipients = $FROMWEB{$email};
     $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 $new_letters[$player_number]\nIf you would rather not play, please reply to this letter.\nYours,\nThe Administrator\n";
     &mail_it;

   } # end of foreach

   if ($troubleshoot){print "Letters sent to players.<br>\n";}

   $FROMWEB{'board'} = "$FROMWEB{'name1'}_$FROMWEB{'name2'}_$FROMWEB{'name3'}_$FROMWEB{'name4'}";
   &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 $number (0..99){
 if ($sort_letter_bag[$number] ne '' ) {
  push(@letter_bag, $sort_letter_bag[$number]);
 } # end of if
}# end of foreach $number

} # end of sort_letter_bag subroutine

#---------------------------------------------------
# Points subroutine
#---------------------------------------------------

sub points{

# (simple for now)
 $player_points = $player_points + $FROMWEB{points};
 
}

#---------------------------------------------------
# Print Rules to Screen
#---------------------------------------------------

sub print_rules{

 print <<HTML;
 <center><div align="center">
 <table border="0" width="600">

 <FONT FACE="ARIAL, HELVETICA">
 <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="ARIAL, HELVETICA">
 <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 HJT Files
#---------------------------------------------------
sub open_dialog{

 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
 #---------------------------------------------------
 # @durectory contains all HJT 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="600" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <img src="/games/crossword/images/crossword.gif" alt="Crossword - script by CrookedBush.com">
     <br><font face="Arial" size="3">Please select a board:<br>

HTML

  foreach $item(@directory){
    $item =~ s/\.txt//;
    $item_name = $item;
    $item_name =~ tr/_/ /;
    $item =~ s/\.txt/\./;

    print "<input type=\"radio\"  name=\"board\" value=\"$item\"> $item_name<br>\n";

} # end of foreach $item

    print <<HTML;

     <br>
     <input type="submit" value="Load"></font>
    </td>
   </tr>
  </table>
  </div><p>&nbsp;</p>
</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

#---------------------------------------------------
# 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

} # end of sub open_file

#---------------------------------------------------
# dienice 
# Displays error messages (beautify this later)
#---------------------------------------------------

sub dienice {
    my($errmsg) = @_;
    print <<HTML;

<div align="center"><center>
<table border="0" width="600"><tr><td width="100%">
   <hr width="600"><p align="center">
     $errmsg<p>\n
     <b><a href=\"$URL$script?board=$FROMWEB{'board'}\">Return to Board</a></b><br>
   <hr width="600"><p align="center">
  </tr>
</table>
</center></div>
HTML
 exit;
} # End of dienice subroutine

