-
Notifications
You must be signed in to change notification settings - Fork 0
/
computerRuleChecker.py
69 lines (64 loc) · 2.58 KB
/
computerRuleChecker.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
''' One-letter-placements are counted as rows '''
def conformsBetterRules(situation, attachments):
for spot in situation:
if spot in attachments:
return True # checks if any of the spots is an attachment location
return False
def getMainCombo(isRowCombo, spot, occupied, situation):
''' Get the spots that correspond to the main combo in order '''
locations = [spot]
locator = spot
if isRowCombo:
while (((locator + 1) in occupied) or ((locator + 1) in situation)) and (locator%15 != 14):
locator += 1
locations.append(locator)
locator = spot # resets the locator to the original position
while ((locator - 1) in occupied) and (locator%15 != 0):
locator -= 1
locations.insert(0, locator)
else:
while ((locator + 15) in occupied) or ((locator + 15) in situation):
locator += 15
locations.append(locator)
locator = spot # resets the locator to the original position
while (locator - 15) in occupied:
locator -= 15
locations.insert(0, locator)
return locations
def getSideCombo(isRowCombo, spot, occupied):
''' Get the spots that correspond to the side combo in order '''
locations = [spot]
locator = spot
if isRowCombo:
while (locator + 15) in occupied:
locator += 15
locations.append(locator)
locator = spot
while (locator - 15) in occupied:
locator -= 15
locations.insert(0, locator)
else:
while ((locator + 1) in occupied) and (locator%15 != 14):
locator += 1
locations.append(locator)
locator = spot
while ((locator - 1) in occupied) and (locator%15 != 0):
locator -= 1
locations.insert(0, locator)
if locations == [spot]:
return []
return locations
def getAllCombos(situation, occupied, isRowCombo):
''' Gets combos of letters made by a valid placement for a computer '''
combosMade = []
mainCombo = getMainCombo(isRowCombo, situation[0], occupied, situation)
if len(mainCombo) != 1:
combosMade.append(mainCombo)
# the main combo comes first
for spot in situation:
sideCombo = getSideCombo(isRowCombo, spot, occupied)
if sideCombo != []:
combosMade.append(sideCombo)
if len(combosMade) == 0:
combosMade.append(mainCombo) # for situations where a single letter is played by itself
return combosMade # now all of these combos must be checked in the dictionary