//Hangman Game by John Bezanis for swfspot.com
//Array containing all of the answers
gameanswers = ['yawn' ];
//Attach the missgraphic
this.attachMovie('missgraphic','missgraphic',this.getNextHighestDepth(), {_x:0,_y:80});
//Function that begins a new game
function newGame() {
//select a random answer from the array
answer = gameanswers[Math.floor(Math.random()*gameanswers.length)];
//Add all 26 letter buttons to the stage
for (curindex=0; curindex<26; curindex++) {
//Use the character code for each letter to attach the letter
attachMovie('guessletter', 'guess'+String.fromCharCode(curindex+65), this.getNextHighestDepth());
//set the x position according to its position in the alphabet
eval('guess'+String.fromCharCode(curindex+65))._x = (curindex%13-6.5)*(eval('guess'+String.fromCharCode(curindex+65))._width+3)+Stage.width/2;
//set the y position according to its position in the alphabet
eval('guess'+String.fromCharCode(curindex+65))._y = Stage.height-(2-Math.floor(curindex/13))*(eval('guess'+String.fromCharCode(curindex+65))._height-4);
//set the character display to the current letter in the loop
eval('guess'+String.fromCharCode(curindex+65)).displayletter = String.fromCharCode(curindex+65);
//When the player clicks the button, call the pressLetter function with the letter set
eval('guess'+String.fromCharCode(curindex+65)).onPress = function() {
//process the letter
pressLetter(this.displayletter);
//remove the button from the stage
removeMovieClip(this);
};
}
//Determine how many characters are going to be printed on each line.
//Keep the letters of words from being split onto different lines
//start the row count at 0
curline = 0;
//the start position in the answer is 0
linestart = 0;
//store the length of each line into an array
var linelengths:Array = new Array();
//traverse all of the characters in the answer
for (linepos=0; linepos<length(answer); linepos++) {
//Maximum length of each formatted line
blanklinelength = 25;
//check if the linelength is smaller than the max line length or the first word on the line is longer than the max
if ((linepos-linestart)<blanklinelength || linelengths[curline] == undefined) {
//if the current character is a space, set a marker so we know how long the line should be
//it may be updated if another word fits within the current line
if (answer.charAt(linepos) == ' ') {
//update the length of the current line
linelengths[curline] = linepos-linestart+1;
}
//else the line has reached its limit, so move to the next line and start at the end of the last word
} else {
//store where to start the next line
linestart = linestart+linelengths[curline];
//move to that position
linepos = linestart;
//move to the next line
curline++;
}
}
//set the length of the last line
linelengths[curline] = length(answer)-linestart;
//Now we are going to add the blank holders to the stage
//start at the first row
currow = 0;
//set the position to 0
curpos = 0;
//Loop through the answer and add the blank holders to the stage
for (curindex=0; curindex<length(answer); curindex++) {
//If the current position is a space, do not create a blank holder
if (answer.charAt(curindex) != ' ') {
//add the letter holder
attachMovie('letterholder', 'holder'+curindex, this.getNextHighestDepth());
//set the x position relative to its position on the line and the number of characters on the line
eval('holder'+curindex)._x = 500+((curpos-(linelengths[currow]/2))*20);
//set the y position according to the currow
eval('holder'+curindex)._y = 410+(currow-(linelengths.length/2))*20;
//if the current character is not A-Z, display it
if (!hiddenCharacter(answer.charAt(curindex))) {
//display the character, since it is a special character
eval('holder'+curindex).displayletter = answer.charAt(curindex);
}
}
//If we are at the end of a row, go to the next row
if (++curpos>=linelengths[currow]) {
//move to the next row
currow++;
//reset the position to the left
curpos = 0;
}
}
}
//This function checks if the input character is an A-Z character
function hiddenCharacter(curchar) {
//Set of characters that aren't revealed
hiddenchars = 'abcdefghijklmnopqrstuvwxyz';
//Loop through the character set
for (charindex=0; charindex<length(hiddenchars); charindex++) {
//If the input character is in the list, hide it
if (curchar.toLowerCase() == hiddenchars.charAt(charindex)) {
return true;
}
}
//character is not A-Z, so display it
return false;
}
//function called each time a letter button is pressed
function pressLetter(pressedletter) {
//check if the pressed letter is in the answer
if (!inAnswer(pressedletter)) {
//update the graphic to the next image
missgraphic.gotoAndStop(missgraphic._currentframe+1);
//check if the last frame has been reached
if (missgraphic._currentframe == missgraphic._totalframes) {
//game lost. remove all of the remaining buttons
for (curindex=0; curindex<26; curindex++) {
//if the letter button exists, remove it
if (eval('guess'+String.fromCharCode(curindex+65))) {
//delete the button. 65 is the ascii character A
removeMovieClip('guess'+String.fromCharCode(curindex+65));
}
}
//the game has been lost, so fill in all of the blank spaces with the answers in red
for (curindex=0; curindex<length(answer); curindex++) {
//if there is a space or the letter has been guessed already do nothing
if (answer.charAt(curindex) != ' ' && eval('holder'+curindex).displayletter == undefined) {
//the letter has not been guesed, so reveal it and make it red
eval('holder'+curindex).displayletter = answer.charAt(curindex);
eval('holder'+curindex).letter.textColor = '0xFF0000';
}
}
//Display the button to start a new game
showPlayAgainButton();
}
}
}
//This function checks to see if a guessed letter is in the answer
function inAnswer(pressedletter) {
//initialize goodletter to false.
//we are going to loop through the answer to see if the guessed letter is in the answer
goodletter = false;
//check if there are any letters in the answer that haven't been guessed yet
blankspace = 0;
//loop through the answer
for (curindex=0; curindex<length(answer); curindex++) {
//check if the guessed letter is the character in the current position of the answer
if (answer.charAt(curindex).toLowerCase() == pressedletter.toLowerCase()) {
//display the character
eval('holder'+curindex).displayletter = answer.charAt(curindex);
//setting goodletter to true prevents the image from going to the next frame
goodletter = true;
//if the character at the current position hasn't been guessed, reveal it
} else if (answer.charAt(curindex) != ' ' && eval('holder'+curindex).displayletter == undefined) {
//there is at least one letter that hasn't been guessed
blankspace = 1;
}
}
//if every letter has been guessed, the player wins
if (!blankspace) {
//remove all of the guess buttons
for (curindex=0; curindex<26; curindex++) {
//if the button at the current position exists, remove it
if (eval('guess'+String.fromCharCode(curindex+65))) {
removeMovieClip('guess'+String.fromCharCode(curindex+65));
}
}
//loop through all of the characters and set their color to green
for (curindex=0; curindex<length(answer); curindex++) {
//if the current character isn't a space, set its color to green
if (answer.charAt(curindex) != ' ') {
eval('holder'+curindex).letter.textColor = '0x00AE00';
}
}
//show the play again button
showPlayAgainButton();
}
//return whether or not the guessed letter exists in the answer
return (goodletter);
}
//Show the button to play again
function showPlayAgainButton() {
//attach the button
attachMovie('playagain', 'playagain', this.getNextHighestDepth());
//move it to the middle of the screen
playagain._x = Stage.width/2;
//move it towards the bottom
playagain._y = 370;
//Add a listener for when the button is pressed. When pressed, start a new game
playagain.onPress = function() {
//Reset the hangman graphic to the first frame
missgraphic.gotoAndStop(1);
//loop through all of the letter holders of the answer on the screen and delete them
for (curindex=0; curindex<length(answer); curindex++) {
//if the current position in the answer isn't a blank, remove it
if (answer.charAt(curindex) != ' ') {
removeMovieClip('holder'+curindex);
}
}
//start a new game
newGame();
//delete this button
removeMovieClip('playagain');
};
}
//Start a new game when the applet loads
newGame();