Now I can drag and drop pieces on the board. When dropped the piece is centered properly in the square. So I can almost play a game of chess with myself:
![[woodpusher]](/images/screenshots/screenshot_woodpusher_0.01-2.png)
Next step is to look again at the interfaces to gnuchess, phalanx, icc, etc. and prepare to integrate with real players and engines. Read on to see the code I’ve got so far.
// WoodPusher.cs
//
// Author: Jamin P. Gray
//
// (c) 2003 Jamin P. Gray
using System;
using Gtk;
using Gdk;
using Glade;
using Gnome;
using GtkSharp;
public class WoodPusher : Gnome.Program
{
// Members
[Glade.Widget("canvas")]
private Gnome.Canvas myCanvas;
[Glade.Widget("window")]
private Gnome.App myWindow;
private int board_size = 400;
private int square_size;
private double offset_x = 0.0, offset_y = 0.0;
private bool moving = false;
private const string VERSION = "0.01";
private Pixbuf darkSquarePixbuf;
private Pixbuf whitePawnPixbuf;
private Pixbuf whiteKnightPixbuf;
private Pixbuf whiteBishopPixbuf;
private Pixbuf whiteRookPixbuf;
private Pixbuf whiteQueenPixbuf;
private Pixbuf whiteKingPixbuf;
private Pixbuf blackPawnPixbuf;
private Pixbuf blackKnightPixbuf;
private Pixbuf blackBishopPixbuf;
private Pixbuf blackRookPixbuf;
private Pixbuf blackQueenPixbuf;
private Pixbuf blackKingPixbuf;
private Pixbuf lightSquarePixbuf;
private int counter;
public struct Coordinate
{
public int x;
public int y;
}
public enum Piece
{
EM,
WP,
WN,
WB,
WR,
WQ,
WK,
BP,
BN,
BB,
BR,
BQ,
BK
}
public Piece[] boardSetup =
{
Piece.BR, Piece.BN, Piece.BB, Piece.BQ, Piece.BK, Piece.BB, Piece.BN, Piece.BR,
Piece.BP, Piece.BP, Piece.BP, Piece.BP, Piece.BP, Piece.BP, Piece.BP, Piece.BP,
Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM,
Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM,
Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM,
Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM, Piece.EM,
Piece.WP, Piece.WP, Piece.WP, Piece.WP, Piece.WP, Piece.WP, Piece.WP, Piece.WP,
Piece.WR, Piece.WN, Piece.WB, Piece.WQ, Piece.WK, Piece.WB, Piece.WN, Piece.WR
};
public static void Main ( string[] args )
{
WoodPusher app = new WoodPusher( args );
app.Run();
}
public WoodPusher(string[] args)
: base ("WoodPusher " + VERSION, VERSION, Modules.UI, args)
{
square_size = board_size / 8;
Glade.XML appGxml = new Glade.XML (null, "woodpusher.glade", "window", null);
appGxml.Autoconnect (this);
darkSquarePixbuf = Scale ( new Pixbuf ( "pixmaps/square.dark.png" ), square_size );
lightSquarePixbuf = Scale ( new Pixbuf ( "pixmaps/square.light.png" ), square_size );
whitePawnPixbuf = Scale ( new Pixbuf ( "pixmaps/white.pawn.png" ), square_size );
whiteKnightPixbuf = Scale ( new Pixbuf ( "pixmaps/white.knight.png" ), square_size );
whiteBishopPixbuf = Scale ( new Pixbuf ( "pixmaps/white.bishop.png" ), square_size );
whiteRookPixbuf = Scale ( new Pixbuf ( "pixmaps/white.rook.png" ), square_size );
whiteQueenPixbuf = Scale ( new Pixbuf ( "pixmaps/white.queen.png" ), square_size );
whiteKingPixbuf = Scale ( new Pixbuf ( "pixmaps/white.king.png" ), square_size );
blackPawnPixbuf = Scale ( new Pixbuf ( "pixmaps/black.pawn.png" ), square_size );
blackKnightPixbuf = Scale ( new Pixbuf ( "pixmaps/black.knight.png" ), square_size );
blackBishopPixbuf = Scale ( new Pixbuf ( "pixmaps/black.bishop.png" ), square_size );
blackRookPixbuf = Scale ( new Pixbuf ( "pixmaps/black.rook.png" ), square_size );
blackQueenPixbuf = Scale ( new Pixbuf ( "pixmaps/black.queen.png" ), square_size );
blackKingPixbuf = Scale ( new Pixbuf ( "pixmaps/black.king.png" ), square_size );
DrawBoard ( );
SetUpBoard ( boardSetup );
}
public void DrawBoard ( )
{
bool light = true;
for ( int x = 0; x < board_size; x += square_size )
{
light = !light;
for ( int y = 0; y < board_size; y += square_size )
{
light = !light;
CanvasPixbuf boardCanvasPixbuf = new CanvasPixbuf ( myCanvas.Root ( ) );
if ( light )
{
boardCanvasPixbuf.Pixbuf = lightSquarePixbuf;
}
else
{
boardCanvasPixbuf.Pixbuf = darkSquarePixbuf;
}
boardCanvasPixbuf.X = x;
boardCanvasPixbuf.Y = y;
}
}
}
public void SetUpBoard ( Piece[] board )
{
int i = 0;
for ( int y = 0; y < board_size; y += square_size )
{
for ( int x = 0; x < board_size; x += square_size )
{
Pixbuf piece;
switch ( board[i] )
{
case Piece.BP:
piece = blackPawnPixbuf;
break;
case Piece.BN:
piece = blackKnightPixbuf;
break;
case Piece.BB:
piece = blackBishopPixbuf;
break;
case Piece.BR:
piece = blackRookPixbuf;
break;
case Piece.BQ:
piece = blackQueenPixbuf;
break;
case Piece.BK:
piece = blackKingPixbuf;
break;
case Piece.WP:
piece = whitePawnPixbuf;
break;
case Piece.WN:
piece = whiteKnightPixbuf;
break;
case Piece.WB:
piece = whiteBishopPixbuf;
break;
case Piece.WR:
piece = whiteRookPixbuf;
break;
case Piece.WQ:
piece = whiteQueenPixbuf;
break;
case Piece.WK:
piece = whiteKingPixbuf;
break;
default:
piece = null;
break;
}
if (piece != null)
{
CanvasPixbuf canvasPiece = new CanvasPixbuf ( myCanvas.Root ( ) );
canvasPiece.Pixbuf = piece;
canvasPiece.X = x;
canvasPiece.Y = y;
canvasPiece.CanvasEvent += new GnomeSharp.CanvasEventHandler (Piece_Event);
}
i++;
}
}
}
public Pixbuf Scale ( Pixbuf image, int size )
{
return image.ScaleSimple ( size, size, Gdk.InterpType.Bilinear );
}
public Coordinate GetCoordinates ( double x, double y )
{
Coordinate c;
c.x = (int) ( x / square_size + 1 );
c.y = (int) ( y / square_size + 1 );
if (c.x > 8 || c.y > 8 || c.x < 1 || c.y < 1) {
c.x = -1;
c.y = -1;
}
return c;
}
public void OnWindowDeleteEvent (object o, DeleteEventArgs args)
{
Quit ();
args.RetVal = true;
}
void Piece_Event (object obj, GnomeSharp.CanvasEventArgs args) {
EventButton ev = EventButton.New (args.Event.Handle);
CanvasPixbuf item = (CanvasPixbuf) obj;
switch (ev.type) {
case EventType.ButtonPress:
if (ev.button == 1) {
offset_x = ev.x % square_size;
offset_y = ev.y % square_size;
args.RetVal = true;
return;
}
break;
case EventType.ButtonRelease:
if (moving) {
Coordinate c = GetCoordinates ( ev.x, ev.y );
if (c.x != -1) {
double new_x = c.x * square_size - square_size;
double new_y = c.y * square_size - square_size;
item.X = new_x;
item.Y = new_y;
}
args.RetVal = true;
moving = false;
return;
}
break;
case EventType.MotionNotify:
Gdk.ModifierType state = (Gdk.ModifierType) ev.state;
if ((state & Gdk.ModifierType.Button1Mask) != 0) {
moving = true;
item.X = ev.x - offset_x;
item.Y = ev.y - offset_y;
args.RetVal = true;
return;
}
break;
}
args.RetVal = false;
return;
}
}
Tags: Mono










October 29th, 2003 at 11:18 pm
I need help with code. I want pictures too