Crossword forge free cod
# If the word doesn't overlap the current word (already in the # doesn't happen on the same letter in each word), we can # other direction but DOESN'T intersect it (i.e. # If the word overlaps another word that is placed in the # If the word is adjacent to any other words in the grid, we can (d = Direction.ACROSS and c + len(word) >= self.num_cols)): If ((d = Direction.DOWN and r + len(word) >= self.num_rows) or # Make sure we aren't inserting the word outside the grid id = letterĭef _word_fits(self, word: str, r: int, c: int, d: Direction): Raise TypeError("Only GridWords can be inserted into the Grid")ĭelta_r, delta_c = grid_deltas()įor idx, letter in enumerate(grid_word.word): S += id if id != "" else "-"Ĭenter = (math.floor(self.num_rows / 2), math.floor(self.num_cols / 2)) # (1) point directly to the right of (or below) a word placed acrossĭef _init_(self, num_rows = 50, num_cols = 50): # (-1) point directly to the left of (or above) a word placed across R = self.r1 + idx*lta_r + delta*lta_cĬ = self.c1 + idx*lta_c + delta*lta_r Raise TypeError("Adjacency is only defined for two GridWords") Return((self.r1, self.c1) in other or (other.r1, other.c1) in self) Raise TypeError("Overlap check is only defined for two GridWords") If letter1 = letter2 and rr1 = rr2 and cc1 = cc2: Rr2 = other.r1 + idx2*lta_c # because the direction is reversed Raise ValueError("Intersection is only defined for GridWords placed in different directions")įor idx1, letter1 in enumerate(self.word):įor idx2, letter2 in enumerate(other.word): Raise TypeError("Intersection is only defined for two GridWords") Return(self.r1 ' requires string or coordinate pair as left operand") # The left operand is a tuple that contains two integers, i.e. Return(f"")Įlif isinstance(item, tuple) and len(item) = 2 and isinstance(item, int) and isinstance(item, int): Raise TypeError("Direction must be an enum of type Direction") Raise ValueError("Row and column positions must be positive integers") If not (isinstance(r, int) and isinstance(c, int) and r >= 0 and c >= 0): Return("ACROSS" if self is Direction.ACROSS else "DOWN")ĭef _init_(self, word: str, r: int, c: int, direction: Direction):
Crossword forge free cod code#
I'd greatly appreciate any feedback on either the algorithm for inserting words into the puzzle (which results is a pretty "formulaic" grid) and the general structure/style of the Python code as well. words = Īnd creates what I'm calling a "sparse crossword" puzzle that looks like this.
This code takes a list of words that all (pairwise) share at least one letter, e.g.