<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

#############################
# eMineSweeper              #
######################################################
# By: Po-Han Lin 			             #
# pohanl@hotmail.com       			     #
# http://edepot.com         			     #
#                           			     #
# This script freely distributable and installable   #
# on the condition that the entire script is intact. #
# You may NOT modify it with the exception of the    #
# "domain name line" and "location of perl line"     #
# to make it work on your server. 		     #
# (credit info located here and at the footer when   #
# this script is executed must not be modifed or     #
# removed)                  			     # 
#                           			     #
######################################################
use strict;


# This is the only line you need to modify...
# Change the line to reflect the domain and location where you
# put this script on your server.

my($theurl)="http://YourDomainName/cgi-bin/minesweeper.pl";


##########################################################################
################## Do Not Modify Anything Below This Line ################
##########################################################################

my($markedbomb,$time_start,$Row,$Col,$theRow,$theCol,@Mine,@Sweep,$end,$total,$temp,$y,$x,$count,$in,@in,%in);

my($i, $loc, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "POST") {
  read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}
@in = split(/&amp;/,$in);
  
foreach $i (0 .. $#in) {
   $in[$i] =~ s/\+/ /g;
   ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
   $key =~ s/%(..)/pack("c",hex($1))/ge;
   $val =~ s/%(..)/pack("c",hex($1))/ge;
   $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
   $in{$key} .= $val;
}
my(%colorCodes)=(1,"blue",2,"darkgreen",3,"red",4,"darkblue",5,"brown",6,"green",7,"black",8,"gray","X","red");
my(%buttonSymbols)=(0,".",1,"",2,"*",3,"?",4," ");

print "Content-type: text/html\n\n";
if ($in{'TS'}) {
   $time_start=$in{'TS'};
}

print "&lt;html&gt;&lt;head&gt;&lt;title&gt;eMineSweeper&lt;/title&gt;&lt;/head&gt;"; 
print "&lt;body&gt;&lt;center&gt;&lt;font size=4 face=Verdana&gt;&lt;b&gt;eMineSweeper&lt;/b&gt;"; 

if (!defined $in{'G'}) {$in{'G'}=1;}

  if ($in{'best'}) {
      &amp;showTimes();
      exit;
  }

