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:

http://flashbynight.com/tutes/hangman

Here is a downloadable pdf version:

http://www.scribd.com/doc/57512256/Create-a-HangMan-Game-in-Flash-AS3-Tutorial

Enjoy!

7 comments:

  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.
    :)

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  4. Hello Dely,

    I have made a version that should be suitable for you. You can download it from http://flashbynight.com/tutes/hangman/HangmanAI.zip

    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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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++){
    if(Array1[i].length==4){Array2[Array2.length]=Array1[i];}
    }//for i

    trace(Array2);

    ReplyDelete