sábado, 27 de febrero de 2010

Mastermind in scheme. Now with GUI!!

plt scheme has a wide collection of libraries, some bundled with the basic package, and some which have to be installed through PLaneT.

GUI library is from the first group, so if you have plt-scheme already installed (not mzshceme alone, but the complete suite, with drscheme and so) you already have everything you need.

WARNING: It seems there's some programming course in NY with an assignment related to writing a mastermind. If it's your case, please, do not use my code blindly. I'm a novice scheme programmer, and probaly, my code is not very idiomatic. Anyway, have fun.

I've been using the mastermind game as a toy project to learn scheme for the last month. And now, it even has a little GUI. I also have a solver in 5 tries (in the worst case) but I haven't embedded it yet in the GUI.

The process of writing GUIs using plt-scheme is easier than I thought. It's great to write little things (we could say it makes easy things really easy). I don't know how difficult it'd be to write a more complex GUI, for example, I haven't found any grid component. Only comboboxes, radiobuttons, buttons and text-fields.

The good part is that it's really easy to assemble these compoonents in layouts, panels, frames, and so. It's also really easy adding menus to windows.

Here follows a little code that lets you play mastermind, fully coded in scheme.

Btw, This is (more or less) what I used last wednesday at flibug meeting to show an example of GUIs in plt. I really enjoyed the meeting: Learning, sharing, having fun, and beers :) .There's some more info about the last flibug meeting in my last post.

2 comentarios:

Jillika dijo...


i am a scheme newbie and chanced upon this game and your code!!! could you please explain what you are doing.
it seems very complicated to me.
thank you.

Raimon Grau dijo...


This code can be divided in 2 big parts:
The mastermind game itself and the GUI.
GUI uses scheme/gui package, so the only secret is going and reading some DrScheme (now Racket) docs.

The mastermind game implemented here is the 'master' part.
First, it defines sol as a solution of length 'num', and values between 0 and max.

Then, there's a function to check white pegs given 2 combinations, and another to check for black pegs.

the total check (check-sols) is just black=black, and white = white - black, as whites contain exact matches, that are already counted as blacks.

Hope that helps.

Btw, there's a solver (the guesser part) that solves that configuration on 5 guesses at worst. It uses the previously mentioned algorithm by Knuth.

I'm not sure if I posted it on the blog. if you want more info, comment.