diff options
Diffstat (limited to 'elements.py')
-rwxr-xr-x | elements.py | 157 |
1 files changed, 146 insertions, 11 deletions
diff --git a/elements.py b/elements.py index 56d606a..6e286ed 100755 --- a/elements.py +++ b/elements.py @@ -68,7 +68,6 @@ class Letter: @param other: Other letter @return: True if the Letter strings are the same ''' - # To do controlar los acentos. if(isinstance(other, Letter)): a = utility.get_unicode(self.get_character()) b = utility.get_unicode(other.get_character()) @@ -84,7 +83,6 @@ class Letter: @param other: Other letter @return: True if the Letter strings are not the same ''' - # To do controlar los acentos. if(isinstance(other, Letter)): if(self == other): return False @@ -266,12 +264,28 @@ class Move: ''' Get the first move''' return get_move(0) - def is_valid(self): - ''' Check whether this move is valid + def is_valid(self, dictionary): + ''' Check whether this move is valid, that is + form the word and look if it exists in the dictionary - @return: True if the move is valid + @param dictionary: the dictionary + @return: True if the move is valid, otherwise False ''' - # implementar.... + word = self.word_to_play() + if self.is_horizontal() or self.is_vertical(): + if utility.search_word(word, dictionary): + if self.has_common_tile(len(word), board): + if verify_words(board): + return True + else + return False + else + return True + else + return False + else + return False + def is_horizontal(self): ''' Check whether the Move is horizontal. @@ -332,6 +346,107 @@ class Move: the given word into the gameboard, otherwise False. ''' # implementar.... + + + def word_to_play(self): + ''' To form the word from the move object + + @return: Word that this Move spells + ''' + + word = "" + for letter in self.get_moves(): + word += letter.get_character() + return word + + + def has_common_tile(self, l, board): + ''' + Check to see if this move has a common tile with another word in the board + + @param board: the board that contains the words + @param l: the length of the word + @return: True if this Move has a letter,x,y tile in common + ''' + LETTER = 0 + ROW = 1 + COL = 2 + row_in = self.get_move(0)[ROW] + col_in = self.get_move(0)[COL] + + row_fi = self.get_move(l - 1)[ROW] + col_fi = self.get_move(l - 1)[COL] + + if self.is_horizontal(): + flag = True + j = col_in + while j >= col_in and j <= col_fi and flag = True: + if board.get_value_at(row_in, j) == " ": + flag = False + j += 1 + + #if flag == True: + #the word has a tile in common, so the move is ok + # return True + #else: + #the word hasn't a tile in common, so try another move + # return False + return flag + + if self.is_vertical(): + flag = True + i = row_in + while i >= row_in and i <= row_fi and flag = True: + if board.get_value_at(i, col_in) == " ": + flag = False + i += 1 + + #if flag == True: + #the word has a tile in common, so the move is ok + # return True + #else: + #the word hasn't a tile in common, so try another move + # return False + return flag + + def verify_words(self, board): + ''' + Check if the gameboard has corret words + + @param board: the gameboard with the words + ''' + + words = " " + + #verify if the rows have correct words + for i in range(0,board.get_size()): + for j in range(0, board.get_size()): + word += str(board.get_value_at(i, j)) + + list_words = words.split() + for word in list_words: + if not utility.search_word(word, dictionary): + return False + words = " " + + #verify if the cols have correct words + for j in range(0, board.get_size()): + for i in range(0, board.get_size()): + words += str(board.get_value_at(i, j)) + list_words = words.split() + for word in list_word: + if not utility.search_word(word, dictionary): + return False + words = " " + + return True + + def first_move(self, board): + middle = (board.get_size() + 1) / 2 + if board.get_value_at(middle, middle) != " ": + return True + return False + class Bag: @@ -348,10 +463,19 @@ class Bag: def remove_letters(self, amount = constants.DEFAULT_AMOUNT_LETTERS): ''' Removes Letters from the bag - @param size: the amount of letters that are going to be removed + @param size: the amount of Letters that are going to be removed @return: the letters removed. ''' # format "letter": (count, score) + + n = self.amount_letters_in_bag() + + # if amount > n just remove the n letters in the bag. + if n > 0 and amount > n: + amount = n + elif n == 0 + return -1 + removed = [] i = 0 while i < amount: @@ -360,18 +484,18 @@ class Bag: if data[0] > 0: removed.append(Letter(key, data[1])) self.get_bag_letters()[key] = (data[0] - 1, data[1]) - i = i + 1 + i += 1 return removed def add_letters(self, letters): ''' Add letters to the bag - @param letters: the list of letters that are going to be added + @param letters: the list of Letters that are going to be added ''' for l in letters: - count , score = self.get_bag_letters()[l] - self.get_bag_letters()[l] = (count + 1, score) + count , score = self.get_bag_letters()[l.get_character()] + self.get_bag_letters()[l.get_character()] = (count + 1, score) @@ -379,3 +503,14 @@ class Bag: ''' Get the bag of letters ''' return self.__bag_letters + + def amount_letters_in_bag(self): + ''' Return the number of letters that are avalable in the bag. + ''' + count = 0 + for key, data in self.get_bag_letters().items(): + count += data[0] + return count + + + |