Chess piece hierarchy design: inheritance vs type fields

前端 未结 6 755
别跟我提以往
别跟我提以往 2021-02-15 13:23

I have a base class for pieces

 class piece;

and an array containing derived objects

piece* board[8][8];

Advantag

6条回答
  •  孤独总比滥情好
    2021-02-15 14:08

    I would go with the class hierarchy.

    For finding a piece you can keep a separeted list for each piece type. So you know where to look for each piece type.

    For comparison you can rely on virtual methods too.

    Another aproach is to use a component architecture (like described here: http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/), but I think it is too much for a chess game where you clealy know the types and know that those types will not change soon :).

提交回复
热议问题