#!/usr/bin/perl # # Name: WebConcentration # Author: James Dean Palmer # License: GPL v2 (See http://www.fsf.org/) # Release: 0.2 (7-21-98) # # This is a *VERY* simple concentration game written in perl. # Just modify these first few variables for your system: $path = "/~james/concentration/"; # The skin file changes what the html part of the game looks like $skin = "/home/james/public_html/concentration/skin.html"; # This file must bOCe writeable by the user your web server runs under. $scorefile = "/home/james/public_html/concentration/.scores"; # The images that the player will have to remember: @imagelist = ("a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg", "f.jpg", "g.jpg", "h.jpg" ); # The prize picture revealed at the end of a game. @picturelist = ("ga.jpg", "gb.jpg", "gc.jpg", "gd.jpg", "ge.jpg", "gf.jpg", "gg.jpg", "gh.jpg", "gi.jpg", "gj.jpg", "gk.jpg", "gl.jpg", "gm.jpg", "gn.jpg", "go.jpg", "gp.jpg"); # The number of columns for the image board. $columns = 4; $facecard = "card.jpg"; # Okay, you probably don't need to modify anything beyond # this point. open (INFILE, "< $skin"); while () { $skinoutput = $skinoutput . $_; } close(INFILE); use CGI qw(escape :standard); print header; $puzzleoutput .= start_form; $puzzleoutput .= "
"; $selection=0; $gameid=0; # Create a puzzle if one doesn't exist.. if (!param("q")) { $i=0; foreach $x (@imagelist) { push @alist, $i; push @alist, $i; $i++; } srand(time ^ $$); foreach $x (@alist) { $p = rand; $p = int($p*100000); push @blist, "$p\:$x"; } @blist = sort @blist; foreach $x (@blist) { ($a, $b) = split(/\:/, $x); push @dlist, 0; push @dlist, $b; } } else { $elist = param("q"); $elist = unpack "u", $elist; ($elist, $selection, $gameid) = split(/r/, $elist); @dlist = split(/q/, $elist); } # print the cards according to their state: # 0 >= facedown # -1 == faceup # -2 == solved if (param("pick")) { $pick = param("pick") - 10; } else { $pick = -1; } # if (param("selection")) { $selection = param("selection"); } # else { $selection = 0; } $i=0; $scount=0; $a=-1; $b=-1; while ($i<@dlist) { if ($i==$pick) { $dlist[$i] = -1; } if (($dlist[$i] == -1) && ($a>-1)) { $b = $i; } if (($dlist[$i] == -1) && ($a==-1)) { $a = $i; } if ($dlist[$i] == -2) { $scount = $scount + 2; } if (param("win")) { $dlist[$i] = -2; } $i = $i + 2; } @nextdlist = @dlist; if (( (!param("win")) && ($a > -1) && ($b > -1) )) { if (@dlist[$a+1] eq @dlist[$b+1]) { @nextdlist[$a]=-2; @nextdlist[$b]=-2; $scount = $scount + 4; if ($scount == @dlist) { $puzzleoutput .= p . "Your Name: " . textfield("username") . " \ \;"; $puzzleoutput .= hidden("pick", $i+10); $elist = join("q", @nextdlist); $elist = join("r", ($elist, $selection, $gameid)); $elist = pack "u", $elist; $puzzleoutput .= hidden("q", "$elist"); $puzzleoutput .= hidden("win", "1"); # $puzzleoutput .= hidden("selection", "$selection"); $puzzleoutput .= submit("See Puzzle / Scores"); } else { $puzzleoutput .= h3("A very good match!"); $puzzleoutput .= h4("Select another card."); } } else { $puzzleoutput .= h3("Sorry, that's not a match."); $puzzleoutput .= h4("Select another card."); @nextdlist[$a]=10; @nextdlist[$b]=10; } } else { if (!param("win")) { $puzzleoutput .= "Select a card."; } else { $puzzleoutput .= "Good Game, " . param("username") . "! "; $puzzleoutput .= "You won in " . $selection . " moves.

"; $scoreout = "NameScore"; $i=0; $name = param("username"); open (INFILE, "< $scorefile"); while () { if (($_ ne "") && ($_ ne "\n")) { s/\n//gi; push @scoretable, $_; } } close(INFILE); open (INFILE, "> $scorefile"); foreach $x (@scoretable) { # dont add the same score/name AGAIN. if ($x eq "$selection\:$name") { $i=1; } } if ($i==0) { push @scoretable, "$selection\:$name"; } $i=0; @scoretable = sort { $b <=> $a; } @scoretable; while ((@scoretable) && $i<10) { $i++; $_ = pop @scoretable; ($score, $name) = split /\:/, $_; print INFILE "$score\:$name\n"; $scoreout .= "$name$score"; } close(INFILE); } } $i=0; if (param("win")) {$puzzleoutput .= "\n"; } else { $puzzleoutput .= "
\n"; } while ($i<@dlist) { $link = "\"; # $link .= "\&selection\=" . ($selection + 1) $image = "\"; if (!($i % ($columns*2))) { $puzzleoutput .= "\n\n\n"; } $puzzleoutput .= ""; $i=$i+2; } $puzzleoutput .= "
"; if ($dlist[$i] == -1) { $puzzleoutput .= $image; } if ($dlist[$i] == -2) { $puzzleoutput .= "\"; } if ($dlist[$i] >= 0 ) { $puzzleoutput .= $link . "\\<\/a\>"; } $puzzleoutput .= "
\n

"; $skinoutput =~ s/\<\!\-\- concentration puzzle \-\-\>/$puzzleoutput/sig; $skinoutput =~ s/\<\!\-\- concentration score \-\-\>/$scoreout/sig; print $skinoutput;