#!/usr/bin/perl -w

# Mr Hang Thang
# Assignment 3, CS 191T,  CSU Fresno,  Fall 1998
# Robert G. Werner
# $Id: mrhangthang.cgi,v 1.1 2003/05/31 07:43:36 robertw Exp robertw $
# $Revision: 1.1 $
# $State: Exp $

$words_path = "/usr/share/dict/words";

use CGI;
$query = new CGI;

if ($query->param("frame")) {
	if ($query->param("frame") eq "game") {
		&game_window;
	} elsif ($query->param("frame") eq "hang") {
		&hang_window;
	} 
} else {
	&frame_set;
	die;
}

# print the frame set dfn.
sub frame_set {
	print $query->header;

	print "<TITLE>Mr. Hang Thang</TITLE>\n";
	print "<FRAMESET cols=\"*,30%\">\n";
	print "\t<FRAME src=\"mrhangthang.cgi?frame=game\" noresize name=\"game_win\">\n";
	print "\t<FRAME src=\"mrhangthang.cgi?frame=hang\" noresize name=\"hang_win\" scrolling=\"no\">\n";
	print "\t<NOFRAME>\n";
	print "Sorry,  but I had to write this page using JavaScript and frames.  Please visit my <A HREF=\"../index.html\">home page</A> for an HTML compliant page!.\n";
	print "\t</NOFRAME>\n";
	print "</FRAMESET>\n";
	print $query->end_html;
}

sub print_head {
	print $query->header;
	print $query->start_html(-title=>@_,
				-author=>'robert@inreachtech.net',
				-bgcolor=>'#ffffff',
				-TEXT=>'#000000', 
				-LINK=>'#000099', 
				-VLINK=>'#663333');
	print "\n";
}

