Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/elements.py
blob: 56d606aeecd4c34e18eb3ffc09bdace6d346c700 (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
#!/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
		'''
		# To do controlar los acentos.
		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
		'''
		# To do controlar los acentos.
		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):
		''' Check whether this move is valid
			
			@return: True if the move is valid
		'''
		# implementar....
	
	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....
		
        
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)
		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 = 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]
			self.get_bag_letters()[l] = (count + 1, score)
			 
	

	def get_bag_letters(self):
		''' Get the bag of letters '''
		return self.__bag_letters