if ($in{'G'} eq "1") { 
  $total=$in{'RS'}*$in{'CS'}; 
  $time_start=time(); 
  $in{'GO'}=0;
  if (($in{'RS'}&lt;9)){ $in{'RS'}=9; }
  if (($in{'RS'}&gt;24)){ $in{'RS'}=24; }
  if (($in{'CS'}&lt;9)){ $in{'CS'}=9; }
  if (($in{'CS'}&gt;30)){ $in{'CS'}=30; }
  if ($in{'TotalMines'}&lt;10) {$in{'TotalMines'}=10;}
  if ($in{'TotalMines'}&gt;$in{'CS'}*$in{'RS'}) {$in{'TotalMines'}=($in{'CS'}-1)*($in{'RS'}-1); }

  $count=0; 
  while ($count&lt;$in{'TotalMines'}) { 
    $Row=1+int(rand($in{'RS'})); 
    $Col=1+int (rand($in{'CS'})); 
    if ($Mine[$Row][$Col] ne "9") { 
      $Mine[$Row][$Col]="9"; 
      $count++; 
    } 
  } 

  for ($Row=1;$Row&lt;=$in{'RS'};$Row++) { 
    for ($Col=1;$Col&lt;=$in{'CS'};$Col++) { 
      $Sweep[$Row][$Col]="0"; 
      $count=0;
      for ($y=-1; $y&lt;=1; $y++) { 
         for ($x=-1; $x&lt;=1; $x++) { 
             if ($Mine[$Row+$y][$Col+$x] eq "9") { $count++; } 
         } 
      } 
      if ($Mine[$Row][$Col] ne "9") {
        $Mine[$Row][$Col]=$count; 
      }
    }
  }
} else { 
  $total=$in{'RS'}*$in{'CS'}; 
  $markedbomb=0;
  my($tempMines);
  $tempMines=$in{'Map'};
  $Col=1;
  $Row=1;
  my($theSubmit);
  while ($tempMines =~ /M([\d|\+|X]+)S([\d]+)/g) {
    $Mine[$Row][$Col]=$1;
    $Sweep[$Row][$Col]=$2;
    if ($2==1) {--$total;}
    elsif ($2==2) {++$markedbomb;}
    if ($1 eq "X") {++$markedbomb;}
    $temp="s".($Row*($in{'CS'}+1)+$Col); 
    if ($in{$temp}) {
	$theRow=$Row;
        $theCol=$Col;
	$theSubmit=$in{$temp};
    }
    $Col++;
    if ($Col == $in{'CS'}+1) {
	++$Row;
	$Col=1;
    }
  }
  $end=0; 
         if ($theSubmit &amp;&amp; (! defined $in{'GO'})) { 

	    if ($in{'TP'} eq "Unmark"){ 
		if ($theSubmit eq "*" ||$theSubmit eq "?") {
		  $Sweep[$theRow][$theCol]=0;
		  if ($theSubmit eq "*") {
		    $markedbomb--;
		  }
		}
	    }elsif ($in{'TP'} eq "Mark Mine"){ 
		if ($theSubmit eq "." ||$theSubmit eq "?") {
		$Sweep[$theRow][$theCol]=2;
		$markedbomb++;
		}
	    }elsif ($in{'TP'} eq "Mark ?"){ 
		if ($theSubmit eq "." ||$theSubmit eq "*") {
		  $Sweep[$theRow][$theCol]=3;
		  if ($theSubmit eq "*") {
		    $markedbomb--;
		  }
		}
	    }else { 
		if ($theSubmit eq "."|| $theSubmit eq "?" ) {
              		--$total;
			if ($Mine[$theRow][$theCol] ne "9") { 
                  		$Sweep[$theRow][$theCol]=1; 
				if ($Mine[$theRow][$theCol] eq "0") {
    				  &amp;clearArea($theRow,$theCol);
				}
              		} else { $end=1; }
		}
	    }
         } 
  if ($end==1) { 
     $time_start=time()-$time_start;
     $in{'GO'}=1;
     for ($y=1;$y&lt;=$in{'RS'};$y++) { 
        for ($x=1;$x&lt;=$in{'CS'};$x++) { 
	   if ($Mine[$y][$x] eq "9") {
		if ($Sweep[$y][$x]==2) {
		}else {
		   $Sweep[$y][$x]=1;
		}
	   } else {
		if ($Sweep[$y][$x]==2) {
		   $Sweep[$y][$x]=1;
		   $Mine[$y][$x]="X";
		}
	   }
	   if ($Sweep[$y][$x]==0) {$Sweep[$y][$x]=4;}
        } 
     } 
 } 
  if (($total==$in{'TotalMines'})&amp;&amp;($end!=1)&amp;&amp;(! defined $in{'GO'})) { 
     $time_start=time()-$time_start; 
     $in{'GO'}=2;

     for ($y=1;$y&lt;=$in{'RS'};$y++) { 
        for ($x=1;$x&lt;=$in{'CS'};$x++) { 
	   if (($Mine[$y][$x] eq "9")&amp;&amp;($Sweep[$y][$x]!=2)) {
		$Sweep[$y][$x]=2; 
		++$markedbomb;
	   }
        } 
     }
#####

  } 
} 
print "&lt;table&gt;&lt;tr&gt;&lt;td valign=top&gt;";
print "&lt;table&gt;&lt;tr&gt;&lt;td width=33% bgcolor=black align=center&gt;&lt;font color=red&gt;&lt;b&gt;";
print $in{'TotalMines'}-$markedbomb;
print "&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;&lt;td width=33% bgcolor=yellow align=center&gt;";
if ($end==1) {&amp;newGameButton(1,$in{'RS'},$in{'CS'},$in{'TotalMines'},":(");}
elsif ($end!=1 &amp;&amp; $total==$in{'TotalMines'}) {&amp;newGameButton(1,$in{'RS'},$in{'CS'},$in{'TotalMines'},"B)");}
else { &amp;newGameButton(1,$in{'RS'},$in{'CS'},$in{'TotalMines'},":)"); }

print"&lt;/td&gt;&lt;td bgcolor=black align=center width=33%&gt;&lt;font color=red&gt;&lt;b&gt;";
if ($in{'GO'}) { print $time_start;}
else { print time()-$time_start;}