sub game_window {
	open (WORDS, $words_path);
	@words_list = <WORDS>;
	close (WORDS);
	$index = int(rand(@words_list));
	$word = $words_list[$index];
	chomp($word);
	@word_array = split(//, $word);

	&print_head("Game Window");
	print "<SCRIPT>\n";
#	print "<!--\n";
	print <<END;
// The basic functions to play Mr. Hang Thang ...

// Global vars.
secret_word = \"$word\";	// This round's secret word.
pic_prefix = "mrh."
success_jpg = "mrh.success.jpg"
number_found = 0;
number_of_parts = 6;		// Number of diff. views of Mr. Hang Thang
misses = 0;
game_over = false;

// Handle the user's guess.  Decide the correct response.
function guess_handler (letter) {
	var try_this_letter = letter.options[letter.selectedIndex].value;
	letter.selectedIndex = 0;

	// Are we still playing?
	if (game_over) {
		alert ("Still trying to figure this word out? Click \\\"New Game?\\\" for another round.");
		return ;
	}
	// did the user find a letter
	if (in_word(try_this_letter)) {
		// are there any more letters to find
		if (done_yet()) {
			success(); // He's Saved ...
		} else {
			correct();
		}
	} else {
		// Has Mr. Hang Thang died?
		if (dead_yet()) {
			failure();
		} else {
			bzt_tryagain(try_this_letter);
		}
	}
}

// Is the letter in the secret_word word
function in_word (try_this_letter) {
	var found1=false;
	for (var i = 0 ; i < secret_word.length ; i++ ) {
		var secret_letter = secret_word.charAt(i);
		if (try_this_letter == secret_letter.toUpperCase()){
			var found1=true;
			var field_name = "field" + i;
			if (document.display[field_name].value) { 
				alert("You already got that letter.  Try another.");
			} else {
				number_found++;
				// Change the field field_name to be try_this_letter
				document.display[field_name].value = try_this_letter;
			}
		}
	}
	return (found1);
}

// No it isn't
function bzt_tryagain (try_this_letter){
	misses++;
	var the_text = "Sorry,  the letter " + try_this_letter + " isn't in the word.";
	alert(the_text);
	change_pic(misses);
}

// Change Mr. Hang Thang's picture (i.e. add another piece).
function change_pic (misses) {
	// change the pic in frame hang_win
	var hang = parent.hang_win.document;
	var pic = pic_prefix + misses + ".jpg";
	hang.clear();
	hang.open("text/html");
	hang.write("<HTML><HEAD>");
	hang.write("<TITLE>Hang Window</TITLE>");
	hang.write("</HEAD><BODY BGCOLOR=#ffffff>");
	hang.write("<CENTER><IMG src=" + pic + ">");
	hang.write("</BODY></HTML>");
	hang.close();
}

// User got one right
function correct () {
	alert("Yea!!  Keep going and Mr. Hang Thang might make it yet!");
}

// Do we still have letters to find?
function done_yet () {
	return ( number_found == secret_word.length);
}

// Is Mr. Hang Thang, ... er, ... well,  dead?
function dead_yet () {
	return (misses >= number_of_parts - 1);
}

// User won the game
function success () {
	alert("Excelent!!! Mr. Hang Thang lives another day.  Click \\\"New Game\\\" to play another.");
	// change the pic in frame hang_win
	var hang = parent.hang_win.document;
	hang.clear();
	hang.open("text/html");
	hang.write("<HTML><HEAD>");
	hang.write("<TITLE>Hang Window</TITLE>");
	hang.write("</HEAD><BODY BGCOLOR=#ffffff>");
	hang.write("<CENTER><IMG src=" + success_jpg + "><P>");
	hang.write("<EM>Mrs. Hang Thang thanks you for saving her Thang!!</EM><P>");
	hang.write("<H1>Ready to give it another shot?  Please try again.</H1>");
	hang.write("</BODY></HTML>");
	hang.close();
	game_over=true;
}

// It's dead Jim ...
function failure () {
	misses++;
	alert("I'm sad to say that Mr. Hang Thang died today ... due to YOUR negligence ;-).  Click \\\"New Game\\\" and try and do better.");
	// change the pic in frame hang_win
	var hang = parent.hang_win.document;
	var pic = pic_prefix + misses + ".jpg";
	hang.clear();
	hang.open("text/html");
	hang.write("<HTML><HEAD>");
	hang.write("<TITLE>Hang Window</TITLE>");
	hang.write("</HEAD><BODY BGCOLOR=#ffffff>");
	hang.write("<CENTER><IMG src=" + pic + "><P>");
	hang.write("<EM>Mrs. Hang Thang was so hoping you could save her Thang!!</EM><P>");
	hang.write("The word was <EM>" + secret_word + "</EM>,  by the way.<P>");
	hang.write("<H1>Game Over.  Please try again.</H1>");
	hang.write("</BODY></HTML>");
	hang.close();
	game_over=true;
}
END
#	print "// -->\n";
	print "</SCRIPT>\n";
	print "<H1><CENTER>Want to play a game?</H1></CENTER>\n";
	print "<P>\n";
	print "<CENTER>";
	print <<END;
The object of the game is to find out what the word is.  <EM>Remember Mr. Hang Thang's life depends on it.</EM>  Simply select a letter from the menu and keep going unil you free Mr. Hang Thang or he meets his undeserved fate.  Hit the "New Game" button if this word looks too hard but the next one might be harder.
END
	print "</CENTER><P>\n";
	print $query->startform(-name=>"display",
				-method=>"post",
				-target=>"_top",
				-action=>"mrhangthang.cgi");
	print "<CENTER><TABLE>\n";
	print "<TR>\n";
	$len = @word_array;
	for ($i = 0 ; $i < $len ; $i++) {
		print "<TD>";
		print $query->textfield(-name=>"field$i",
					-size=>"1",
					-value=>"",
					-onFocus=>"this.blur()"),"\n";
	}
	print "</TABLE></CENTER>\n";
	print "<P><CENTER>\n";
	print $query->popup_menu(-name=>"letter",
			  	 -"values"=>["Choose a letter",A..Z],
				 -onChange=>"guess_handler(this)"),"\n";
	print "<P>\n";
	print $query->submit(-name=>"New_game",
			     -value=>"New Game?"),"\n";
	print $query->endform , "\n";
	print "<P>\nIf you liked this game,  please send me mail at: <A HREF=\"mailto:robert\@inreachtech.net\">robert\@inreachtech.net</A>\n<P>\n";
	print "<CITE>Copyright Robert G. Werner,  1998</CITE>\n";
	print $query->end_html;
}

sub hang_window {
	&print_head("Hang Window");
	print "<CENTER>Ready for a  hangin'?!!</CENTER>\n";
	print $query->end_html;
}
