The Artima Developer Community
Sponsored Link

Python Buzz Forum
White black knight then black white knight

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Thomas Guest

Posts: 236
Nickname: tag
Registered: Nov, 2004

Thomas Guest likes dynamic languages.
White black knight then black white knight Posted: Mar 31, 2008 5:20 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Thomas Guest.
Original Post: White black knight then black white knight
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
Latest Python Buzz Posts
Latest Python Buzz Posts by Thomas Guest
Latest Posts From Word Aligned: Category Python

Advertisement
Small chess board

At the end of yesterday’s article I admitted defeat. I’d developed a script to render chess positions, using a suitable font as a source of scalable bitmasks to represent the pieces. Sadly, you could clearly see the board through the pieces, which meant white pieces on black squares looked wrong. I couldn’t see an easy fix.

Happily one of my readers was more resourceful:

You can make white pieces by drawing a “black” piece in white, then overlaying that with a “white” piece in black.

This is a clever trick which I wish I’d thought of! The redrawn pictures do look better.

Fixed chess board

We need three more lines of code and comments apiece.

def chess_position_font(fen, font_file, sq_size):
    ....
    for sq, piece in filter(not_blank, zip(sqs, pieces)):
        if is_white_piece(piece):
            # Use the equivalent black piece, drawn white,
            # for the 'body' of the piece, so the background
            # square doesn't show through.
            filler = unichr_pieces[piece.lower()]
            put_piece(sq, filler, fill='white', font=font)
        put_piece(sq, unichr_pieces[piece], fill='black', font=font)
    return board

Note, in passing, that I don’t think comments can or should be entirely eliminated from source code — here’s a case where they help.

Even after this hack, the pictures aren’t pixel perfect. But I do like the grey mane you get when a white knight occupies a dark square.

White knight on a black square

Read: White black knight then black white knight

Topic: Let supervisor monitor your services Previous Topic   Next Topic Topic: Write Supervisor Code for GSoC

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use