print "&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=3&gt;";
print "&lt;form method=\"post\" action=\"$theurl\"&gt;"; 
print "&lt;input type=hidden name=TS value=$time_start&gt;"; 
print "&lt;input type=hidden name=TotalMines value=$in{'TotalMines'}&gt;"; 
print "&lt;input type=hidden name=RS value=$in{'RS'}&gt;";
print "&lt;input type=hidden name=CS value=$in{'CS'}&gt;"; 
print "&lt;input type=hidden name=G value=0&gt;"; 
if ($in{'GO'}) { print "&lt;input type=hidden name=GO value=".$in{'GO'}."&gt;"; }

print "&lt;p&gt;&lt;table bgcolor=\"gray\"  &gt;"; 
for ($Row=1; $Row&lt;=$in{'RS'}; $Row++) { 
   print "&lt;tr bgcolor=\"lightgrey\"&gt;"; 
   for ($Col=1; $Col&lt;=$in{'CS'}; $Col++) { 
	if (($end==1)&amp;&amp;($Row==$theRow)&amp;&amp;($Col==$theCol)){
	     print "&lt;td width=16 bgcolor=\"red\" height=15 align=center valign=middle&gt;"; 
	} else {
	      print "&lt;td width=16 height=15 align=center valign=middle&gt;"; 
	}

      if ($Sweep[$Row][$Col]==1) { 
          if (($Mine[$Row][$Col] eq "+")||($Mine[$Row][$Col] eq "0")) { 
             print "&amp;nbsp;"; 
          } else { 
             if ($Mine[$Row][$Col] eq "9") { 
                 print "&lt;b&gt;*&lt;/b&gt;"; 
             } else { 
		print "&lt;font color=\"";
		print $colorCodes{$Mine[$Row][$Col]};
		print "\"&gt;&lt;b&gt;$Mine[$Row][$Col]&lt;/b&gt;&lt;/font&gt;";
             } 
          } 
      } else {
        print "&lt;input type=submit name=s".($Row*($in{'CS'}+1)+$Col)." value=\"";
	print $buttonSymbols{$Sweep[$Row][$Col]};
	print "\"&gt;"; 
      } 
      print "&lt;/td&gt;"; 
   } 
   print "&lt;/tr&gt;"; 
} 
print "&lt;/table&gt;";
print "&lt;input type=hidden name=submit value=ok&gt;"; 
print "&lt;select name=TP&gt;";
print "&lt;option selected&gt;Sweep&lt;option&gt;Mark Mine&lt;option&gt;Mark ?&lt;option&gt;Unmark";
print "&lt;/select&gt;";
if ($in{'GO'}==1) {
   print "&lt;font color=red&gt;&lt;b&gt;Kaboom!&lt;/b&gt;&lt;/font&gt;";
 } elsif ($in{'GO'}==2) {
   print "&lt;font color=green&gt;&lt;b&gt;All Detected!&lt;/b&gt;&lt;/font&gt;";
 }
print "&lt;input type=hidden name=Map value=\"";
print &amp;generateString();
print "\"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;td valign=top&gt;";

print "&lt;table cellpadding=5 border=1 bgcolor=\"gray\"&gt;&lt;tr bgcolor=yellow&gt;&lt;td&gt;&lt;b&gt;Information&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=lightgrey&gt;&lt;td&gt;";
print "&lt;input type=submit name=\"best\" value=\"Best Times...\"&gt;";
print "&lt;/form&gt;";
print "&lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=yellow&gt;&lt;td align=center&gt;&lt;b&gt;New Game&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=lightgrey&gt;&lt;td&gt;";
&amp;newGameButton("1","9","9","10","Beginner");
&amp;newGameButton("1","16","16","40","Intermediate");
&amp;newGameButton("1","16","30","99","Expert");

print"&lt;hr&gt;";

&amp;newGameButton("0",$in{'RS'},$in{'CS'},$in{'TotalMines'},"Custom");

print "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";
print "&lt;p&gt;&lt;center&gt;&lt;font size=-2&gt;&amp;copy;Copyright 2001, &lt;a href=\"mailto:pohanl\@hotmail.com\"&gt;Po-Han Lin&lt;/a&gt; - All rights reserved &lt;a href=\"http://www.edepot.com\"&gt;edepot.com&lt;/a&gt;&lt;/font&gt;&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;
";

