0

I've been trying to make a hangman game where if the command "!hangman" is iniated, this code will execute:

def cmdHangman(event):
    print("!hangman") #this print works
    type("Game: Hangman")
    type(Key.ENTER, Key.SHIFT)
    Hangman.Playing()

Where Hangman.Playing() leads to:

listOfWords = ["example", "says", "python", "rocks"]
guessWord = random.choice(listOfWords)
board = [" * " for char in guessWord]
alreadySaid = ""

class Hangman():

    def Playing():
        print("Playing") #print not showing
        global guessWord, board, alreadySaid    

        whatplayersaid = hangmanRegion.text()
        hangmanRegion.stopObserver()

        if whatplayersaid in guessWord:
          board = [char if char == whatplayersaid or char in alreadySaid else " * " for char in guessWord]
          board = "".join(board)
          type(board)
          type(Key.ENTER)
        else:
         type("Nope")
         type(Key.ENTER)
        alreadySaid = alreadySaid + whatplayersaid
        Hangman.PlayBuffer()

    def gotoPlaying(event):
        print("gotoPlaying") #print not showing
        Hangman.Playing()

    def PlayBuffer():
        print("PlayBuffer") #print not showing
        wait(1)
        hangmanRegion.onChange(Hangman.gotoPlaying)
        hangmanRegion.observe(FOREVER)

It should scan the next chat input for letters using hangmanRegion.text(), so if someone in the groupchat types: "A", whatplayersaid should equal "A"

The weird thing, it doesnt even work. At all. There is no error log or anything. I tried putting prints everywhere in class Hangman() and I didn't see any of the prints in the log.

Could someone pinpoint a flaw in my code? Is it not being executed or is Sikuli IDE glitching out? Or am I just really tired?

Log after typing the command:

!hangman
[log] TYPE "Game: Hangman"

[log] ( Shift ) TYPE "#ENTER."
SikuliXUser
  • 53
  • 1
  • 2
  • 9

1 Answers1

2

It looks like there are 2 things wrong with your class that is causing it to silently die. When you call the Playing function you are trying to call it directly rather than from an instance of the class.

Also (and I may be off base with this one as i'm still learning python myself) the function within a class should be passed at least 1 argument, usually self.

If you look at the example below you can see if i try and call Hangman.Playing() directly, i get an error about it not being an instance.

If instead i create an instance x and call it from that, the print statement successfully executes.

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Hangman:
...     def Playing(self):
...             print "Playing"
...
>>> Hangman.Playing()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method Playing() must be called with Hangman instance as     first argument (got nothing instead)
>>> x = Hangman()
>>> x.Playing()
Playing
>>>
uzusan
  • 36
  • 2