|
|
@@ -1,4 +1,5 @@
|
|
|
import random, string, json
|
|
|
+from tracemalloc import start
|
|
|
|
|
|
global word_list
|
|
|
global letters_dict
|
|
|
@@ -20,6 +21,7 @@ class Player:
|
|
|
self.id = randomword(10)
|
|
|
self.name = name
|
|
|
self.tiles = []
|
|
|
+ self.score = 0
|
|
|
|
|
|
|
|
|
class Game:
|
|
|
@@ -31,6 +33,7 @@ class Game:
|
|
|
self.current_word = ""
|
|
|
self.hand_size = hand_size
|
|
|
self.match_amount = match_amount
|
|
|
+ self.turns = turns
|
|
|
|
|
|
for key, value in letters_dict.items():
|
|
|
for i in range(value["occurrence"]):
|
|
|
@@ -51,9 +54,15 @@ class Game:
|
|
|
|
|
|
starting_player = random.choice([self.player1, self.player2])
|
|
|
self.current_player = starting_player
|
|
|
+ self.starting_player = starting_player
|
|
|
return starting_player
|
|
|
|
|
|
def change_turn(self):
|
|
|
+ if self.current_player != self.starting_player:
|
|
|
+ self.turns -= 1
|
|
|
+ if not self.turns:
|
|
|
+ return False
|
|
|
+
|
|
|
self.current_player = (
|
|
|
self.player1
|
|
|
if self.current_player == self.player2
|
|
|
@@ -69,14 +78,22 @@ class Game:
|
|
|
self.tile_pile = self.discard_pile
|
|
|
self.discard_pile = []
|
|
|
|
|
|
- def check_word(self, word):
|
|
|
- if word not in word_list:
|
|
|
- return final_word
|
|
|
+ return True
|
|
|
|
|
|
+ def check_word(self, word):
|
|
|
final_word = {
|
|
|
"valid": False,
|
|
|
"word": [{"letter": letter, "status": 0} for letter in list(word)],
|
|
|
}
|
|
|
+
|
|
|
+ score = 0
|
|
|
+ for letter in list(word):
|
|
|
+ score += int(letters_dict[letter]["tile_value"])
|
|
|
+ final_word["score"] = score
|
|
|
+
|
|
|
+ if word not in word_list:
|
|
|
+ return final_word
|
|
|
+
|
|
|
temp_player_tiles = self.current_player.tiles.copy()
|
|
|
remaining_letters = []
|
|
|
|
|
|
@@ -115,14 +132,10 @@ class Game:
|
|
|
) and not remaining_letters:
|
|
|
final_word["valid"] = True
|
|
|
|
|
|
- score = 0
|
|
|
- for letter in list(word):
|
|
|
- score += int(letters_dict[letter]["tile_value"])
|
|
|
- final_word["score"] = score
|
|
|
if final_word["valid"]:
|
|
|
+ self.current_player.score += final_word["score"]
|
|
|
self.current_word = word
|
|
|
self.current_player.tiles = temp_player_tiles
|
|
|
- self.change_turn()
|
|
|
return final_word
|
|
|
|
|
|
def discard_letter(self, letter):
|
|
|
@@ -135,10 +148,15 @@ class Game:
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- game = Game("testplayer", 7, 2, 10)
|
|
|
+ game = Game("testplayer", 7, 2, 2)
|
|
|
game.player_join("testplayer2")
|
|
|
game.start_game()
|
|
|
while True:
|
|
|
+ print("Turns: " + str(game.turns))
|
|
|
print(game.current_player.tiles)
|
|
|
- print(game.check_word(input("Word: ")))
|
|
|
+ word = game.check_word(input("Word: "))
|
|
|
+ print(word)
|
|
|
+ if word["valid"]:
|
|
|
+ if not game.change_turn():
|
|
|
+ break
|
|
|
pass
|