codeword success

codeword gui version 1

first idea for a GUI

It only took a screenshot of my project in action to put myself back on the right path, and a very pleasant afternoon was spent adding the last piece of functionality to it – as a reminder, it’s a program that allows me to enter the settings for a codeword grid, which is a puzzle that looks like a crossword but with number standing for letters. The user tries out a letter in place of a number and eventually solves the whole puzzle, with each number representing a different letter. My program does not create or provide puzzles, but instead you can use it as a tool to help solve commercially available puzzles, to save scrubbing out incorrect guesses.

I had got it to the point where it would display the grid in the console and accept changes via direct entry in the console, so what was left to do was to provide a list of what letters had been used already and to not allow the same letter to be used for two different numbers.

Writing code for the list proved to be straightforward – rather than delving into different data types and debating which was best, I opted for a simple list, with the first entry at index 0 set to “*” and the rest of the entries set to ” ” (space). Adding a letter for a specific number then involved setting the list at that index to the letter, and resetting involved setting the entry back to a space.

I could then call these list functions as part of the setting and resetting a letter, and check whether a letter had been used already before allowing it to be entered into the grid.

My code at the moment has very little help for the user (hence part of my problems yesterday!) and it has very little error checking for input, but neither of these is a real issue as the intention is to put a GUI on the front of the code, to handle display and entries.

And here I hit the next issues: issue one is how to create a GUI in python and issue two is which editor to use to create it.

The book I’ve been working from suggests pythoncard, but I have not been able to install this successfully. I’ve managed to establish that Tkinter is already available as part of the python standard library, but it carries a warning that it will not run with the basic python IDLE, as that does not handle events properly, so that then leaves me looking around for an editor that will do the job.

I enjoy working with the IDLE, as it’s incredibly straightforward, and I object to using more time figuring out how to use an editor than I spend actually coding. I’ve got Eclipse that I’ve used for Java, and I’m currently looking into using that with a pydev plugin, but I’m starting to get frustrated by all these different options, which all seem designed to provide high levels of support with very complex projects and make life very complicated for the casual user who just wants to create a simple project.

The other IDE that I’m trying out is Geany, which I recognise as the software that originally came on my raspberry pi, and so I have used already for python. I’ve got it running with my codeword software, but when it gets to the end of the program, which goes as far as creating the grid, the program then seems to terminate without allowing me to enter further commands via the console. If I can put a GUI onto the program, then the console won’t be needed anyway, but it’s just another frustrating step.

I think with fondness of the days when I learnt programming, when we used C++ with an IDE that allowed us to design a GUI and attach code to it really easily. Even the Java editor we used back in those days seemed far more straightforward, even though I never did really get the hang of the different containers that you could use for java layouts.

Surely these things should be getting easier, not more complex?

Incidentally I’m now running a log of my project, with notes for each version change on what I’m attempting to do. Each feature is being developed piece by piece, with constant resaves as different versions – all versions ending in 4X are the basic version, while 5X added in the list and 6X used the list to check whether the letter was available before using it. I’ve also included screenshots of the project in action and a first sketch of what the interface might look like.

Hopefully if I end up leaving the project again for an extended period (for example while I build up my GUI skills) this log will make it much easier next time to pick it up and catch up with progress. I can’t say I understand every line of the code I’ve written or could duplicate it easily, but at least I know enough to know what it does and how to use it, and which functions are available.

 

Advertisements

Picking up the pieces

codeword throwing an errorIt’s been a while – a hectic six weeks of term, until finally we’ve reached half term and I have time to try to pick up the pieces of my technical developments and see if I can make progress with them.

I left a few projects hovering: there’s the virtual pet that I started in Scratch and app inventor, the Simple Simon game that I made in Scratch and started to convert to app inventor, the general learning I was doing with Python and the codeword project that I started in Python.

It was the codeword project that’s been my ambition for longest, and the project that I remember as the furthest developed, so that’s the one I’ve opted for first. And there I hit my first problem.

Just in trying to pick up the pieces of this project, I’ve learned several things: that it’s really hard to pick up a complex project after so long, that without any real structure it’s difficult to pick it apart and get an overview, and that with no log of changes it’s hard to even find which version to use! I’ve got versions of the file from codeword to codeword6, plus codeword4a and codeword4b. I’ve gone back to codeword4b, as that contained a note saying it was the latest – but of course there’s nothing to say I didn’t progress further and just not remove the note. I remember the project being workable via command line codes, but now it doesn’t seem to respond to even the simplest of commands.

This blog is probably the place I’ve documented my project the most, so I guess I’ll have to read back and see if it’s any help. In the meantime, I really need to pick the code apart and understand it thoroughly, and produce some sort of map or algorithm so that I know how it all ties together – although algorithms/flowcharts become rather complicated when they’re event driven rather than procedural!

In the meantime I’ve learnt that even the most straightforward of projects needs to be thoroughly documented and planned before coding, and that actively writing and testing code is just a small part of any software project. Managing your code so that it can be easily understood and followed, and leaving some sort of explanation to progress, and building in error checking are also incredibly important.

I’ve also proved the point that software needs concentrated effort to get to grips with, and picking it up only for a short while can be counterproductive, as it can take so long to catch up with where you were that you never actually have time to make proper progress. On the other hand, maybe  little and often isn’t too much of a problem – but little and seldom definitely is a no-no!

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