From 6de24a96bd2aacb0a4de3c85d86499ccedfa7a45 Mon Sep 17 00:00:00 2001 From: Dejan Gvozdenac Date: Sun, 17 Sep 2017 17:50:36 -0400 Subject: [PATCH] refactored index and split variety into color and is_correct(), now the selected clue pops up below the title --- .gitignore | 1 + app.py | 14 +---- cell.py | 10 ++-- room.py | 6 +- state.py | 40 +++++-------- static/puzzle.css | 6 ++ templates/index.html | 12 ++-- templates/index_crossword_grid.html | 90 +++++++++-------------------- 8 files changed, 65 insertions(+), 114 deletions(-) diff --git a/.gitignore b/.gitignore index 5887d5b..20c1ee1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc puzs/ +.DS_Store \ No newline at end of file diff --git a/app.py b/app.py index 0fb5461..18e0a3a 100644 --- a/app.py +++ b/app.py @@ -33,8 +33,7 @@ def new_puzzle(): clues = parser.create_clues(date) puzzle = parser.create_puzzle(date) grid = State(puzzle, clues) - check = None - rooms[room_name] = Room(clues, grid, check) + rooms[room_name] = Room(clues, grid) return redirect(room_name) @@ -119,23 +118,14 @@ def command(): else: state.submit(Submission.DELETE_WORD, clue) elif command_type == "Check": - rooms[room_name].check_displayed = False - if state.check_solution(): - rooms[room_name].check = True - else: - rooms[room_name].check = False + state.check_solution() elif command_type == "Uncheck": - rooms[room_name].check_displayed = True - rooms[room_name].check = None state.uncheck_solution() elif command_type == "New Puzzle": return redirect(url_for("new_puzzle", room_name=room_name)) elif command_type == "Switch Room": return redirect(url_for("join")) - if rooms[room_name].check != None: - state.check_solution() - return redirect(url_for("index", room_name=room_name)) if __name__ == "__main__": diff --git a/cell.py b/cell.py index 0909973..f59cf08 100644 --- a/cell.py +++ b/cell.py @@ -2,10 +2,9 @@ class Cell: # values that Cell.color can take: BLACK = "." WHITE = "-" - WHITE_INCORRECT = "/" - def __init__(self, x, y, variety, number=None, content=None, answer=None, circled=False, clue_across=None, clue_down=None): - self.variety = variety + def __init__(self, x, y, color, number=None, content=None, answer=None, circled=False, clue_across=None, clue_down=None): + self.color = color 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 @@ -14,4 +13,7 @@ def __init__(self, x, y, variety, number=None, content=None, answer=None, circle self.coordinate["X"] = x self.coordinate["Y"] = y self.clue_across = clue_across - self.clue_down = clue_down \ No newline at end of file + self.clue_down = clue_down + + def is_correct(self): + return self.content == self.answer \ No newline at end of file diff --git a/room.py b/room.py index a2f46ef..585b82a 100644 --- a/room.py +++ b/room.py @@ -1,10 +1,8 @@ from clue import Clue class Room: - def __init__(self, clues, state, check): + def __init__(self, clues, state): self.clues = {} 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 \ No newline at end of file + self.state = state \ No newline at end of file diff --git a/state.py b/state.py index e34fae1..0ce1024 100644 --- a/state.py +++ b/state.py @@ -7,6 +7,7 @@ class State: def __init__(self, puzzle, clues): self.height = puzzle.height self.width = puzzle.width + self.show_incorrect_cells = False 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.grid = [[None for i in range(self.width)] for j in range(self.height)] @@ -25,13 +26,13 @@ def _populate_grid(self, puzzle, circle_idxs): color = puzzle.fill[flat_array_idx] if color == Cell.BLACK: - self.grid[row][col] = Cell(variety=Cell.BLACK, x=col, y=row) + self.grid[row][col] = Cell(color=Cell.BLACK, x=col, y=row) continue circled = True if flat_array_idx in circle_idxs else False if self._cell_starts_across_clue(row, col) or self._cell_starts_down_clue(row, col): - self.grid[row][col] = Cell(variety=Cell.WHITE, + self.grid[row][col] = Cell(color=Cell.WHITE, x=col, y=row, number=number, @@ -39,7 +40,7 @@ def _populate_grid(self, puzzle, circle_idxs): circled=circled) number += 1 else: - self.grid[row][col] = Cell(variety=Cell.WHITE, + self.grid[row][col] = Cell(color=Cell.WHITE, x=col, y=row, answer=puzzle.solution[flat_array_idx], @@ -76,11 +77,11 @@ def _connect_with_clues(self, puzzle): # private helper method def _cell_starts_across_clue(self, row, col): - return (col == 0 or self.grid[row][col - 1].variety == Cell.BLACK) + return (col == 0 or self.grid[row][col - 1].color == Cell.BLACK) # private helper method def _cell_starts_down_clue(self, row, col): - return (row == 0 or self.grid[row - 1][col].variety == Cell.BLACK) + return (row == 0 or self.grid[row - 1][col].color == Cell.BLACK) def _get_clue_index(self, row, col, direction): if direction == Clue.ACROSS: @@ -92,9 +93,9 @@ def parse_number(self, number, is_across): for row in range(self.height): for col in range(self.width): if self.grid[row][col].number == number: - if is_across and (col == 0 or self.grid[row][col-1].variety == Cell.BLACK): + if is_across and self._cell_starts_across_clue(row, col): return row, col - elif not is_across and (row == 0 or self.grid[row-1][col].variety == Cell.BLACK): + elif not is_across and self._cell_starts_down_clue(row, col): return row, col else: return None, None @@ -111,14 +112,14 @@ def parse_submission(self, place): def submit_letter_exact(self, row, col, letter): if (letter == ' '): self.delete_letter_exact(row, col, cluesAcross, cluesDown) - elif row < self.height and col < self.width and self.grid[row][col].variety != Cell.BLACK: + elif row < self.height and col < self.width and self.grid[row][col].color != Cell.BLACK: old_letter = self.grid[row][col].content self.grid[row][col].content = letter if (old_letter != ' ' and old_letter != None): return def delete_letter_exact(self, row, col): - if self.grid[row][col].variety != Cell.BLACK: + if self.grid[row][col].color != Cell.BLACK: old_letter = self.grid[row][col] self.grid[row][col].content = None if (old_letter == ' ' or old_letter == None): @@ -139,7 +140,7 @@ def delete_letter(self, row, col, offset, is_across): def submit_word(self, row, col, word, is_across): for i in range(len(word)): - if row >= self.height or col >= self.width or self.grid[row][col].variety == Cell.BLACK: + if row >= self.height or col >= self.width or self.grid[row][col].color == Cell.BLACK: return self.submit_letter_exact(row, col, word[i]) @@ -149,7 +150,7 @@ def submit_word(self, row, col, word, is_across): row += 1 def delete_word(self, row, col, is_across): - while row < self.height and col < self.width and self.grid[row][col].variety != Cell.BLACK: + while row < self.height and col < self.width and self.grid[row][col].color != Cell.BLACK: self.delete_letter_exact(row, col) if is_across: col += 1 @@ -160,7 +161,6 @@ def submit(self, submission_type, clue, solution = None, offset = None): try: row, col, is_across = self.parse_submission(clue) except Exception, e: - print "oops" return if row is None: @@ -176,19 +176,7 @@ def submit(self, submission_type, clue, solution = None, offset = None): self.delete_word(row, col, is_across) def check_solution(self): - result = True - for row in range(self.height): - for col in range(self.width): - if self.grid[row][col].content != self.grid[row][col].answer: - self.grid[row][col].variety = Cell.WHITE_INCORRECT - result = False - elif self.grid[row][col].variety != Cell.BLACK: - self.grid[row][col].variety = Cell.WHITE - - return result + self.show_incorrect_cells = True def uncheck_solution(self): - for row in range(self.height): - for col in range(self.width): - if self.grid[row][col].variety == Cell.WHITE_INCORRECT: - self.grid[row][col].variety = Cell.WHITE \ No newline at end of file + self.show_incorrect_cells = False \ No newline at end of file diff --git a/static/puzzle.css b/static/puzzle.css index 5a79576..ee0e2a3 100644 --- a/static/puzzle.css +++ b/static/puzzle.css @@ -7,6 +7,12 @@ div { contentEditable: true; } +.selected_clue { + position: relative; + width: 100%; + height: 20px; +} + .filled { width: 28px; height: 28px; diff --git a/templates/index.html b/templates/index.html index f2a4b23..5ff1d6d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,7 +1,10 @@ {% extends "layout.html" %} {% block main %} -

