Skip to content

Crossword Improvable

Jakub Dakowski edited this page Jun 18, 2023 · 2 revisions

Crossword representation using CrosswordImprovable

This class can be imported from the platyrynchos package:

>>> from platyrhynchos import CrosswordImprovable

It is a subclass of the Crossword class, implementing ColRow class to retrieve columns and rows and a default size. It is used to implement the "smart" word insertion algorithm.

make static method for creating new crosswords

To generate the beginner one-word puzzles, you should use the static method CrosswordImprovable.make. Consult the docstring for the arguments. Note how you can print the crosswords in your console using the print function.

>>> print(CrosswordImprovable.make("dupa", 10, 4))
dupa::::::
::::::::::
::::::::::
::::::::::

>>> print(CrosswordImprovable.make("dupa", 4, 1))
dupa

>>> c = CrosswordImprovable.make("dupa", 5, 1)
>>> print(c)
dupa:

>>> CrosswordImprovable.make("dupa", 1, 1)
Traceback (most recent call last):
    ...
platyrhynchos.commons.exceptions.TooLargeException: h=2 vs max_h=1; v=0 vs max_v=1

check_size

>>> c.check_size(10, 10)
Traceback (most recent call last):
    ...
platyrhynchos.commons.exceptions.TooLargeException: h=10 vs max_h=5; v=10 vs max_v=1

rotate - rotates the crossword in place

>>> c.rotate()
>>> print(c)
d
u
p
a
:

>>> c.rotate()
>>> print(c)
dupa:

It swaps column and row ids and rows_vertical with rows_horizontal (while also changing the ids in them):

>>> c = CrosswordImprovable({(0,1):'a'}, 2, 2, words_horizontal={'a':{(0,1)}}, crossings = {(0,1)})
>>> c.rotate()
>>> c.crossings
{(1, 0)}
>>> c.words_vertical
{'a': {(1, 0)}}
>>> c.words_horizontal
{}

colrow - gets colrow from crossword

This funcion retrieves a ColRow object of a crossword What distinguishes it from a ColRow constructor is that it also checks if this ColRow actually exists in this crossword.

>>> c2 = CrosswordImprovable({(0,1):'a', (2,1):'b', (4,1):'s', (1,1):'g'}, 10, 10, words_horizontal={'a':{(0,1)}}, crossings = {(0,1)})
>>> c2.colrow(True, 0)
Col(0, :a::::::::)

>>> c2.colrow(False, 0)
Row(0, ::::::::::)

>>> c2.colrow(True, 4)
Col(4, :s::::::::)

>>> c2.colrow(True, 11)
Traceback (most recent call last):
    ...
platyrhynchos.commons.exceptions.TooLargeException: h=11 vs max_h=10; v=0 vs max_v=10

add - adding a word to the crossword

This functions retrieves a given column or row and searches for the best way to insert the word into it.

>>> c = CrosswordImprovable.make("dupa", 4, 4)
>>> c.add("peja", (True, 2))
>>> print(c)
dupa
::e:
::j:
::a:

>>> c.add("ej", (False, 2))
>>> print(c)
dupa
::e:
:ej:
::a:

If no better intersection can be found, it will inject the word in the first good region.

>>> c.add("co", (False, 3))
>>> print(c)
dupa
::e:
:ej:
coa:

>>> c.add("uxeo", (True, 1))
>>> print(c)
dupa
:xe:
:ej:
coa:

>>> c.add("ejo", (False, 2))
>>> print(c)
dupa
:xe:
:ejo
coa:

add_letter - adding single letters to the crossword using its coordinates

>>> c = CrosswordImprovable.make("dupa", 4, 4)
>>> c.add_letter((3,3), 'g')
>>> print(c)
dupa
::::
::::
:::g

>>> c.add_letter((3,3), 'g')
>>> print(c)
dupa
::::
::::
:::g
>>> c.crossings
{(3, 3)}

>>> c.add_letter((0,0), 'g')
Traceback (most recent call last):
    ...
platyrhynchos.commons.exceptions.UninsertableException: This field is already occupied (coord=(0, 0); new=g; old=d)