Skip to content

Commit

Permalink
refactoring data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
dejangvozdenac committed Sep 17, 2017
1 parent b40bb89 commit cd4793b
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 157 deletions.
26 changes: 14 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from cell import Cell
from clue import Clue
from room import Room
from state import State
from submission import Submission

from flask import Flask, render_template, request, redirect, url_for
# from flask_sqlalchemy import SQLAlchemy
Expand All @@ -28,11 +30,11 @@ def new_puzzle():
room_name = request.form["room_name"]

global rooms
across_clues = parser.create_clues_across(date)
down_clues = parser.create_clues_down(date)
state = parser.create_state(date)
clues = parser.create_clues(date)
puzzle = parser.create_puzzle(date)
grid = State(puzzle, clues)
check = None
rooms[room_name] = Room(across_clues, down_clues, state, check)
rooms[room_name] = Room(clues, grid, check)

return redirect(room_name)

Expand Down Expand Up @@ -70,12 +72,12 @@ def room(room_name):

# 3. The room has been joined and there is an ongoing puzzle.
across_clues = rooms[room_name].clues["across"]
finished_across_clues = list(filter(lambda clue: clue.finished, across_clues))
unfinished_across_clues = list(filter(lambda clue: not clue.finished, across_clues))
finished_across_clues = list(filter(lambda clue: clue.finished(), across_clues))
unfinished_across_clues = list(filter(lambda clue: not clue.finished(), across_clues))

down_clues = rooms[room_name].clues["down"]
finished_down_clues = list(filter(lambda clue: clue.finished, down_clues))
unfinished_down_clues = list(filter(lambda clue: not clue.finished, down_clues))
finished_down_clues = list(filter(lambda clue: clue.finished(), down_clues))
unfinished_down_clues = list(filter(lambda clue: not clue.finished(), down_clues))

return render_template("index.html",
state=rooms[room_name].state,
Expand Down Expand Up @@ -108,14 +110,14 @@ def command():

if command_type == "Fill":
if position:
state.submit_letter(clue, int(position), solution, clues_across, clues_down)
state.submit(Submission.ADD_LETTER, clue, solution=solution, offset=int(position))
else:
state.submit_word(clue, solution, clues_across, clues_down)
state.submit(Submission.ADD_WORD, clue, solution=solution)
elif command_type == "Delete":
if position:
state.delete_letter(clue, int(position), clues_across, clues_down)
state.submit(Submission.DELETE_LETTER, clue, offset=int(position))
else:
state.delete_word(clue, clues_across, clues_down)
state.submit(Submission.DELETE_WORD, clue)
elif command_type == "Check":
rooms[room_name].check_displayed = False
if state.check_solution():
Expand Down
10 changes: 8 additions & 2 deletions cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ class Cell:
# values that Cell.color can take:
BLACK = "."
WHITE = "-"
WHITE_INCORRECT = "/"

def __init__(self, variety, number=None, content=None, answer=None, circled=False):
def __init__(self, x, y, variety, number=None, content=None, answer=None, circled=False, clue_across=None, clue_down=None):
self.variety = variety
self.number = number # the clue number of the cell, or None if cell is not numbered
self.content = content # length 1 string
self.answer = answer # length 1 string
self.circled = circled # boolean
self.circled = circled # boolean
self.coordinate = {}
self.coordinate["X"] = x
self.coordinate["Y"] = y
self.clue_across = clue_across
self.clue_down = clue_down
14 changes: 11 additions & 3 deletions clue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from cell import Cell

class Clue:
# values that Clue.direction can take:
ACROSS = True
DOWN = False

def __init__(self, number, across, question, answer, finished):
def __init__(self, number, direction, question, answer):
self.number = number
self.across = across
self.direction = direction
self.question = question # string
self.answer = answer # string
self.finished = finished
self.cells = [] # a list of Cell objects that the clue contains

def finished(self):
for cell in self.cells:
if cell.content == None:
return False
return True
27 changes: 16 additions & 11 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,46 @@ def save_puzzle(date):
with open("puzs/" + filename, 'wb') as f:
f.write(response.content)

def create_clues_across(date):

def create_clues(date):
clues = []
add_clues_across(clues, date)
add_clues_down(clues, date)

return clues

def add_clues_across(clues, date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)

numbering = p.clue_numbering()
cluesAcross = []
for clue in numbering.across:
answer = ''.join(
p.solution[clue['cell'] + i]
for i in range(clue['len']))
cluesAcross.append(Clue(clue['num'], True, clue['clue'], answer, False))
return cluesAcross
clues.append(Clue(clue['num'], Clue.ACROSS, clue['clue'], answer))
return clues

def create_clues_down(date):
def add_clues_down(clues, date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)

numbering = p.clue_numbering()
cluesDown = []
for clue in numbering.down:
answer = ''.join(
p.solution[clue['cell'] + i]
for i in range(clue['len']))
cluesDown.append(Clue(clue['num'], False, clue['clue'], answer, False))
return cluesDown
clues.append(Clue(clue['num'], Clue.DOWN, clue['clue'], answer))
return clues

def create_state(date):
def create_puzzle(date):
path_name = "puzs/" + date + ".puz"
if not os.path.isfile(path_name):
save_puzzle(date)
p = puz.read(path_name)

state = State(p)
return state
return p
8 changes: 5 additions & 3 deletions room.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from clue import Clue

class Room:
def __init__(self, clues_across, clues_down, state, check):
def __init__(self, clues, state, check):
self.clues = {}
self.clues["across"] = clues_across
self.clues["down"] = clues_down
self.clues["across"] = [x for x in clues if x.direction == Clue.ACROSS]
self.clues["down"] = [x for x in clues if x.direction == Clue.DOWN]
self.state = state
self.check = check
self.check_displayed = True
Loading

0 comments on commit cd4793b

Please sign in to comment.