sub showTimes {
    print "&lt;html&gt;&lt;head&gt;&lt;title&gt;eMineSweeper&lt;/title&gt;&lt;/head&gt;"; 
    print "&lt;body&gt;&lt;center&gt;&lt;table width=60% border=1 bgcolor=gray&gt;";
    print "&lt;tr bgcolor=yellow&gt;&lt;td align=\"center\"&gt;&lt;b&gt;Fastest eMineSweepers&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr bgcolor=lightgrey&gt;&lt;td&gt;&lt;center&gt;";
    print "Like this program?  Help support my efforts by registering.  In addition, support for high scores is available in the registered version ";
    print "only.  Scores will be stored at an external server.  Your ";
    print "registration fee will go towards paying for the upkeep and ";
    print "maintenance of this server.  To register, Click:";
    print "&lt;A HREF=\"https://secure.paypal.x.com/xclick/business=pohanl%40hotmail.com&amp;undefined_quantity=1&amp;item_name=eMineSweeper+Registration&amp;amount=15&amp;return=http%3A//www.edepot.com/phl.html\" target=\"_blank\"&gt;&lt;IMG SRC=\"http://images.paypal.com/images/x-click-but6.gif\" BORDER=\"0\" ALT=\"Make payments with PayPal - it's fast, free and secure!\"&gt;&lt;/A&gt;&lt;br&gt;";
    &amp;continueButton("OK","0");
    print "&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;";
    print "&lt;/table&gt;";
    print "&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;";
}


sub generateString {
  my($theString)="";
  for ($Row=1;$Row&lt;=$in{'RS'};$Row++) { 
    for ($Col=1;$Col&lt;=$in{'CS'};$Col++) { 
      $theString .= "M".$Mine[$Row][$Col];
      $theString .= "S".$Sweep[$Row][$Col];
    } 
  } 
  return $theString;	
}
sub clearArea {
  my($theRow,$theCol)=@_;
  my(@markforclear);
  my($tmpi,$tmpj);
  $Mine[$theRow][$theCol]="+";
      for ($tmpi=-1; $tmpi&lt;=1; $tmpi++) { 
         for ($tmpj=-1; $tmpj&lt;=1; $tmpj++) { 
		if (defined $Mine[$theRow+$tmpi][$theCol+$tmpj]) {
	  		if ($Sweep[$theRow+$tmpi][$theCol+$tmpj]!=1) {
	       		   if ($Sweep[$theRow+$tmpi][$theCol+$tmpj]!=2) {
               		      $Sweep[$theRow+$tmpi][$theCol+$tmpj]=1;
				--$total;
		  	   }
			}
		  	if ($Mine[$theRow+$tmpi][$theCol+$tmpj] eq "0") {
			   &amp;clearArea($theRow+$tmpi,$theCol+$tmpj);
		  	}
		}	
         } 
      } 
}

sub newGameButton {
  my($hidden,$rows,$cols,$mines,$label)=@_;
  print "&lt;form action=\"$theurl\" method=\"post\"&gt; ";
  if (!$hidden) {
    print "Height:&lt;input type=text name=RS value=\"$rows\" size=2&gt;&lt;br&gt;
    Width:&lt;input type=text name=CS value=\"$cols\" size=2&gt;&lt;br&gt; 
    Mines:&lt;input type=text name=TotalMines value=\"$mines\" size=2&gt;&lt;br&gt;"; 
  } else {
    print "&lt;input type=hidden name=RS value=\"$rows\" size=2&gt;
    &lt;input type=hidden name=CS value=\"$cols\" size=2&gt; 
    &lt;input type=hidden name=TotalMines value=\"$mines\" size=2&gt;";
  }
  print "&lt;input type=hidden name=G value=1&gt;
  &lt;input type=submit name=submit value=\"$label\"&gt; 
  &lt;/form&gt; ";
}

sub continueButton {
  my($label,$condition)=@_;
  print "&lt;form action=\"$theurl\" method=\"post\"&gt; ";
    print "&lt;input type=hidden name=G value=\"$condition\"&gt;"; 
    print "&lt;input type=hidden name=GO value=\"$in{'GO'}\"&gt;"; 
    print "&lt;input type=hidden name=TS value=\"$time_start\"&gt;"; 
    print "&lt;input type=hidden name=RS value=\"$in{'RS'}\"&gt;"; 
    print "&lt;input type=hidden name=CS value=\"$in{'CS'}\"&gt;"; 
    print "&lt;input type=hidden name=TotalMines value=\"$in{'TotalMines'}\"&gt;"; 
    print "&lt;input type=hidden name=Map value=\"$in{'Map'}\"&gt;"; 
    print"&lt;input type=submit name=submit value=\"$label\"&gt; 
    &lt;/form&gt; ";
}


</pre></body></html>