Python codeword project

Another project I’ve been thinking of (for many, many years, actually, since I first came across the puzzle type) is a program to help solve codeword puzzles.  These are the ones that look like crosswords, except that each square has a number and each number stands for a letter of the alphabet, so you have to crack the code to solve the puzzle.  I love these puzzles but don’t like trying a letter out, changing my mind and having to scrub them back out of the book, so my idea is to create something that will replicate the grid on screen and allow you to type in a number and letter and have the program automatically replace for you, with a reset button for each number if you change your mind.

I think I’ve even made a half-hearted attempt to get going before, but never really got very far, but having seen the listings for python games using the console, including Reversi and tic-tac-toe (or othello and naughts and crosses if you prefer) I’ve realised that it’s worth making a start in console mode to get the basic functionality sorted, with a better interface as a future development.

I’ve had a frustrating time at some points in this project, but I’ve also learned a lot. There were two main learning foci – data structures and classes.  With data structures I think I hit problems because I was thinking too much about two dimensional arrays to store the grid, but in Python I have a list  of lists – the grid looks like [[codeword, codeword, codeword],[codeword, codeword, codeword]] where each of the inner lists represents one line in the grid and each object in that list is an object of a new class I’ve defined.

Python offers a useful way to iterate through lists: you simply call

for <element> in <list>:

and then you can run through them, for example:

for e in self.square:
    for f in e:
        if f.number==num:
            f.setLetter(let)

This cycles through all the elements in the main list, all the elements within that element, and if the number matches it sets the chosen letter.

The codeword object contains three attributes: it has a number, a letter and a boolean called fixed, which enables me to lock a letter/number combination when I’m sure it’s right (or if it’s one of those given as part of the original puzzle).

codeword console in action, showing grid drawn and redrawn

codeword console in action

So far I can display the grid in the console, change, lock, unlock or reset letters and redraw the grid to display any changes.  The next stage will be to provide a key, which shows which letters have been assigned and which are still available to use.

Then I need to tie the engine to an interface, as the console display makes it hard to see the words properly and it’s tiresome to have to type commands directly into the console rather than clicking on buttons.

The idea of writing classes and functions to deal with different aspects of the project is to make it modular, so that I can replace my current codeword object with a similar object that will respond to the same calls but display things differently, so that it’s easier to replace/modify small areas of code rather than having to write the whole lot again, but in Python you can define more than one class in the same file, and indeed have your main program code there as well, whereas in Java it’s usual to have one class per file and import them all.

I haven’t mastered the art of modules yet to include code from other files that I have created, but then I haven’t really needed to, as my entire project is only 134 lines long, including comments and whitespace.

There’s still plenty of room for improvement: as well as the interface upgrade, it’s also possible to provide an option to save and restore games, and the method of inputting the grid needs to be streamlined – at the moment I have to type the numbers in for each line separated by a space, with 0 for a blank.  A check is made that I’ve entered the right number of values for the line, but not that they’re correct, and any mistake means restarting.

Still, I’ve made a working prototype that’s proven the possibility, and had fun developing my thinking skills in the process.  I’m also starting to remember some of what I learned about interfaces to objects, encapsulation and various other topics.  I had an issue with Python’s typing at one point as it doesn’t need its variable types declared, and I was getting muddled between ints and strings, but it just means it’s down to me to watch what I’m doing rather than relying on the language to enforce type or flag up problems.

I’m constantly reminded, though, that it helps to understand what’s going on and what the principles are, and so the importance of understanding theory as well as having practical experience, and how each feeds into the other constantly.

I’ve provided the code below, if anyone’s interested in looking – it’s very much a Work in Progress, and probably very clumsy and muddled in places, so be warned!

codeword puzzle assistant

Advertisements

About emmyleigh
Writer/editor/proofreader who loves technology

One Response to Python codeword project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: