Jabber/XMPP conference bot & framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
brathuhn/modules/active/boggle.py

215 lines
5.1 KiB

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'])
DICE.append(['A','D','E','N','N','N'])
DICE.append(['A','E','E','E','E','M'])
DICE.append(['A','E','E','G','M','U'])
DICE.append(['A','E','G','M','N','N'])
DICE.append(['A','F','I','R','S','Y'])
DICE.append(['B','J','K','Qu','X','Z'])
DICE.append(['C','C','E','N','S','T'])
DICE.append(['C','E','I','I','L','T'])
DICE.append(['C','E','I','P','S','T'])
DICE.append(['C','E','I','L','P','T'])
DICE.append(['D','D','H','N','O','T'])
DICE.append(['D','H','H','L','O','R'])
DICE.append(['D','H','L','N','O','R'])
DICE.append(['D','H','L','N','O','R'])
DICE.append(['E','I','I','I','T','T'])
DICE.append(['E','M','O','T','T','T'])
DICE.append(['E','N','S','S','S','U'])
DICE.append(['F','I','P','R','S','Y'])
DICE.append(['G','O','R','R','V','W'])
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, 0], [ 0, 1],
[ 1,-1], [ 1, 0], [ 1, 1]]
bog_current = None
answers = {}
def timesup(a):
brathuhn.sendMsg(brathuhn.ROOM,"Time's up!",'groupchat')
def boggle(msg,args):
if len(args)==2 and args[1]=="start":
boggle_start(msg,args)
elif len(args)==2 and args[1]=="count":
boggle_count(msg,args)
else:
brathuhn.sendMsg(brathuhn.ROOM,"Usage: !boggle start|count",'groupchat')
def boggle_start(msg,args):
global bog_current
global answers
answers = {}
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
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])
def boggle_count(msg,args):
global answers
# remove double words
removed = {}
for k in answers:
if k not in removed:
removed[k] = []
ans = answers[k]
for word in ans:
for k_others in answers:
if k_others==k:
continue
ans_o = answers[k_others]
if word in ans_o:
if not word in removed[k]:
removed[k].append(word)
if k_others in removed:
removed[k_others].append(word), k_others
else:
removed[k_others] = [word]
ans_o.remove(word)
for word in removed[k]:
if word in ans:
ans.remove(word)
# count the points
for k in answers:
ans = answers[k]
valid = bog_verify(ans, bog_current)
invalid = list(set(ans)-set(valid))
count = count_points(valid)
user = k
strinv = ""
strval = ""
strdoub = ""
if len(invalid) != 0:
strinv = "; invalid: {0}".format(invalid)
if len(valid) != 0:
strval = "; valid: {0}".format(valid)
if len(removed[k]) != 0:
strdoub = "; doubles: {0}".format(removed[k])
brathuhn.sendMsg(brathuhn.ROOM,"{0}: {1} pts{2}{3}{4}".format(user, count, strinv,strval,strdoub),'groupchat')
# reset answers
answers = {}
brathuhn.addCommand("boggle","Play Boggle! Usage: !boggle start|count",boggle)
def bog_ans(msg, args):
global answers
answer = [w.strip().upper() for w in args[1:]]
user = msg.getFrom().getResource()
answers[user]=answer
brathuhn.sendMsg(brathuhn.ROOM, user+": ACK!",'groupchat')
brathuhn.addCommand("ba","=boggle answer; Usage: !ba ans1 ans2 ...",bog_ans)
def bog_verify(msgs, bog):
ret = [];
import sys
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 = 0;
for r in bog:
pos_l = 0
for l in r:
valid = False
if bog[pos_r][pos_l].upper() == first:
valid = check_valid(m[1:], bog, [[pos_r,pos_l]])
elif bog[pos_r][pos_l].upper() == first+"U":
valid = check_valid(m[1:], bog, [[pos_r,pos_l]])
if m[1].upper()=="U":
valid = valid or check_valid(m[ 2:], bog, [[pos_r,pos_l]])
if(valid):
ret.append(m)
pos_l += 1
pos_r += 1
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()
valid = False
for k in bog_kern:
try:
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") ):
if [pos[0] + k[0], pos[1]+k[1]] in path:
continue
if(len(m) == 1 ):
valid = True
else:
cpath = list(path)
cpath.append([pos[0] + k[0],pos[1] + k[1]])
if check_valid(m[1:], bog, cpath):
valid = True
elif first == "Q":
valid = valid or check_valid(m[ 1:], bog, cpath )
if m[1].upper()=="U":
valid = valid or check_valid(m[ 2:], bog, cpath)
except IndexError:
pass
return valid
def count_points(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