Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utility.py
blob: 338d2b4fe2ce007172640f94fb3ebacf64b4c6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python

'''Utility functions module.
'''
import constants
import re
import os

def get_unicode(data):
	if not isinstance(data, unicode):
		return unicode(data, "utf-8")
	return data
	

def create_board(size):
	board = []
	for i in range(0, size):
		row = []
		for j in range(0, size):
			row.append("")
		board.append(row)
	
	return board
	

def get_letters_from_file(lenguage = constants.DEFAULT_LENGUAGE):
	letters = {}
	letters_path = os.path.join(constans.LENGUAGES_PATH, lenguage, 
							    "letters.txt")
	letters_path =  os.sep + letters_path
	file = open(letters_path, "r")
	list = file.readlines()
	# format "Letter": (count, score)
	pattern = re.compile(r".(?P<letter>[\w\s]+).: .(?P<count>\d+),(?P<score>\d+).")
	
	for i in list:
		m = pattern.match(i)
		if m:
			info = (m.group('count') , m.group('score'))
			letters[m.group('letter')] = (int(info[0]) , int(info[1]))	
	
	return letters
	
	
def get_dictionary(lenguage = constants.DEFAULT_LENGUAGE):
	''' Get the dictionary used in the Game
	
		@param lenguage: the lenguage.
		@return: a list containing the dictionary.
	'''
	file_name = "dict-" + lenguage + ".txt" 
	dict_path = os.path.join(constants.DICTIONARIES_PATH, file_name)
	dict_path = os.sep + dict_path 
	file = open(dict_path, "r")
	dict = file.readlines()
	return dict

def search_word(lenguage = constants.DEFAULT_LENGUAGE, word, dict):
	''' Look for the specified word in the dictionary 
	
		@param lenguage: the lenguage.
		@param word: word to look for
		@param dict: the dictionary.
		@return: True or False
	'''
	l2 = filter(lambda n: re.match(word, n), dict)
	if not l2:
		return False
	return True

def can_form_a_word(board, letters,letter, posx, posy):
	''' Check if the given letter can be used to form a new word
		on the board.
		
		@param board: the gameboard
		@param letters: the list of letters to use.
		@param letter: the letter at [row][column] on the board
		@param posx: x-possition [row]
		@param posy: y-possition [column]
		@return: A tuple containing a regular expresion to use as a 
				pattern to search in the dictionary and the type of 
				the given letter. The possible types are:
					- First letter of the word
					- Middle letter.
					- Last letter.
	'''
	# implementar...

def search_words_that_match(criterion):
	''' Look for words in the dictionary that match with the given
		pattern
		
		@param criterion: the pattern
		@param dict: the dictionary
		@return: the list of words that matched				
	'''
	#implementar...