-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble1.py
164 lines (105 loc) · 3.14 KB
/
scrabble1.py
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
import sys
import random
TILES_USED = 0 # records how many tiles have been returned to user
SHUFFLE = False # records whether to shuffle the tiles or not
# inserts tiles into myTiles
def getTiles(myTiles):
global TILES_USED
while len(myTiles) < 7 and TILES_USED < len(Tiles):
myTiles.append(Tiles[TILES_USED])
TILES_USED += 1
# prints tiles and their scores
def printTiles(myTiles):
tiles = ""
scores = ""
for letter in myTiles:
tiles += letter + " "
thisScore = getScore(letter)
if thisScore > 9:
scores += str(thisScore) + " "
else:
scores += str(thisScore) + " "
print("\nTiles : " + tiles)
print("Scores: " + scores)
# gets the score of a letter
def getScore(letter):
for item in Scores:
if item[0] == letter:
return item[1]
scoresFile = open('scores.txt')
tilesFile = open('tiles.txt')
# read scores from scores.txt and insert in the list Scores
Scores = []
for line in scoresFile:
line = line.split()
letter = line[0]
score = int(line[1])
Scores.append([letter,score])
scoresFile.close()
# read tiles from tiles.txt and insert in the list Tiles
Tiles = []
for line in tilesFile:
line= line.strip()
Tiles.append(line)
tilesFile.close()
# decide whether to return random tiles
rand = input("Do you want to use random tiles (enter Y or N): ")
if rand == "Y":
SHUFFLE = True
else:
if rand != "N":
print("You did not enter Y or N. Therefore, I am taking it as a Yes :P.")
SHUFFLE = True
if SHUFFLE:
random.shuffle(Tiles)
myTiles = []
getTiles(myTiles)
printTiles(myTiles)
########################################################################
# Write your code below this
########################################################################
#########Create Dictionary List#############
with open("dictionary.txt", "r+") as d:
dic = d.readlines()
d.close()
dic = [i.strip('\n') for i in dic]
############################################
#Word checker
def checkWord(w):
if w.isalpha() == True:
return True
def checkDict(w):
for i in dic:
if w.lower() == i.lower():
return True
def checkTiles(w):
count = 0
wordLength = len(w)
tiles = myTiles[:]
t = False
for j in range(len(w)):
if wordLength == 0:
tiles = myTiles[:]
for i in range(len(myTiles)):
if (w[count]).upper() == tiles[i]:
tiles.append("")
tiles.pop(i)
wordLength -= 1
count += 1
if wordLength == 0:
return True
###
wordValid = False
while wordValid == False:
word = input("Enter your word here: ")
if checkWord(word) == True:
if checkDict(word) == True:
if checkTiles(word) == True:
print("Cool, this is a valid word\n")
wordValid = True
else:
print("This word cannot be made using the tiles\n")
else:
print("I have never heard of this word\n")
else:
print ("Only use English Letters\n")