Friday, June 10, 2011

New Tute! Guide to coding a hangman game.

Here is a step-by-step guide to creating a hangman game in Flash CS5 with AS3. Feel free to play around with the code:

Here is a downloadable pdf version:



  1. can we make hangman with two players in flash?
    for example human against the computer by using a backtracking algorithm?
    I'm making my final lecture about it.
    I would be delighted if you can help me solve it. please replay.. thanks before.

  2. Hello Dely,

    Sure, you could make a version where the player is against the computer. I can't really picture what it would look like, though. The PC and the player would take turns at the same puzzle? Who would win? The first one to place all the letters or would you add the functionality to guess the whole word?

    You wouldn't need a backtracking algorithm; you could do it using simple randomisation techniques, since the computer already 'has' the answer.

    Also, you would need to make major changes to the code as it stands. If you want to take a crack at it, I can help you with the code.

  3. Thanks before for the reply.
    I made a hangman game where humans and computers would guess the same word. They have to guess the word letter by letter in turn. First, I will use random numbers to determine the first player to guess. For example, human who got the first turn. After guessing one letter, then turn to the computer to guess one letter too. So on until successfully guess the word or their chances to guess have been exhausted. The winner is the first player who successfully guess all the letters in the word.
    I need backtracking algorithms to create artificial intelligence for the computer so as if computers can think like humans. So it becomes fair game.
    I find it difficult to make a human and a computer to guess in turn and apply bactracking as artificial intelligence algorithms for computers.
    Of course I will be glad for your help . I will wait for your reply. Thanks again... :D

  4. Hello Dely,

    I have made a version that should be suitable for you. You can download it from

    The code and the graphics are a little scruffy. Please keep in mind that it takes a long time to do what you are asking.

    As I mentioned earlier, a randomisation algorithm is able to handle the AI without backtracking. The algorithm works like this for the PC turn:

    Base probability of guessing a letter correctly = 15%
    With each correct letter on the board, this increases by 10%

    Probability of guessing the whole word = base probability / 5

    The AI will not guess the correct word unless at least two letters are on the board

    So this algorithm can be used to simulate an artificial intelligence. The percentages in the algorithm can be tweaked to make the computer opponent smarter or dumber.

    Best wishes and happy coding

  5. Thank you so much ... Sorry if I ask too much.
    This is very helpful. I will learn it.
    If I want to ask again, please do not mind. :D

  6. Sir, i wanna ask u about array.
    If i had array=[satu,dua,tiga,empat].
    For example, i want to make new array from that array that consist four characters length of words.
    So, array2=[satu,tiga,empat].
    How to make it in AS3?
    I've tried but failed.
    The case must exist on the value of variables. I'm confused between integer and string...
    Please your advice. :D

  7. Hello again Dely,

    I didn't see your comment until today. To answer your question, you can use the measure the length of a string in an array.

    The following code will take Array1 and extract all the four-letter words into Array2. I think you'll understand the code when you read through it:

    var Array1:Array = ["one","two","three","four","five","six","seven","eight","nine","ten"];
    var Array2:Array= new Array;

    for(var i=0;i<Array1.length;i++){
    }//for i