aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xsolver.py70
1 files changed, 41 insertions, 29 deletions
diff --git a/solver.py b/solver.py
index 7a5ce13..228f986 100755
--- a/solver.py
+++ b/solver.py
@@ -70,6 +70,8 @@ def test_is_swappable():
class KanaGrid:
+ valid_types_for_chain = ("froz", "norm", "rock")
+
def __init__(self, size, grid, action_count=0, score=0, parent=None):
self.width = size[0]
@@ -128,39 +130,49 @@ class KanaGrid:
and self.action_count == other.action_count
)
-
- def update_score(self):
- chain_length = 0
- kana_iter = iter(self.grid)
- valid_types_for_chain = ("froz", "norm", "rock")
+ def generate_valid_pos_for_chain(self):
for y in range(self.height):
for x in range(self.width):
- kana = next(kana_iter)
-
- if kana.type_name not in valid_types_for_chain:
- continue
-
- kana_checks = [
- self.get_kana((x, y-1)),
- self.get_kana((x+1, y)),
- self.get_kana((x, y+1)),
- self.get_kana((x-1, y)),
- ]
-
- for kana_check in kana_checks:
- if kana_check == kana_void:
- continue
- elif kana_check.type_name == "empt":
- continue
- elif kana_check.type_name == "myst": # don't care as brutforce
- continue
- elif kana_check.type_name in valid_types_for_chain:
- if is_kana_compatible(kana, kana_check):
- chain_length += 1
-
- self.score = chain_length / 2 + 1
+ kana = self.get_kana((x, y))
+ if kana.type_name in self.valid_types_for_chain:
+ yield (x, y)
+ def populate_chain(self, pos1, chain_positions):
+ if pos1 in chain_positions:
+ return
+ kana1 = self.get_kana(pos1)
+ chain_positions.add(pos1)
+ pos2_list = [
+ (pos1[0], pos1[1]-1), # up
+ (pos1[0]+1, pos1[1]), # right
+ (pos1[0], pos1[1]+1), # down
+ (pos1[0]-1, pos1[1]), # left
+ ]
+ for pos2 in pos2_list:
+ if pos2 in chain_positions:
+ continue
+ kana2 = self.get_kana(pos2)
+ if kana2.type_name in self.valid_types_for_chain:
+ if is_kana_compatible(kana1, kana2):
+ self.populate_chain(pos2, chain_positions)
+
+ def longest_chain(self):
+ already_evaluated_pos = set()
+ highest_length = 0
+ highest_length_chain = 0
+ for pos in self.generate_valid_pos_for_chain():
+ if pos in already_evaluated_pos:
+ continue
+ chain = set()
+ self.populate_chain(pos, chain)
+ already_evaluated_pos = already_evaluated_pos.union(chain)
+ if highest_length < len(chain):
+ highest_length = len(chain)
+ highest_length_chain = chain
+ return highest_length #, highest_length_chain # easy to add if needed
+ def update_score(self):
+ self.score = self.longest_chain()
def get_hash(self):
data = ''.join((