So I spent the evening hacking together this little app to draw a standard chess board with some 32×32 chess piece icons from IconBuffet. Sadly, ruby-gnome2 ’s documentation is lacking with respect to Cairo, so I had to switch between using reflection to examine the instance methods of various Cairo classes and their respective C documentation. Graphics programming is usually fun since the rewards are so immediate. But for the first couple hours I was stumped on a handful of code that was merely supposed to draw a single line and a PNG. The app either drew nothing at all, or painted the entire surface black. I eventually traced the errors to an unnecessary scaling function call that caused things to be drawn much, much larger than normal in a clipped region. So trying to rationalize the app’s odd behavior was like driving while wearing binoculars. Nevertheless, Cairo is cleanly designed and powerful. I look forward to messing around with it some more.
Next up is refactoring this simple do-nothing app into a full-fledged widget and by then I’ll have a good enough grasp of the differences between Cairo’s Ruby bindings and the C API to rewrite Davyd Madeley’s excellent GNOME Journalarticles on writing Cairo-based GTK widgets (a Pythonizedversion already exists).
May 19, 2006, 03:35AM PDT | 2 cheers | 0 comments
Every Rubyist and their :pet => dog is creating DSLs these days to provide an elegant solution to a relatively narrow problem scope. So I spent the last few weeks reading and deconstructing Dwemthy’s Array – why the lucky stiff’s bizarre dungeon simulacrum – and a numberof hisblog posts to get a handle on metaclasses (eigenclasses). Things are now starting to fall into place on a prototype framework I’m playing around with for creating Chess variants. It’s part of one of my older ideas that haven’t really seen the light of day, but that’s the subject of another post.
I want a way to define the fundamental moves for each class of ChessPiece with two keywords: can_step and can_jump. These methods add a class variable Foo#moves that lists the steps (x-y couplets) for each valid move. can_step :forward => 1, for example, loads Foo#moves with [[0, 1]]. can_step :forward => (1..2) loads Foo#moves with [[0, 1]] and [[0, 1], [0, 2]]. Here are the class definitions of a few ChessPieces that exhibit standard moves:
class Pawn < ChessPiece
can_step :forward => 1
can_step :forward => 1, :left => 1
can_step :forward => 1, :right => 1
end
class Knight < ChessPiece
can_jump :forward => 1, :left => 2
can_jump :forward => 2, :left => 1
can_jump :forward => 1, :right => 2
can_jump :forward => 2, :right => 1
can_jump :backward => 1, :left => 2
can_jump :backward => 2, :right => 1
can_jump :backward => 1, :left => 2
can_jump :backward => 2, :right => 1
end
class Bishop < ChessPiece
can_step :forward => (1..7), :left => (1..7)
can_step :forward => (1..7), :right => (1..7)
can_step :backward => (1..7), :left => (1..7)
can_step :backward => (1..7), :right => (1..7)
end
May 11, 2006, 08:28AM PDT | 3 cheers | 2 comments