From d54fc9279e751965b4ada0d33ee8d41bc8a72df6 Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Fri, 15 Feb 2013 00:02:59 +0100 Subject: [PATCH] Boggle Qu/Q bug fixed --- modules/active/boggle.py | 130 +++++++++++++++++++++++++++++++++++++-- modules/active/tell.py | 9 +-- 2 files changed, 130 insertions(+), 9 deletions(-) diff --git a/modules/active/boggle.py b/modules/active/boggle.py index 1e24c20..847c601 100644 --- a/modules/active/boggle.py +++ b/modules/active/boggle.py @@ -1,5 +1,4 @@ -import brathuhn, random, time - +import random, time, brathuhn DICE = [['A','A','A','F','R', 'S']] DICE.append(['A','A','E','E','E','E']) DICE.append(['A','A','F','I','R','S']) @@ -30,22 +29,143 @@ DICE.append(['I','P','R','R','R','Y']) DICE.append(['N','O','O','T','U','W']) DICE.append(['O','O','O','T','T','U']) +bog_kern = [[-1,-1], [-1, 0], [-1, 1], + [ 0,-1] , [ 0, 1], + [ 1,-1], [ 1, 0], [ 1, 1]] + +bog_current = None; + def timesup(a): brathuhn.sendMsg(brathuhn.ROOM,"Time's up!",'groupchat') + def boggle(msg,args): if len(args)==1: + global bog_current mymsg = "" random.shuffle(DICE) i = 0 + + bog_current = []; + j = -1; for d in DICE: if i % 5 == 0: + bog_current.append([]) + j = j+1 mymsg += "\n" - i+=1 - mymsg += "%3s" % ( d[random.randint(0,5)] ) + i+=1 + c = d[random.randint(0,5)] + bog_current[j].append(c); + mymsg += "%3s" % ( c ) brathuhn.sendMonoMsg(brathuhn.ROOM,mymsg,'groupchat') brathuhn.sendMsg(brathuhn.ROOM,"you have 3 minutes",'groupchat') brathuhn.callAt(timesup, time.time()+60*3, [1]) else: brathuhn.sendMsg(brathuhn.ROOM,"Usage: !boggle",'groupchat') -brathuhn.addCommand("boggle","Usage: !boggle",boggle) +brathuhn.addCommand("boggle","Play Boggle! Usage: !boggle",boggle) + + +def bog_ans(msg, args): + answer = [w.strip().upper() for w in args[1:]] + + valid = bog_verify(answer, bog_current) + invalid = list(set(answer)-set(valid)) + + count = bog_count(valid) + user = msg.getFrom().getResource() + strinv = "" + if len(invalid) != 0: + strinv = "; invalid: {0}".format(invalid) + brathuhn.sendMsg(brathuhn.ROOM,"{0}: {1} pts{2}".format(user, count, strinv),'groupchat') +brathuhn.addCommand("ba","=boggle answer; Usage: !ba ans1 ans2 ...",bog_ans) + + +def bog_verify(msgs, bog): + + ret = []; + + # TODO migrate to creation of bog_current + bog_pad = [[""]*7] + for i in range(5): + t = [""]; + t.extend(bog[i]) + t.append("") + bog_pad.append(t) + bog_pad.append([""]*7) + + + for m in msgs: + if len(m)<=1: + continue + first = m[0].upper() + + # loop through bog, if first letter == m[0] then checkmachine + pos_r = pos_l = -1; + + for r in bog: + pos_r += 1 + pos_l = -1 + for l in r: + pos_l += 1 + if bog[pos_r][pos_l].upper() == first: + valid = check_valid(m[ 1:], bog_pad, [[pos_r+1,pos_l+1]]) + if(valid): + ret.append(m) + elif bog[pos_r][pos_l].upper() == first+"U": + valid = check_valid(m[ 1:], bog_pad, [[pos_r+1,pos_l+1]]) + if m[1].upper()=="U": + valid = valid or check_valid(m[ 2:], bog_pad, [[pos_r+1,pos_l+1]]) + if(valid): + ret.append(m) + return list(set(ret)) + + +def check_valid(m, bog, path): + + # check path, no letters must be used twice + # pass path along, always use last position in path as + # pos, but check if new match exists in pos + pos = path[-1:][0] + + first = m[0].upper() + + for k in bog_kern: + if ( ( bog[pos[0] + k[0]][pos[1]+k[1]].upper() == first) or \ + ( bog[pos[0] + k[0]][pos[1]+k[1]].upper() == first+"U") ) and \ + ( [pos[0] + k[0], pos[1]+k[1]] not in path) : + + if(len(m) == len(first) ): + return True + else: + cpath = path + cpath.append([pos[0] + k[0],pos[1] + k[1]]) + + if check_valid(m[1:], bog, cpath): + return True + elif first == "Q": + valid = check_valid(m[ 1:], bog_pad, cpath ) + if m[1].upper()=="U": + valid = valid or check_valid(m[ 2:], bog_pad, cpath ) + return valid + + return False + +def bog_count(words): + count = 0; + + for w in words: + l = len(w) + if l < 3: + continue; + elif l < 5: + count += 1 + elif l < 6: + count += 2 + elif l < 7: + count += 3 + elif l < 8: + count += 5 + elif l >= 8: + count += 11 + + return count diff --git a/modules/active/tell.py b/modules/active/tell.py index 4d19d0f..6cb5c06 100644 --- a/modules/active/tell.py +++ b/modules/active/tell.py @@ -4,6 +4,7 @@ import brathuhn import time from datetime import datetime +from datetime import timedelta from persistent import PersistentContainer import config @@ -61,25 +62,25 @@ def tellCB(nick, msg, status): store.remove(r) container["store"] = store -def cleartell(): +def cleartell(ignore): global container store = container["store"] now = datetime.today() rem = [] for l in store: - if(now - l[3] < HOLDBACK): + if(now - l[3] < timedelta(seconds=HOLDBACK)): rem.append(l) for r in rem: store.remove(r) container["store"] = store - brathuhn.callAt(cleartell, time.time()+DELAY, []) + brathuhn.callAt(cleartell, time.time()+DELAY, [None]) ############################################## brathuhn.addCommand("tell","Usage: !tell nick text", tell) brathuhn.addStatusListener(tellCB) -brathuhn.callAt(cleartell, time.time()+DELAY, []) +brathuhn.callAt(cleartell, time.time()+DELAY, [None]) ##############################################