Wednesday, 16 January 2013

Example 20: Hangman server (2/3)


Example 20: Hangman server (2/3)

def find_number(sender):
cdb = contacts.open()
matches = cdb.find(sender)
if matches:
num = matches[0].find("mobile_number")
if num:
return num[0].value
else:

return None
return sender
def message_received(msg_id):
global guessed, num_guesses
box = inbox.Inbox()
msg = box.content(msg_id).lower()
sender = box.address(msg_id)
box.delete(msg_id)
print "Message from %s: %s" % (sender, msg)
if current_word == None:
return
elif msg.startswith("guess") and len(msg) >= 7:
guess = msg[6]
for i in range(len(current_word)):
if current_word[i] == guess:
guessed[i] = guess
num_guesses += 1
elif msg.startswith("word"):
if msg[5:] == current_word:
appuifw.note(u"%s guessed the word!" % sender)
guessed = list(current_word)
num = find_number(sender)
if num:
messaging.sms_send(num, u"Status after %d guesses: %s" %\
(num_guesses, "".join(guessed)))




The second section of the code contains the game logic embedded in
the callback function message received(), which handles incoming
SMS messages. In this function, we first fetch the message contents to the
string msg and convert it to lower case. If the message arrives before a
new game has been initialized, the variable current word is still in its
initial value None and the function returns immediately.
We recognize two types of message: the first follows format ‘GUESS x’
where ‘x’ is any single character. The second format is ‘WORD someword’
where ‘someword’ is the player’s guess for the full word. The function
startswith() is used to distinguish the two cases.
In the first case, we make sure that the message actually contains
some character after the word ‘GUESS’: this is true only if the message
length is at least seven characters. We reveal the guessed characters in
the list guessed by checking in which positions the guessed character
guess occurs in the original word current word. We use a simple
for loop to go through the individual characters. Nothing is changed if
the character does not occur in the word at all.
In the second case, the player tries to guess the full word, so we
check whether the word in the message msg[5:] (we omit the substring
"WORD") matches the original word current word. Note that here we


do not need additional checks for the message length, since expression
msg[5:] returns an empty string if the length is fewer than six characters.
At the end of the function message received(), we send a reply
message to inform the guesser about the current status of the game.
However, there is a small catch: the Inbox object’s function address(),
which should return information about the sender of the message, does
not always return the sender’s phone number. If the sender exists in the
address book of your phone, the name is returned instead of the phone
number.
This might be convenient for an application that shows the sender
information to the user but, in this case, it makes replying impossible
if the sender’s number exists in the address book. Luckily, there is a
workaround: we use the contacts module of PyS60 to access the
phone’s address book, from which the sender’s actual phone number can
be found. This operation is performed in the function find number.














No comments:

Post a Comment