Results 1 to 10 of 10

Thread: Hangman

  1. #1
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default Hangman

    I am attempting to write a version of hangman and I have got everything down except for one particular part.

    I want a function to take the word that we are trying to guess and use that to create either a string or a list that contains underscores with the same length of the word.

    I then need the blanks to be replaced by the guessed letters as the player guesses letters. But the blanks need to be replaced at their specific location that matches with the location of the letter in the word.

    Any ideas on how to possibly do this? I was able to get to the point where it would replace the first occurrence, but I cant get it to replace all of them...

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    @Janilabo; is a god at string functions

  3. #3
    Join Date
    Jan 2013
    Posts
    146
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by Shield View Post
    I am attempting to write a version of hangman and I have got everything down except for one particular part.

    I want a function to take the word that we are trying to guess and use that to create either a string or a list that contains underscores with the same length of the word.

    I then need the blanks to be replaced by the guessed letters as the player guesses letters. But the blanks need to be replaced at their specific location that matches with the location of the letter in the word.

    Any ideas on how to possibly do this? I was able to get to the point where it would replace the first occurrence, but I cant get it to replace all of them...
    What's the code for what you have now?

    Im Back... Previously known as Megaleech
    [Herbalife]

  4. #4
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    Simba Code:
    //~ underscore('Shield','sl'); => S___l_

    function underscore(word,guesses:string):string;
    begin
      exit(replaceRegExpr('(?i)[^'+guesses+' \-]',word,'_',false));
    end;
    Last edited by Obscurity; 11-08-2015 at 11:53 PM.




    Skype: obscuritySRL@outlook.com

  5. #5
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by Saint//+ View Post
    What's the code for what you have now?


    That is what I currently have. Unfortunately I accidentally deleted my function that was able to replace the first occurrence.
    From what I remember about that function is that it involved using .pop and .insert to replace the underscore.

    Quote Originally Posted by Obscurity View Post
    Simba Code:
    //~ underscore('Shield','sl'); => S___l_

    function underscore(word,guesses:string):string;
    begin
      exit(replaceRegExpr('(?i)[^'+guesses+' \-]',word,'_',false));
    end;
    Thanks for that, Obscurity, but unfortunately this is for Python and not pascal...

  6. #6
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    I didn't bother to check the subforum to see Python, sorry. :P. I usually just assume everything on Villa is Lape/PS. Lol.

  7. #7
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by Obscurity View Post
    I didn't bother to check the subforum to see Python, sorry. :P. I usually just assume everything on Villa is Lape/PS. Lol.
    Its all good man! But thanks for trying to help!

  8. #8
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    I would probably do something like this, more or less:
    python Code:
    gen = (c if c.lower() in guesses.lower() else '_' for c in word)
    print ''.join(gen)

    Obscuritys can be translated to python very easily:
    python Code:
    import re
    print re.sub('(?i)[^'+guesses+']','_',word);
    Last edited by slacky; 11-09-2015 at 12:52 AM.
    !No priv. messages please

  9. #9
    Join Date
    Sep 2015
    Posts
    65
    Mentioned
    7 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    I would probably do something like this, more or less:
    python Code:
    gen = (c if c.lower() in guesses.lower() else '_' for c in word)
    print ''.join(gen)

    Obscuritys can be translated to python very easily:
    python Code:
    import re
    print re.sub('(?i)[^'+guesses+']','_',word);
    Dang dude! You're amazing! I never thought about using list comprehension...
    Thanks for this!

  10. #10
    Join Date
    Jan 2013
    Posts
    146
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    Quote Originally Posted by Shield View Post
    Dang dude! You're amazing! I never thought about using list comprehension...
    Thanks for this!
    Here's a simple version of hangman i made if it helps at all.

    python Code:
    class Word:
        def __init__(self, word):
            self.word = word
            self.hidden_word = []
            self.guesses = []
            self.limbs = 6
           
        def setup(self):
            self.letters = list(self.word)
            self.hidden_word = list("_" * int(len(self.word)))

        def guess(self, guess):
            w1 = "".join(self.hidden_word)
            for i in range(0, len(self.letters)):
                if guess == self.letters[i]:
                    self.hidden_word[i] = self.letters[i]
            print(''.join(self.hidden_word))
            w2 = "".join(self.hidden_word)
            if w1 == w2:
                return False
            else:
                self.guesses.append(guess)
                return True
           
        def hang(self, man):
            if man is False:
                self.limbs -= 1

    class Hangman(Word):
        def play(self):
            self.setup()
            attempts = 0
            while ''.join(self.hidden_word) != self.word and self.limbs>0:
                print('Your Guesses:' + ", ".join(self.guesses))
                print('Limbs left: ' + str(self.limbs))
                self.hang(self.guess(input('Guess?')))
               

    hangman = Hangman('burbon')
    hangman.play()


    it'd be cool to add something that goes to a site and grabs its word of the day and also a definition of the word or something you could use as a hint.

    Im Back... Previously known as Megaleech
    [Herbalife]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •