-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniScrabble.py
187 lines (137 loc) · 4.62 KB
/
miniScrabble.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
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
"""
File: miniScrabble.py
Project: 4
Author: James Lawson, Harrison Pinkerton, Laruie Jones
A small fragment of a scrabble game
"""
from arrays import Array
from abstractBag import AbstractBag
import random
class miniScrabble(AbstractBag):
"""An array-based bag implementation."""
# Class variables
DEFAULT_CAPACITY = 25
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self,
which includes the contents of sourceCollection,
if it is present."""
self._items = Array(miniScrabble.DEFAULT_CAPACITY)
super().__init__(sourceCollection)
def __contains__ (self, item):
""" implements a more efficient search algorithm"""
#write this
for self in self._items:
if self == item:
return True
return False
# Accessor Methods
def __iter__(self):
myModCount = self._modCount
cursor = 0
while cursor < len(self):
yield self._items[cursor]
if myModCount != self._modCount:
raise AttributeError("Cannot modify!")
cursor += 1
def checkWord(self, word):
score = 0
scrabble = self._items
for i in word:
if self.__contains__(i) is True:
self.remove(i)
score += 1
else:
score = -1
self.remove(i)
return score
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
if other is self:
return True
if len(other) != len(self):
return False
if len(other) != len(self):
return False
if type(other) != type(self):
return False
otherIter = iter(other)
for item in self:
if item != next(otherIter):
return False
return True
# Mutator Methods
def generate(self, v,c):
self.clear()
vowels = ["a", "e", "i", "o", "u"]
consonants = ["b", "c", "d", "f", "g", "h", "j", "k" ,"l" ,"m" ,"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]
for i in range(v):
self.add(random.choice(vowels))
for i in range(c):
self.add(random.choice(consonants))
def clear(self):
self._size = 0
self._items = Array(miniScrabble.DEFAULT_CAPACITY)
self._modCount += 1
def add(self, item):
# resize here if needed
#print(self._items)
if len(self._items) == len(self):
self.grow()
newIndex = len(self)
for i in range(len(self)):
if item <= self._items[i]:
newIndex = i
break
for j in range(len(self), newIndex, -1):
self._items[j] = self._items[j -1]
self._items[newIndex] = item
self._size += 1
def remove(self, item):
"""Precondition: item is in self.
Raises: KeyError if item in not in self.
Postcondition: item is removed from self."""
tIndex = 0
for i in range(len(self._items)):
if item == self._items[i]:
tIndex = i
break
#if i == len(self)+1:
#raise IndexError("item is removed from self")
for j in range(tIndex, len(self)-1):
self._items[j] = self._items[j + 1]
self._size -= 1
if self._size < .25*len(self._items):
self.shrink()
def grow(self):
"""Doubles in size"""
tempArray = Array(len(self) * 2)
for i in range(len(self)):
tempArray[i] = self._items[i]
self._items = tempArray
pass
def shrink(self):
"""Becomes half the current size, does not become smaller than
initial capacity."""
half = int(len(self._items) / 2)
halfArray = Array(half)
if half > miniScrabble.DEFAULT_CAPACITY:
for i in range(len(self)):
halfArray[i] = self._items[i]
self._items = halfArray
else:
pass
def main():
b = miniScrabble()
b.generate(10, 15)
print("Avaliable letters: ", b._items)
userWord = input("Make a word:")
score = b.checkWord(userWord)
if score != -1:
print ("You scored", score, "points!")
else:
print ("you cant spell that word")
print ("Your score is:", score)
if __name__ == "__main__":
main()