Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Sunday, 27 December 2009

Sudoku for (S)XEmacs

Once a night, there was no soft girlfriend at the side and I decided to play in some game. Tetris is to fast and active, sokoban is completed long ago and World of Goo heats CPU too much — so I choose sudoku. Opened websudoku.com and started playing. Everything is ok, but that is not under SXEmacs and not available offline. Decided to write sudoku.el to play sudoku under SXEmacs. Skim over the internet and found sudoku.el for (X)Emacs. Downloaded, started and disappointed, looked to the code and got depression. So night was spent rewriting sudoku.el to satisfy my needs, namely:

  • Handy, eye-candy and informative interface
  • Possibility to download sudokus from websudoku.com and menneske.no
  • Possibility to print sudokus
  • Possibility to edit sudokus
  • Possibility to save/load sudokus
  • Pencils for master techniques
  • Easy cells autocompletion, so you solve only hard part of the puzzle

Example

Let's run M-x sudoku RET:

sudoku-mode

Here is a bunch of easy cells, for example R5C5 (where cursor is) and of course we are to lazy to fill them, so let's press a, we will get:

autoinsert

Now is really hard part of the puzzle, but peculiarity for the value 4 is seen, so we get the pencil — p and use Nishio technique, starting from R8C9 cell:

pencil
  1. R8C9 -> 4 (Nishio start)
  2. R2C3 -> 4 (only cell in C3 where 4 can be)
  3. R3C7 -> 4 (only cell in R1 where 4 can be)
  4. No place for 4 in 8th square

So 4 can't be in R8C9. C-u p to cancel the pencil. Now we know that 4 is in R2C9:

won

Nice sudoku. I like it, my son will like it too, so let's print it with P. On the paper it will look like this:

printed

Download

Download from http://lgarc.narod.ru/xemacs/sudoku.el

Send notices, bugreports, fixes to lg AT sxemacs.org

Buttons generator

There is outstanging package in (S)XEmacs called xpm-button.el. It can generate button image on fly. It is very handy, but eye-ugly results depresses much. So I decided to write flexibly generator for eye-candy buttons, using Wand-mode. While implementing it, I also extended Wand-mode to get necessary functionality. Here is what I've got:

(wand-button 150 32 "Wand Button"
             :bg "cornflowerblue" :fg "white"
             :round 8.0 :depth 1 :yoff 8 ; :type 'pressed
             :wand-font (make-wand-font :family "Zapfino"))

button1

(wand-button 100 20 "Wand Button"
             :bg "gray80" :fg "black" :round 10.0
             :contrast 0.5 :depth 2 ; :type 'pressed
             :wand-font (make-wand-font :family "Copperplate"))

button2

(wand-button 100 40 (encode-coding-string "привет" 'utf-8)
             :bg "palegreen" :fg "green" :round 0.0
             :depth 4 :yoff -2 ; :type 'pressed
             :wand-font (make-wand-font :family "Arial Black"
                                        :size 20))

button2

Quite interesting to integrate this functionality into the SXEmacs widgets. Current implementation of buttons (using ugly static images) is far from ideal.