Crossword Puzzle

+
+

Crossword Puzzle

+
+
{% include 'index_across_questions.html' %} @@ -24,9 +27,9 @@ {% for row in range(0, state.width) %} {% for col in range(0, state.height) %} - {% if state.grid[row][col].variety != "." %} - localStorage.setItem("v_" + {{ row }} + "_" + {{ col }} , {{ state._get_clue_index(row, col, False) }} ); - localStorage.setItem("h_" + {{ row }} + "_" + {{ col }} , {{ state._get_clue_index(row, col, True) }} ); + {% if state.grid[row][col].color != "." %} + localStorage.setItem({{ row }} + "_" + {{ col }} , {{ state._get_clue_index(row, col, False) }} ); + localStorage.setItem({{ row }} + "_" + {{ col }} , {{ state._get_clue_index(row, col, True) }} ); {% endif %} {% endfor %} {% endfor %} @@ -68,6 +71,7 @@ $("#clueText").val(clueNumber + " " + direction[0]); $("#clueText").css("color", "red"); $("#solutionText").focus(); + document.getElementById("selected_clue").innerHTML = clueNumber + " " + direction + ":" + text.split(".")[1]; }); {% endblock %} \ No newline at end of file diff --git a/templates/index_crossword_grid.html b/templates/index_crossword_grid.html index 11cc529..7c9aa79 100644 --- a/templates/index_crossword_grid.html +++ b/templates/index_crossword_grid.html @@ -1,74 +1,36 @@
{% for row in state.grid %} {% for cell in row %} - {% if cell.variety == "." %} + {% if cell.color == "." %}
- {% elif cell.number %} - {% if cell.variety == "/" %} - {% if cell.circled %} -
-
-
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% else %} -
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% endif %} - {% else %} - {% if cell.circled %} -
-
-
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% else %} -
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% endif %} - {% endif %} {% else %} - {% if cell.variety == "/" %} - {% if cell.circled %} -
-
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% else %} -
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% endif %} + {% if cell.number %} +
{% else %} - {% if cell.circled %} -
-
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% else %} -
- {% if cell.content %} -
{{ cell.content }}
- {% endif %} -
- {% endif %} +
+ {% endif %} + + {% if state.show_incorrect_cells and not cell.is_correct() %} +
+ {% endif %} + + {% if cell.circled %} +
+ {% endif %} + + {% if cell.content %} +
{{ cell.content }}
+ {% endif %} + + {% if cell.circled %} +
+ {% endif %} + + {% if state.show_incorrect_cells and not cell.is_correct() %} +
{% endif %} + +
{% endif %} {% endfor %} {% endfor %}