#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Help coding a hangman/word guessing game?


    So this is a two player game where players pick the rounds to play, and take turns trying to guess a word that the other person has picked. (like hangman). players get points based on how close they get to picking the word.

    Each round a person types in a secret word, and when the players presses enter the screen is cleared. The person then allows how many guesses that are allowed to guess the word.

    The opponent player guesses a character a-z until he guesses it or until he runs out of guesses, in which case they roles are swapped. after each guess he should be able to see partial words. (if the word is candy and he guesses a, then it should show c*ndy).



    I'm a noob at this, so far I've only gotten past the beginning part:

    Code:
    rounds_question= int(raw_input("How many rounds do you want to play?"))
    if rounds_question > 0:
        print rounds_question,  "rounds"
    else:
        while rounds_question <= 0:
            rounds_question = int(raw_input("Error: pick a positive number"))
            if rounds_question > 0:
                    print rounds_question, "rounds!"
                    break


    The next part i have trouble with right now is displaying the number of rounds in the beginning of the match. Like if he chooses 5 rounds, then how do i display round 1, round 2, at the end of every match? (one round = one turn each)


    how do i code this using chr() and ord () ?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    In my opinion, you should get the basic game working first before adding all the extra logic about whose turn it is and the word source. You could use chr and ord, or you could compare characters directly. Will this function help you on your way?
    Code:
    def skin_word(word, letters):
        '''
            use doctest
            shell_prompt$ python -m doctest thisfile.py
    
            >>> word = 'syzygy'.lower()
            >>> guessed_letters = set('etasg')
            >>> print(skin_word(word, guessed_letter))
            s***g*
        '''
        return ''.join(letter if letter in letters else '*' for letter in word)
    
        # alternatively
        return ''.join(('*', letter,)[letter in letters] for letter in word)
    
        # both are shorthand for
        result = list()
        for letter in word:
            if letter in letters:
                result.append(letter)
            else:
                result.append('*')
        return ''.join(result)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    hmm yes, thanks. I'm new to python so I don't understand the 'set' and 'def' very well. I'll take a look at this code, i appreciate it!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    sets are quite a bit like mathematical sets.
    Yup, intersection, union, all items unique.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo