Skip to content

Commit

Permalink
refactored index and split variety into color and is_correct(), now t…
Browse files Browse the repository at this point in the history
…he selected clue pops up below the title
  • Loading branch information
dejangvozdenac committed Sep 17, 2017
1 parent 8d7eda1 commit 6de24a9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
puzs/
.DS_Store
14 changes: 2 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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__":
Expand Down
10 changes: 6 additions & 4 deletions cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
self.clue_down = clue_down

def is_correct(self):
return self.content == self.answer
6 changes: 2 additions & 4 deletions room.py
Original file line number Diff line number Diff line change
@@ -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
self.state = state
40 changes: 14 additions & 26 deletions state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -25,21 +26,21 @@ 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,
answer=puzzle.solution[flat_array_idx],
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],
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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])
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
self.show_incorrect_cells = False
6 changes: 6 additions & 0 deletions static/puzzle.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ div {
contentEditable: true;
}

.selected_clue {
position: relative;
width: 100%;
height: 20px;
}

.filled {
width: 28px;
height: 28px;
Expand Down
12 changes: 8 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{% extends "layout.html" %}

{% block main %}
<center><h1>Crossword Puzzle</h1></center>
<center>
<h1>Crossword Puzzle</h1>
<div class="selected_clue" id="selected_clue"></div>
</center>
<div id="wrap">
<!-- across questions -->
{% include 'index_across_questions.html' %}
Expand All @@ -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 %}
Expand Down Expand Up @@ -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];
});
</script>
{% endblock %}
90 changes: 26 additions & 64 deletions templates/index_crossword_grid.html
Original file line number Diff line number Diff line change
@@ -1,74 +1,36 @@
<div id="wrapper">
{% for row in state.grid %}
{% for cell in row %}
{% if cell.variety == "." %}
{% if cell.color == "." %}
<div class="filled"></div>
{% elif cell.number %}
{% if cell.variety == "/" %}
{% if cell.circled %}
<div class="empty incorrect">
<div class = "circle"></div>
<div class = "numbered"></div>
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% else %}
<div class="empty numbered incorrect">
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% endif %}
{% else %}
{% if cell.circled %}
<div class="empty">
<div class = "circle"></div>
<div class = "numbered"></div>
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% else %}
<div class="empty numbered">
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% endif %}
{% endif %}
{% else %}
{% if cell.variety == "/" %}
{% if cell.circled %}
<div class="empty incorrect">
<div class = "circle"></div>
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% else %}
<div class="empty incorrect">
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% endif %}
{% if cell.number %}
<div class="empty numbered">
{% else %}
{% if cell.circled %}
<div class="empty">
<div class = "circle"></div>
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% else %}
<div class="empty">
{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}
</div>
{% endif %}
<div class="empty">
{% endif %}

{% if state.show_incorrect_cells and not cell.is_correct() %}
<div class="incorrect">
{% endif %}

{% if cell.circled %}
<div class = "circle">
{% endif %}

{% if cell.content %}
<div class="letter">{{ cell.content }}</div>
{% endif %}

{% if cell.circled %}
</div>
{% endif %}

{% if state.show_incorrect_cells and not cell.is_correct() %}
</div>
{% endif %}

</div>
{% endif %}
{% endfor %}
{% endfor %}
Expand Down

0 comments on commit 6de24a9

Please sign in to comment.