tags:

views:

147

answers:

6

So, I currently have a Board class that is composed of Pieces. Each Piece has a color and a string that describes the kind of piece. It also has a 2d matrix with bits either set on or off, that allows me to know which pixels to paint with the desired color or not.

My question is, which class should have the responsability to draw the pieces on the board? On one hand, I'd say the Piece class should do it. But to do it, I'd have to pass a Board as reference to Piece's Draw() method and although it's not terrible I find it kinda awkward. This raises the problem that Piece would have "to know" the Board class.

On the other hand, I could just have the Piece have a

Boolean[,] IsPixelSet(int x, int y)

and Board would then have a method of the form:

void DrawPieceOnBoard() {
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if (piece.IsPixelSet(x, y) {
                board.DrawPixelAt(x, y, piece.GetColor());
            }
        }
    }
}

How would you do it? And why? I can't see a clear winner in any of these approaches.

Thanks

edit

I'm not talking about actually drawing things on screen. What happens is that I'm implementing a Tetris game (currently, no GUI) and I need at every moment to set the Pixels of a Square on different positions on the board as it falls to the ground. The board basically only has an accessor and a mutator for each one of the (x, y) points. Let's now say I want to draw a Square(a type of Piece) on the Board. Should the Piece know of the board and its Draw() method make the changes to the Board, or should the Board access Piece's getter method and do it itself?

+4  A: 

FWIW, in general MVC theory, neither class should draw itself. Presentation would be a concern of a separate view.

nonnb
Hmmm..I am not talking about drawing it on screen really. I'm talking about setting piece's correspondent pixels on or off on the board.
devoured elysium
I'd say that MVC is not suited for this particular case and would hopelessly overcomplicate things.
MvanGeest
I agree with @MvanGeest. MVC is not an easy pattern to understand and there are different variations of it. Keep it simple!
GarethOwen
+2  A: 

I'd say the Piece draws. For example, your function does not allow to enhance the Piece to allow them to have several colors.

The Piece does not have to know all the Board, just some method (maybe part of an interface) Draw(x,y,color).

pascal
I had not thought of that, and that's indeed a good point.
devoured elysium
+1 this would be my choice. The board should not know too much about the pieces. It just asks them to draw themselves. This is more flexible for later enhancements, as pascal's example where a piece may have several colors, or where it writes it's name as text in the board - maybe different pieces want different text styles. You don't want this logic in the Board class.You can change to be able to change the behavoiur of a piece class without changing the board class. See Open / Closed principle: http://en.wikipedia.org/wiki/Open_Closed_Principle
GarethOwen
A: 

I agree with nonnb, there's no logical concept of "drawing" for a chess piece or a board, it could be argued that the board has references to all the pieces, so it should draw itself. But that argument equally says that it could be a helper object, or the environment that does the drawing.

I'd personally opt for a rendering class (for example, maybe you want to be able to print the board in ASCII for a record of all the moves), or maybe in the future upgrade to a 3d rendering engine or something... that's just my 0.2¢

Beaks
I'm sorry if I wasn't clear enough. I'm not talking about drawing on screen. I'm talking about setting the "abstract" board's matrix pixels on or off.
devoured elysium
I still think the logic belongs in a "Game" object, that game object has a Board, which has many Pieces.
Beaks
The problem that I see with puting that logic in a game object is that I end up having to put getters and setters in both Piece and Board, which isn't tha good of a thing. For example, when puting the logic on Piece, one would only need one setter(on Board). When puting the logic on the Board, we would need a getter(on the Piece).
devoured elysium
+1  A: 

In my opinion the Piece should draw the piece and the Board should draw the board.

I would have something like this:

class Piece 
{
    Image Render(Rectangle bounds) { /*  */ }
}

class Board
{
    void Render(Graphics g)
    {
        //Draw the base

        foreach (piece in Pieces)
        {
            var rect = figureOutPosition(); //Positioning logic to 
            g.DrawImage(location, rect, piece.Render(rect));
        }

        //Draw any overlays

    }
}
Pondidum
I would also choose this method. The board contains the pieces therefore it should be responsible for calling their draw method but the pieces now what kind they are (color, queen, pawn, etc.) Coming from the Java perspective I think there is something like a canvas missing in the render method, an object that the piece draws itself on but that is my only objection.
Janusz
I edited my original post.
devoured elysium
A: 

It does not matter. What you may want to do is to make it so that either board depends on piece or piece on board for everything you do.

Radtoo
A: 

I'd go for the Composite Design Pattern - mostly the same solution as @Pondidum.

  • Drawable has the method draw()
  • Board isA Drawable
  • Piece isA Drawable
  • Board hasA array of Drawable.
Daniel Voina
Composite doesn't have a reason to be here. Read the edit on the original post.
devoured elysium
The edit changes almost all - it makes the display dynamic not a static one as in Composite. I have to think it further.
Daniel Voina