Peg Solitaire


Jon says:

This is the latest in my project to re-implement many of Simon Tatham's Portable Puzzle Collection in Javascript.

I've hopefully taken on board some of the comments made previously, particularly about not polluting the global namespace. I don't think it's particularly elegant code - far too many nested ifs - but it works.

Anyway, I'm really pleased that I've now managed to complete two programming projects. I'll probably carry on creating the odd game here or there when I have the time, but I promise not to keep polluting the TuxRadar feed with information about it!

I love getting feedback, so please do send it. If you like the puzzles and would like to know when I finish new ones, just so I can get more feedback(!) you can always follow my personal Twitter feed (only started using this recently!) @jonrob.

You should follow us on or Twitter

Your comments

Fantastic, how about github?

Hey Jon,

I love that you are doing this, as its one aspect of tuxradar that I think might be a bit lacking on the side of the website, programming and interacting with the readers/listeners about programming.

My only suggestion would be to perhaps move the js source to a version control system and maybe a display hub such as github so that anyone looking to contribute can send u a pull request with changes. Looking at your source, its wonderfully commented and this could be a wonderful opportunity to build a programming hobbiest community around this site, and we could all build towards a html5/js version of the Simon Tatham Collection, your aiming at.

Let me know what you think.

VCS would be good, yes!


A quick glance and it looks good. :) If you do put your code onto GitHub (or similar) as Newky suggests, then it's possible for people to comment on specific lines of code very easily.

I agree with Newky that it's great to see some programming happening on TuxRadar. Moving to GitHub, and perhaps getting people collaborating using Cloud 9 IDE, could get the community that Newky alludes to happening...

I'll take a closer look at the code a bit later, and, if it's ok, email you with comments. It's probably better to do that than to fill the comment thread with lengthy exposition! :)

plays well

Couple of non-coding criticisms, hope you don't mind:

Firstly the design/colours. I've not played this game since I was about 9 (I used to love it though, played it in the car on family outings) and I couldn't remember what's a peg and what's not, so it took me several minutes to work out how to start the game (in retrospect, it's obvious, but still). Be nice if the design (though you're drawing shapes in code so there's little you can do there without going nuts, I realise) or colours made it more obvious what's a peg and what's a gap. Bright red/green colours would do it of course, but I like the muted tones of the containing page so maybe a dull maroon or something? (a subtle, slightly offset radial gradient on the top of the pegs would help even more. I stress subtle. An offset, slightly-lighter circle might look better though, just to suggest a slight shininess).

Secondly, the 'illegal move' alert box is really annoying. Especially given that I will click in a random place to cancel moving a particular peg when I change my mind in which case I know it's an illegal move, it's supposed to be. I don't think it's really necessary to tell someone when it's an illegal move - just don't move the peg. But if you believe it is then putting some text in a div or something would be better, I think.

It's fun though. Brought back memories. And I love the idea of converting all those games to html5. Really nice learning-project.


Thanks to you all for the comments so far :-)

@Spangwiches: that's a fair comment about the pegs - I had the same thought myself. I like your idea of adding some kind of gradient for shininess. Canvas definitely supports this, so it would be something good for me to try out. Also, I'm glad you like the muted tones :-)

Re: the illegal move alert box: that's just a left-over from when I was debugging, really. I was going to put a nice GMailesque error/alert in to the code, but haven't got around to it yet, but maybe this isn't even necessary? Also agree that it would be great to have someway to cancel a move - I was thinking of capturing Esc key presses to do this.

@Newky and barryvan - it sounds like an interesting idea. I'll certainly put the code in a vcs sometime over the next few days (something I need to do anyway - messed some stuff up at one point while working on it - oh how I wished I could roll back!) and then maybe think about how we could do some more organised coding around this.

If you have any more thoughts yourselves, do get in touch, because I'll probably get distracted and not get around to it for weeks!

@barryvan - email if you fancy, but I see nothing wrong with commenting. That way, other people will be able to see the comments well in to the future and maybe benefit from them as well :-) But do which ever you're happiest with - thanks for looking!

I do not have the imagination to think of a title every time I c


I thought the alert box might be a debugging leftover.

I don't think you necessarily need a way to cancel moves - just clicking somewhere I know is illegal is fine, and feels intuitive. Just not when I have to click that box every time :D

If you do do feedback, though, maybe rather than any visual feedback you could play a little ogg file. A (gentle) 'boop' noise which suggests you just did something wrong.

Or, if you don't mind it getting a bit cute, the peg you selected doing a sort of 'head-shake' animation might be nice.

Just thought of a much better way

Can you capture right mouse clicks inside the canvas area (I keep intending to do something with canvas but haven't got around to it yet)?

If so (or even if not, with a different deselecting thing) how about:

When you click a peg, it 'raises up' slightly (i.e. just make the circle slightly bigger (and possibly a tad more saturated, just a tad, to suggest aerial perspective shortening)). So you know which peg you are 'holding'.

Then right click (or if you can't do that, some key press) to put it back down again, so to speak. Or perhaps re-clicking the same peg could do that without it being an error.


Umm.. Why is the domain that of Cornish Holiday Cottage?


Thanks confused Ork.

That was left over from a little casual work I'd done for a friend of a friend before starting at Linux Format. I forgot to remove it after moving the content to it's permanent home. Fixed now though, with a duller index page :-)

By all accounts, it's a very nice cottage though, if you're ever thinking of staying in Cornwall!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Username:   Password: