Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Ramírez <jorgeramirez1990@gmail.com>2010-05-28 21:11:22 (GMT)
committer Jorge Ramírez <jorgeramirez1990@gmail.com>2010-05-28 21:11:22 (GMT)
commit0fc71a6bd4a9c7a92e02465945c4b36a3779c6bd (patch)
treec241bb60021340752958d915085d51cd55f2c91b
parent10f8acfb2f1ab8e9601135f67f3052e028c33def (diff)
Added new functions to the User, computer, elements and game modules.HEADmaster
I also added the Lorena's part.
-rw-r--r--User.py66
-rwxr-xr-xcomputer.py49
-rwxr-xr-xelements.py157
-rw-r--r--game.py29
4 files changed, 267 insertions, 34 deletions
diff --git a/User.py b/User.py
new file mode 100644
index 0000000..1e173fa
--- /dev/null
+++ b/User.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+class User:
+ def __init__(self, bag):
+ ''' Initialize the Computer class.
+
+ @param bag: the bag with letters.
+ '''
+ self.__letters = bag.remove_letters()
+
+
+ def get_letters(self):
+ ''' Return the list of Letters.
+
+ @return: the list of elements.
+ '''
+ return self.__letters
+
+ def letters_length(self):
+ ''' Return the length of the list of Letters.
+
+ @return: the number of elements.
+ '''
+ return len(self.__letters)
+
+ def get_letter_at(self, index):
+ ''' Get the letter at the specified index of the list.
+
+ @param index: the index
+ @return: the Letter
+ '''
+ return self.__letter[index]
+
+ def add_letter(self, letter):
+ ''' Add a new Letter to the list of Letters.
+
+ @param letter: the new Letter.
+ '''
+ self.__letters.append(letter)
+
+
+ def swap_letters(self, bag, letters):
+ ''' Return the given list of Letters to the bag, and take
+ the same amount from the bag.
+
+ @param bag: the bag
+ @param letters: the list of Letters.
+ @return: True if it is possible otherwise False.
+ '''
+ from_bag = bag.remove_letters(len(letters))
+ if from_bag == -1:
+ return False
+ elif len(from_bag) == len(letters):
+ bag.add_letters(letters)
+ for l in letters:
+ self.get_letters().remove(l)
+ for l in from_bag:
+ self.add_letter(l)
+ return True
+ else:
+ bag.add_letters(from_bag)
+ return False
+
+
+
+
+
diff --git a/computer.py b/computer.py
index 87c2f87..e6fd630 100755
--- a/computer.py
+++ b/computer.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
-import elements
+from elements import *
+import random
class Computer:
@@ -12,7 +13,7 @@ class Computer:
@param letters: List of the Letters to use.
'''
- self.__letters = letters
+ self.__letters = bag.remove_letters()
def get_letters(self):
@@ -45,28 +46,36 @@ class Computer:
self.__letters.append(letter)
- def add_letter_from_bag(self, bag, amount = 0):
- ''' Add new Letters to the list of Letters.
+ def swap_letters(self, bag, amount=0):
+ ''' Return the given amount of Letters to the bag, and take
+ the same amount from the bag.
- @param bag: the bag which contains the Letters.
- @amount: the amount.
+ @param bag: the bag
+ @param amount: the amount.
+ @return: True if it is possible otherwise False.
'''
- new_letters = bag.remove_letters(amount)
- for letter in new_letters:
- self.add_letter(letter)
-
-
- def return_letters_to_bag(self, bag, amount = 0):
- ''' Return Letters to the bag.
-
- @param bag: the bag which contains the Letters.
- @amount: the amount.
- '''
- # implementar...
-
-
+ from_bag = bag.remove_letters(amount)
+ if from_bag == -1:
+ return False
+ to_bag = []
+ i = 0
+ amount = len(from_bag)
+ while i < amount:
+ for j in range(0, self.letters_length()):
+ if random.randint(0, 1) and i < amount:
+ l = self.get_letter_at(j)
+ to_bag.append(l)
+ self.get_letters().remove(l)
+ i += 1
+ bag.add_letters(to_bag)
+ for l in from_bag:
+ self.add_letter(l)
+ return True
+
+
+
def make_move(self, board, dict, moves):
''' Function which calculates the move that the computer
will make.
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
+
+
+
diff --git a/game.py b/game.py
index 87f40fb..ea515dd 100644
--- a/game.py
+++ b/game.py
@@ -1,10 +1,11 @@
#!/usr/bin/env python
-import user
-import computer
-import elements
+from User import User
+from computer import Computer
+from elements import *
import constants
import utility
+import random
class Game:
def __init__(self):
@@ -67,3 +68,25 @@ class Game:
for e in l:
list.append(e.get_character())
return list
+
+ def user_swap_letters(self, letters):
+ ''' Return the given list of Letters to the bag, and take
+ the same amount of letters from the bag.
+
+ @param letters: the list of letters to put back.
+ @return: True if it is possible, otherwise False.
+ '''
+ res = self.user.swap_letters(self.bag, letters)
+ return res
+
+ def computer_swap_letters(self):
+ ''' Return the an amount of Letters to the bag, and take
+ the same amount of letters from the bag.
+
+ @return: True if it is possible, otherwise False.
+ '''
+ amount = random.randint(1, 7)
+ res = self.computer.swap_letters(self.bag, amount)
+ return res
+
+