Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/elements.py
blob: 6e286ed8572fde8f6b7d72d56efd00afddc9267a (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python

import utility
import constants

class Letter:
	''' Letter class. Represents a letter on the gameboard.
	'''
	def __init__(self, character = "", score = 0):
		''' Initialize the letter
			@param character: The string representing the letter.
		'''
		self.__score = score
		self.__is_blank = False
		self.set_character(character)
	
	
	
	def set_character(self, character):
		''' Set the letter string
		   
		    @param character: the new letter string value	
		'''
		if(character == " "):
			self.__is_blank = True
		self.__character = character
	
	def set_is_blank(self, is_blank):
		''' Set is_blank property of the letter
		
			@param is_blank: True or False.
		'''
		self.__is_blank = is_blank
	
	def set_score(self, score):
		''' Set the score of the letter.
		
			@param score: the new score value of the letter.
		'''
		self.__score = score
	
	def get_character(self):
		''' Get the letter string
		
		    @return: the string representation of this letter.
		'''
		return self.__character
	
	def get_score(self):
		''' Get the letter score.
		
		    @return: the score of this letter.
		'''
		if(self.is_blank()):
			return 0
		return self.__score
	
	def is_blank(self):
		''' Check if the letter is blank
		
			@return: True if the letter is a blank letter.
		'''
		return self.__is_blank
		
	def __eq__(self, other):
		''' Check if this Letter equals another Letter
		
			@param other: Other letter
			@return: True if the Letter strings are the same
		'''
		if(isinstance(other, Letter)):
			a = utility.get_unicode(self.get_character())
			b = utility.get_unicode(other.get_character())
			if a == b:
				return True
			elif ((a != b) and (self.is_blank() == True and other.is_blank() == True)):
				return True
		return false
	
	def __neq__(self, other):
		''' Check if this Letter does not equal another Letter
		
			@param other: Other letter
			@return: True if the Letter strings are not the same
		'''
		if(isinstance(other, Letter)):
			if(self == other):
				return False
		return True
	
	def __repr__(self):
		''' Return a string formatted as follow:
			Letter_String: Score
			
			@return: string representation of this Letter.
		'''
		return self.get_character() + ": "+str(self.get_score()) 


class Board:
	''' Board class. Represents the gameboard.
	'''
	def __init__(self, size = constants.DEFAULT_BOARD_SIZE):
		''' Initialized the board
			
			@param size: the size of this board. For example 15x15
		'''
		
		self.__size = size
		self.__board = utility.create_board(size)
	
	def get_size(self):
		''' get the size of this board
		
			@return: the size.
		'''
		return self.__size
	
	def set_size(self, size):
		''' set the size value of this board
		
			@param: the new size value.
		'''		
		self.__size = size
		
		
	def get_value_at(self, row, column):
		''' Get the value at the specified position [row][column] on the
			board.
			
			@param row: row on the board
			@param column: column on the board			
			@return: The value at the specificied position.
		'''
		return self.__board[row][column]
	
	def set_value_at(self, value, row, column):
		''' Set the value at the specified position [row][column] on the
			board.
			
			@param value: new value
			@param row: row on the board
			@param column: column on the board
		'''		
		self.__board[row][column] = value
		
	def get_tile_style(self, row, column):
		''' Get the style of the tile  at the specified position 
		    [row][column] on the board.
			
			@param row: row on the board
			@param column: column on the board			
			@return: The style of the tile at the specificied position.
		'''
		# implementar....
	
	def get_tile_score(self, row, column):
		''' Get the score of the tile  at the specified position 
		    [row][column] on the board.
			
			@param row: row on the board
			@param column: column on the board			
			@return: The score of the tile at the specificied position.
		'''
		# implementar....
	
	def is_tile_empty(self, row, column):
		''' Check if the tile is empty  at the specified position 
		    [row][column] on the board.
			
			@param row: row on the board
			@param column: column on the board			
			@return: True or False
		'''
		return  (self.get_value_at(row, column) == "")
	
	 	
		


class Move:
	''' Move class represents a move made by a player.
	    The Move is a list of tuples containing 
	    (letter, x-position, y-position) for all Letters in the Move.	
	'''	
	def __init__(self, move = None):
		self.__score = 0
		self.__move = []
		self.__has_blank = False
		if move != None:
			for l, x, y in move:
				self.add_move(l, x ,y)
	
	def has_blank(self):
        '''
        Check whether this Move contains a blank letter
        
        @return: True if this Move contains a blank Letter
        '''	
		return self.__has_blank
		
	def add_move(self, letter, x, y):
		''' Add a new move
			
			@param letter: the Letter
			@param x: x-position on the board
			@param y: y-position on the board
		'''
		if not self.has_blank():
			self.__has_blank = letter.is_blank()
		move.append((letter, x , y))
	
    def remove_move(self, letter, x, y):
        ''' Remove move
			
			@param letter: the Letter
			@param x: x-position on the board
			@param y: y-position on the board
        '''
        self.move.remove((letter,x,y))

	def get_score(self):
		''' Get the score for the move
			
			@return: the score for the move
        '''
		return self.__score
	
	def set_score(self, score):
		''' Set the score 
			
			@param score: the score 
        '''		
		self.__score = score
	
	def length(self):
        '''
        Get number of tuples in the Move.
        
        @return: Number of tuples in the move.
        '''
        return len(self.move)
    
    def clear(self):
		''' Clear the move '''
		move = []
	
	def get_move(self, index):
		''' Get the Move at the specified index
		
			@param index: index
			@return: the Move at the specified index
		'''
		return move[index]
		
	def get_moves(self):
		''' Get the list of moves
		
			@return: the list of  moves
		'''
		return self.__move	
	
	def get_first_move(self):
		''' Get the first move'''
		return get_move(0)
		
	def is_valid(self, dictionary):
		''' Check whether this move is valid, that is
			form the word and look if it exists in the dictionary
			
			@param dictionary: the dictionary
			@return: True if the move is valid, otherwise False
		'''
        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.
        
			@return: True if all Letters in the move are arranged horizontally.
        '''		
		if self.is_empty():
			return False
			
		l, x, y = get_first_move()
		if not is_empty():
			for _l, _x, _y in self.get_moves():
				if _x != x:
					return False
			return True
		
	
	def is_vertical(self):
        ''' Check whether the Move is vertical.
        
			@return: True if all Letters in the move are arranged vertically.
        '''		
		if self.is_empty():
			return False
		
		l, x, y = get_first_move()
		if not is_empty():
			for _l, _x, _y in self.get_moves():
				if _y != y:
					return False
			return True
	
	def calculate_score(self):
		''' Calculates the score for the move
			
			@return : the score for the move.
		'''
		# implementar....
		
    def isEmpty(self):
        ''' Check whether the Move is empty
        
			@return: True if the Move has no Letters in it.
        '''
        
        return self.length() == 0
    
    def try_move(self, board, word, type, row, column):
		''' Try to insert the given word into the board.
		
			@param board: the gameboard
			@param word: the word to insert
			@param type: the type of the letter, see the 
			utility.can_form_a_word function.
			@param row: the row in the gameboard.
			@param column: the column in the gameboard.
			@return: True if it is possible and also valid to insert 
			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:
    ''' Bag represents a "Bag" of Letters. It contains a dictionary
		named __bag_letters, formatted as follow
		{"Letter" : (count, score)}
	'''	
	def __init__(self, lenguage= constants.DEFAULT_LENGUAGE):
		self.__bag_letters = {}
		self.__bag_letters = utility.get_letters_from_file(lenguage)
		
		
	
	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
			@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:
			for key, data in self.get_bag_letters().items():
				if random.randint(0 , 1) and i < amount:
					if data[0] > 0:
						removed.append(Letter(key, data[1]))
						self.get_bag_letters()[key] = (data[0] - 1, data[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
		'''		
		for l in letters:
			count , score = self.get_bag_letters()[l.get_character()]
			self.get_bag_letters()[l.get_character()] = (count + 1, score)
			 
	

	def get_bag_letters(self):
		''' 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