-
Notifications
You must be signed in to change notification settings - Fork 26
Description
So, I've been thinking of something to come up with in design. To begin, let's get some terminology out of the way:
Board:
A grid consisting of valid locations for a piece to move.
Piece:
An entity that contains logic on how to move and other entities like itself.
Game:
Generally consisting of a board and a set amount of pieces (although not always the case), it handles global logic such as when the game ends, when a piece changes (such as when a pawn reaches the end of the board in a default game), and so on.
Please note that these definitions may need to be extended later since it's difficult coming up with defined terminology to fit the humongous amount of chess variations.
Moving on, there are some things that we know for sure:
- There is always a board.
- There are always pieces.
- The game has to end somehow.
So using, this... we get pretty much nothing. 🎱
So, my idea so far consists of implementing the logic of the game using generic virtual types.
In example:
class Location {
int8_t x;
int8_t y;
//int8_t z; //Perhaps a 3D variation?
}
class Piece {
virtual bool move(short location) = 0; //Returns true on success, fail on failure..
virtual bool attack(short location) = 0;
std::string name;
Location location;
};
class Board{
virtual void place(Piece* piece, Location location) = 0; //This should throw exception on fail.
virtual int move(Location curLoc, Location newLoc) = 0; //No exception, returns error or 0 on success.
};
namespace default {
//We have no reason to make this class virtual that I can see
class Game {
Game(Board* board) { /* Place pieces on the board. */ }
/*
Returns number of moves made. No exception.
This also checks for "rules" such as a check or checkmate and whether the game has a next turn.
*/
int next();
}
}
Obvious flaws are whether the piece should move or the board should move the piece or the game should move the piece. I like the idea of virtual interfaces but I'm not so sure about the interface I'm introducing... this is where I fall short most of the time anyways. Any ideas would be lovely.
Also, I'm starting to think there's no reason to seperate "Game" and "Board" since game needs to know the dimensions of the board anyways.